Elasticsearch Document Bulk API 详解、原理与示例,springboot 前后端分离教程
private long sizeInBytes = 0:整个 Bulk 请求的大小。
通过 add api 为 BulkRequest 添加一个请求。
2、Bulk API 请求格式详解
Bulk Rest 请求协议基于如下格式:
POST _bulk
{ "index" : { "_index" : "test", "_type" : "_doc", "_id" : "1" } }
{ "field1" : "value1" }
{ "delete" : { "_index" : "test", "_type" : "_doc", "_id" : "2" } }
{ "create" : { "_index" : "test", "_type" : "_doc", "_id" : "3" } }
{ "field1" : "value3" }
{ "update" : {"_id" : "1", "_type" : "_doc", "_index" : "test"} }
{ "doc" : {"field2" : "value2"} }
其请求格式定义如下(restfull):
POST 请求,其 Content-Type 为 application/x-ndjson。
每一个命令占用两行,每行的结束字符为\r\n。
第一行为元数据,“opType”
《一线大厂 Java 面试题解析+后端开发学习笔记+最新架构讲解视频+实战项目源码讲义》
【docs.qq.com/doc/DSmxTbFJ1cmN1R2dB】 完整内容开源分享
: {元数据}。
第二行为有效载体(非必选),例如 Index 操作,其有效载荷为 IndexRequest#source 字段。
opType 可选值 index、create、update、delete。
公用元数据(index、create、update、delete)如下
1)_index :索引名
2)_type:类型名
3)_id:文档 ID
4)routing:路由值
5)parent
6)version:数据版本号
7)version_type:版本类型
各操作特有元数据
1、index | create
1)pipeline
2、update
1)retry_on_conflict :更新冲突时重试次数。
2)_source:字段过滤。
有效载荷说明
1、index | create
其有效载荷为_source 字段。
2、update
其有效载荷为:partial doc, upsert and script。
3、delete
没有有效载荷。
对请求格式为什么要设计成 metdata+有效载体的方式,主要是为了在接受端节点(所谓的接受端节点是指收到命令的第一节点),只需解析 metadata,然后将请求直接转发给对应的数据节点。
3、bulk API 通用特性分析
3.1 版本管理
每一个 Bulk 条目拥有独自的 version,存在于请求条目的 item 的元数据中。
3.2 路由
每一个 Bulk 条目各自生效。
3.3 Wait For Active Shards
通常可以设置 BulkRequest#waitForActiveShards 来要求 Bulk 批量执行之前要求处于激活的最小副本数。
4、Bulk Demo
public static final void testBulk() {
RestHighLevelClient client = EsClient.getClient();
try {
IndexRequest indexRequest = new IndexRequest("twitter", "_doc", "12")
.source(buildTwitter("dingw", "2009-11-18T14:12:12", "test bulk"));
UpdateRequest updateRequest = new UpdateRequest("twitter", "_doc", "11")
.doc(new IndexRequest("twitter", "_doc", "11")
.source(buildTwitter("dingw", "2009-11-18T14:12:12", "test bulk update")));
BulkRequest request = new BulkRequest();
request.add(indexRequest);
request.add(updateRequest);
BulkResponse bulkResponse = client.bulk(request, RequestOptions.DEFAULT);
for (BulkItemResponse bulkItemResponse : bulkResponse) {
if (bulkItemResponse.isFailed()) {
BulkItemResponse.Failure failure = bulkItemResponse.getFailure();
System.out.println(failure);
continue;
}
DocWriteResponse itemResponse = bulkItemResponse.getResponse();
if (bulkItemResponse.getOpType() == DocWriteRequest.OpType.INDEX
|| bulkItemResponse.getOpType() == DocWriteRequest.OpType.CREATE) {
IndexResponse indexResponse = (IndexResponse) itemResponse;
最后希望可以帮助到大家!
千千万万要记得:多刷题!!多刷题!!
之前算法是我的硬伤,后面硬啃了好长一段时间才补回来,算法才是程序员的灵魂!!!!
篇幅有限,以下只能截图分享部分的资源!!
(1)多线程(这里以多线程为代表,其实整理了一本 JAVA 核心架构笔记集)

(2)刷的算法题(还有左神的算法笔记)

(3)面经+真题解析+对应的相关笔记(很全面)

(4)视频学习(部分)
ps:当你觉得学不进或者累了的时候,视频是个不错的选择
在这里,最后只一句话:祝大家 offer 拿到手软!!
评论