作者介绍:简历上没有一个精通的运维工程师,下面的思维导图也是预计更新的内容和当前进度(不定时更新)。

中间件,我给它的定义就是为了实现某系业务功能依赖的软件,包括如下部分:

Web服务器

代理服务器

ZooKeeper

Kafka

RabbitMQ

Hadoop HDFS

Elasticsearch ES (本章节)

上个小节我们介绍了分片和副本,并且通过命令创建索引,在创建索引的时候定义了分片和副本,但是我们并没有向索引里面写入数据,今天我们就来介绍如何向Elasticsearch(ES)里面写入数据。

数据写入

自动生成ID

这里如果索引不存在,则会自动创建索引(按照默认的规则定义分片和副本,1分片1副本),--d后面数据就是要写入的数据。

代码语言:javascript复制curl -X POST "http://localhost:9200/your_index/_doc" \

-H "Content-Type: application/json" \

-d '{

"field1": "value1",

"field2": 100,

"timestamp": "2023-10-01T12:00:00"

}'手工指定ID

这个就是我们前面讲解过的指定id,这个id在内容之外的。

代码语言:javascript复制curl -X PUT "http://localhost:9200/your_index/_doc/1" \

-H "Content-Type: application/json" \

-d '{

"field1": "value_for_id_1",

"field2": 200

}'从文件写入数据

代码语言:javascript复制[root@localhost ~]# cat bulk_data.txt

{ "index": { "_index": "your_index", "_id": "2" } }

{ "field1": "value1", "field2": 100 }

{ "create": { "_index": "your_index", "_id": "3" } }

{ "field1": "value2", "field2": 200 }代码语言:javascript复制curl -X POST "http://localhost:9200/_bulk" \

-H "Content-Type: application/x-ndjson" \

--data-binary @bulk_data.txt数据查询

模糊查询

注:这个方式只少量测试数据,大量的数据查询需要加入更多的条件,这个涉及到Mapping(映射)。下面的数据也就是我们前面通过3种方式插入的数据。

代码语言:javascript复制[root@localhost ~]# curl -X GET "http://192.168.31.172:9200/your_index/_search?pretty"

{

"took" : 121,

"timed_out" : false,

"_shards" : {

"total" : 1,

"successful" : 1,

"skipped" : 0,

"failed" : 0

},

"hits" : {

"total" : {

"value" : 4,

"relation" : "eq"

},

"max_score" : 1.0,

"hits" : [

{

"_index" : "your_index",

"_type" : "_doc",

"_id" : "EYIrdZgBLdTDE3X6KcYS", #随机生成did

"_score" : 1.0,

"_source" : { #source里面才是我们写入的内容

"field1" : "value1",

"field2" : 100,

"timestamp" : "2023-10-01T12:00:00"

}

},

{

"_index" : "your_index",

"_type" : "_doc",

"_id" : "2", #从文件里面读取写入es的数据

"_score" : 1.0,

"_source" : {

"field1" : "value1",

"field2" : 100

}

},

{

"_index" : "your_index",

"_type" : "_doc",

"_id" : "3",

"_score" : 1.0,

"_source" : {

"field1" : "value2",

"field2" : 200

}

},

{

"_index" : "your_index",

"_type" : "_doc",

"_id" : "1", #指定的es的id

"_score" : 1.0,

"_source" : {

"field1" : "value_for_id_1",

"field2" : 200

}

}

]

}

}虽然通过curl命令也很容易实现对ES的写入和查询,但是实际情况下我们几乎不会不会使用这个方式写入数据,查询通过curl查询命令在运维层面可能使用会略多一点。

字段过滤查询

虽然这个查询和上面的查询结构是一样的,但是这个是带有查询条件的。

代码语言:javascript复制curl -X GET "http://localhost:9200/your_index/_search?pretty" \

-H "Content-Type: application/json" \

-d '{

"_source": ["field1", "field2"], # 只返回指定字段

"size": 5, # 返回5条文档

"query": { "match_all": {} } # 匹配所有文档

}'