验证ElasticSearch的多线程写入安全性问题

背景信息:

单节点、一个分片,分片名称:test1,一个client连接

直接上代码:

package com.example.test.demo;

import org.apache.http.HttpHost;
import org.apache.http.client.config.RequestConfig;
import org.elasticsearch.action.bulk.BulkRequest;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestClientBuilder;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.Strings;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.builder.SearchSourceBuilder;

import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class InsertTest {
    private static  RestHighLevelClient client;
    static {
      client = new RestHighLevelClient(
        RestClient.builder(new HttpHost("XX", 9200, "http"))
                .setRequestConfigCallback(new RestClientBuilder.RequestConfigCallback() {
                    // 该方法接收一个RequestConfig.Builder对象,对该对象进行修改后然后返回。
                    @Override
                    public RequestConfig.Builder customizeRequestConfig(
                            RequestConfig.Builder requestConfigBuilder) {
                        return requestConfigBuilder.setConnectTimeout(5000 * 1000) // 连接超时(默认为1秒)
                                .setSocketTimeout(6000 * 1000);// 套接字超时(默认为30秒)//更改客户端的超时限制默认30秒现在改为100*1000分钟
                    }
                }));// 调整最大重试超时时间(默认为30秒).setMaxRetryTimeoutMillis(60000);
    }
    public static void batchInsert(RestHighLevelClient client) throws Exception {
        BulkRequest request = new BulkRequest();
        for(int i = 0 ;i < 10000;i++) {
            (new Thread(new InsertDataThread(client,i))).start();
        }
    }


    public static void main(String[] args) throws Exception {
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = new Date();
        System.out.println("开始查询时间:" + df.format(date));
        InsertTest insertTest = new InsertTest();
        //InsertTest.batchInsert(client);
        insertTest.query(client);
    }

    public void query(RestHighLevelClient client) throws IOException, ParseException {
        SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
        sourceBuilder.from(0);
        sourceBuilder.size(1000);
        sourceBuilder.fetchSource(new String[]{"*"}, Strings.EMPTY_ARRAY);
        SearchRequest searchRequest = new SearchRequest();
        //sourceBuilder.sort("reqtime_range", SortOrder.DESC);
        searchRequest.source(sourceBuilder);
        // 查询ES
        searchRequest.indices("test1");
        SearchResponse searchResponse = null;
        searchResponse = client.search(searchRequest, RequestOptions.DEFAULT);
        SearchHit[] hits = searchResponse.getInternalResponse().hits().getHits();
        System.out.println("现在的长度:"+hits.length);
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = new Date();
        System.out.println("结束查询时间:" + df.format(date));
    }
}
package com.example.test.demo;

import net.sf.json.JSONObject;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.xcontent.XContentType;

import java.io.IOException;

public class InsertDataThread implements Runnable{
    private RestHighLevelClient client;
    private int i;

    public InsertDataThread(RestHighLevelClient client,int i) {
        this.client = client;
        this.i = i;
    }

    @Override
    public void run() {
        IndexRequest request = this.add(i);
        try {
            IndexResponse indexResponse = client.index(request, RequestOptions.DEFAULT);
            System.out.println("返回的状态:" + indexResponse.status().getStatus());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public IndexRequest add(int i) {
        IndexRequest request1 = new IndexRequest("test1");
        JSONObject obj = new JSONObject();
        obj.put("id",i);
        obj.put("name","alice"+i);
        obj.put("sex","women"+i);
        request1.source(obj, XContentType.JSON);
        return request1;
    }
}

插入的数据量:

调用main方法插入,查询,直接看下查询结果:

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
根据提供的引用内容,没有找到关于ES多线程分组分页查询的信息。不过,ES支持多线程查询和分组分页查询,可以分别介绍一下。 1. ES多线程查询 ES支持多线程查询,可以通过设置线程池来控制并发查询的数量。以下是一个使用Python Elasticsearch库进行多线程查询的例子: ```python from elasticsearch import Elasticsearch from elasticsearch.helpers import parallel_bulk from multiprocessing import Pool es = Elasticsearch() def process_data(data): # 处理数据的函数 pass def index_data(data): # 索引数据的函数 pass def run_query(query): # 运行查询的函数 pass def run_parallel_bulk(data): with Pool(processes=4) as pool: for success, info in parallel_bulk(es, data, index='my-index', chunk_size=1000, thread_count=4): if not success: print('A document failed:', info) ``` 在上面的例子中,我们使用了Python的multiprocessing库来创建一个进程池,然后使用Elasticsearch的parallel_bulk函数来并发地索引数据。 2. ES分组分页查询 ES支持分组分页查询,可以使用Elasticsearch的search方法来实现。以下是一个使用Python Elasticsearch库进行分组分页查询的例子: ```python from elasticsearch import Elasticsearch es = Elasticsearch() def search_data(): query = { "size": 0, "aggs": { "group_by_field": { "terms": { "field": "my_field", "size": 10 }, "aggs": { "group_by_date": { "date_histogram": { "field": "my_date_field", "interval": "day" }, "aggs": { "sum_by_field": { "sum": { "field": "my_sum_field" } } } } } } } } res = es.search(index="my-index", body=query) return res ``` 在上面的例子中,我们使用了Elasticsearch的聚合功能来进行分组分页查询。我们首先按照my_field字段进行分组,然后按照my_date_field字段进行日期直方图聚合,最后对my_sum_field字段进行求和聚合。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值