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”
}
} ]
}