ES(6): access elasticsearch via curl

    curl是一个非常实用的、用来与服务器之间传输数据的工具;支持的协议包括 (DICT, FILE, FTP, FTPS, GOPHER, HTTP, HTTPS, IMAP, IMAPS, LDAP, LDAPS, POP3, POP3S, RTMP, RTSP, SCP, SFTP, SMTP, SMTPS, TELNET and TFTP),curl设计为无用户交互下完成工作,linux curl功能十分强大,命令参数十分多, 可运行“man curl”命令查看

目录:

  • 访问ES:_cat系列
  • 访问ES:_cluster系列
  • 访问ES:_nodes系列
  • 访问ES:{index}系列

访问ES:_cat系列


  • /_cat/allocation
  • /_cat/shards
  • /_cat/master
  • /_cat/nodes
  • /_cat/indices
  • /_cat/segments
  • /_cat/count
  • /_cat/recovery
  • /_cat/health
  • /_cat/pending_tasks
  • /_cat/aliases
  • /_cat/thread_pool
  • /_cat/plugins
  • /_cat/fielddata
  • /_cat/XXXX/{index}
  • 示例如下图:

访问ES:_cluster系列


  • /_cluster/health?pretty             (查询设置集群状态, pretty=true表示格式化输出)
  • /_cluster/stats?pretty                (显示集群系统信息,包括CPU JVM等等)
  • /_cluster/state?pretty                (集群的详细信息。包括节点、分片等)
  • /_cluster/pending_tasks?pretty   (获取集群堆积的任务)
  • /_cluster/nodes/192.168.1.1/_shutdown   (关闭指定节点)
  • /_cluster/nodes/_master/_shutdown         (关闭主节点)
  • /_shutdown?delay=10s                            (delay=10s表示延迟10秒关闭所有节点)
  • /_cluster/nodes/_shutdown 或 /_cluster/nodes/_all/_shutdown    (关闭所有节点)
  • 修改集群配置, transient 表示临时的,persistent表示永久的, 如下代码
    /_cluster/settings -d '{
        "persistent" : {
            "discovery.zen.minimum_master_nodes" : 2
        }
    }'

访问ES:_nodes系列


  • /_nodes/stats?pretty=true
  • /_nodes/192.168.1.2/stats?pretty=true
  • /_nodes/process
  • /_nodes/_all/process
  • /_nodes/192.168.1.2,192.168.1.3/jvm,process
  • /_nodes/192.168.1.2,192.168.1.3/info/jvm,process
  • /_nodes/192.168.1.2,192.168.1.3/_all
  • /_nodes/hot_threads

访问ES:{index}系列


  • 简单创建索引: curl -XPUT 'http://10.0.0.5:12000/test/'
  • 删除索引:      curl -XDELETE 'http://10.0.0.5:12000/test/'
  • 设置mapping,如下示例
    curl -XPUT 'http://10.0.0.5:12000/etlstasday' -d '
    {    
      "settings": {
        "number_of_shards": 5,
        "number_of_replicas": 1
      },
      "mappings": {
        "etlstasday": {
          "properties": {
            "电站名称": {
              "type": "text",
              "fields": {
                "keyword": {
                  "ignore_above": 256,
                  "type": "keyword"
                }
              }
            },
            "业务日期": {
              "format": "yyyyMMddZ",
              "type": "date"
            },
            "总电量": {
              "type": "double"
            },
            "总收入(元)": {
              "type": "double"
            }
          }
        }
      }
    }'
    View Code
  • mapping 字段类型参见:https://www.elastic.co/guide/en/elasticsearch/reference/current/mapping-types.html
  • 插入数据,如下
    curl -XPUT 'http://10.0.0.5:12000/etlstasday/etlstasday/1' -d '
    { 
            "电站名称": "测试数据1",
            "业务日期": "20160101+08:00",
            "总电量": "1000",
            "总收入(元)": "15000"
    }'
    View Code
  • 查询数据:curl -XGET 'http://10.0.0.5:12000/etlstasday/etlstasday/1'
  • 查询某个索引下某个类型的所有记录: curl -XGET 'http://10.0.0.5:12000/etlstasday/etlstasday/_search?pretty'
  • 查询某个索引下所有数据: curl -XGET 'http://10.0.0.5:12000/etlstasday/_search?pretty'
  • 查询所有索引数据:     curl -XGET 'http://10.0.0.5:12000/_search?pretty'
  • 使用JSON参数的查询: (注意 query 和 term 关键字): $ curl localhost:9200/film/_search -d '{"query" : { "term": { "tag":"bad"}}}'

日常总结:


  • 修改索引最大返回记录
  • PUT /索引名/_settings
    {
        "index" : {
            "max_result_window" : 20000
        }
    }
  • 创建索引
原文地址:https://www.cnblogs.com/tgzhu/p/6437743.html