背景信息:
单节点、一个分片,分片名称: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方法插入,查询,直接看下查询结果: