HTTP协议简介和系统命令curl,AIP调用 (ELK中运用)

http请求由三部分组成
  • 分别是:请求行,消息报头,请求正文
  • 请求行的URI和协议版本,格式如下:
    Method Request-URI HTTP-Version CRLF
  • http请求方法
    –常用方法 GET,POST,HEAD
    –其他方法 OPTIONS,PUT,DELETE,TRACE和CONNECT
  • ES常用
    – PUT — 增
    –DELETE --删
    –POST --改
    –GET --查
    系统命令 curl
    • 在linux 中curl 是一个利用URL规则在命令行下工作的文件传输工具,可以说是一款很强大的http命令行工具.它支持多种请求模式,自定义请求头等强大功能,是一款综合工具
    • curl 常用参数介绍
      – -A 修改请求 agent
      – -X 设置请求方法
      – -i 显示返回头信息
    RESTful API 调用
    • Elasticsearch提供了一系列RESTful的API
      – 检查集群,节点.索引的健康度,状态和统计
      – 管理集群,节点,索引的数据及元数据
      – 对索引进行CRUD操作及查询操作
      – 执行其他高级操作如分页,排序,过渡等

POST或PUT数据使用json格式

  • JSON
    – JSON(JavaScript Object Notation),意思是JavaScript 对象表示法,它是一种基于文本独立于语言的轻量级数据交换格式.
    – JSON格式
    – 数组 [ “aa”,“bb”,“cc” ]
    – 键值对 { key:value }

RESTful API 的简单使用

– _cat API 查询集群状态,节点信息
– V参数显示详细信息

]# http://192.168.1.11:9200/_cat/health?v

– help 显示帮助信息

]# http://192.168.1.11:9200/_cat/health?help
  • Rest API 的简单使用

– nodes 查询节点状态信息

  ]# http://192.168.1.11:9200/_cat/nodes?v

– 索引信息

  ]# http://192.168.1.11:9200/_cat/indices?v
  • Rest API 增加
    – 创建一个索引,并设置分片数量与副本数量
  ]# curl -XPUT 'http://192.168.1.11:9200/home/' -d  '{
  "settings":{
  	"index":{
  		"number_of_shards":5,
  		"number_of_replicas":1
  		}
  	}
  }'

RESTful API 插入数据

– (增)加数据,使用PUT方法
– 调用方式: 数据库地址/索引/类型/id值

]#[root@es5 ~]# curl -X PUT "http://192.168.1.11:9200/home/tan/1" -d '{
"职业":"诗人",
"名字":"李白",
"称号":"诗仙",
"年代":"唐"
}'

POST修改

– 修(改)数据,使用POST方法
– 在修改数据的时候必须调用 _update关键字
– 调用方式: 数据库地址/索引/类型/id值/_update

]#[root@es1 ~]# curl -XPUT "http://192.168.1.11:9200/home/tan/3/_update" -d '{
 "doc":{
 "年代": "唐代"
 }
 }'

(查) 查询

– 查询使用GET方法,GET为默认方法
– 查询显示结果时候可以用 pretty规范显示格式
– 多条查询需要使用_mget 关键字配合json调用

]#[root@es1 ~]# curl -XGET 'http://192.168.1.11:9200/home/tan/1/'

(删) 除

– 删除时候可以是文档,也可以是库,但不能是类型

]#[root@es1 ~]# curl -XDELETE 'http://192.168.1.11:9200/home/tan/1/'
]#[root@es1 ~]# curl -XDELETE 'http://192.168.1.11:9200/home'

批量导入数据

  • 使用_bulk批量导入数据
    – 批量导入数据使用POST方式,数据格式为json,URL编码使用data-binary
    – 导入含有index配置的json 文件
    #gzip -d logs.jsonl.gz
    #curl -XPORT ‘http://192.168.1.11:9200/_bulk’ --data-binary@logs.jsonl

map映射

  • mapping :
    – 映射 :创建索引的时候,可以预先定义字段的类型及相关属性
    – 作用: 这样会让索引建立得更加的细致和完善
    – 分类:静态映射和动态映射
    – 动态映射: 自动根据数据进行相应的映射
    –静态映射: 自定义字段映射数据类型
案例:

使用curl 命令为集群批量导入数据,并查看
测试文件获取:

[root@es1 ~]# wget https://github.com/remembertr/elasticsearch--/blob/master/logs.jsonl.gz

[root@es1 ~]# gzip  -d logs.jsonl.gz 
[root@es1 ~]# curl -X POST "http://192.168.1.11:9200/_bulk"  \ 
--data-binary @logs.jsonl
使用GET查询结果
[root@es1 ~]# curl -XGET 'http://192.168.1.11:9200/_mget?pretty' -d '{
> "docs":[
>      {
>         "_index":"shakespeare",
>         "_type:":"act",
>         "_id":0
> },
> {
>         "_index":"shakespeare",
>         "_type:":"line",
>         "_id":0
> },
> {
>         "_index":"tedu",
>         "_type:":"teacher",
>         "_id":25
> }
> ]
> }'

{
“docs” : [ {
“_index” : “shakespeare”,
“_type” : null,
“_id” : “0”,
“error” : {
“root_cause” : [ {
“type” : “index_not_found_exception”,
“reason” : “no such index”,
“resource.type” : “index_expression”,
“resource.id” : “shakespeare”,
“index” : “shakespeare”
} ],
“type” : “index_not_found_exception”,
“reason” : “no such index”,
“resource.type” : “index_expression”,
“resource.id” : “shakespeare”,
“index” : “shakespeare”
}
}, {
“_index” : “shakespeare”,
“_type” : null,
“_id” : “0”,
“error” : {
“root_cause” : [ {
“type” : “index_not_found_exception”,
“reason” : “no such index”,
“resource.type” : “index_expression”,
“resource.id” : “shakespeare”,
“index” : “shakespeare”
} ],
“type” : “index_not_found_exception”,
“reason” : “no such index”,
“resource.type” : “index_expression”,
“resource.id” : “shakespeare”,
“index” : “shakespeare”
}
}, {
“_index” : “tedu”,
“_type” : null,
“_id” : “25”,
“error” : {
“root_cause” : [ {
“type” : “index_not_found_exception”,
“reason” : “no such index”,
“resource.type” : “index_expression”,
“resource.id” : “tedu”,
“index” : “tedu”
} ],
“type” : “index_not_found_exception”,
“reason” : “no such index”,
“resource.type” : “index_expression”,
“resource.id” : “tedu”,
“index” : “tedu”
}
} ]
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值