es裏面提供了兩種批量建索引的方法:java
1,使用 Bulk Api 特色是:使用比較簡單,但控制不夠靈活spring
2,使用Bulk Processor 特色是:使用稍麻煩,控制很是靈活
使用Bulk Processor處理也比較簡單,注意參數的設置,會影響索引的性能: BulkProcessor實例初始化以後,就能夠直接 遊標讀取添加就行數據庫
application.properties
json
#es數據庫配置
spring.elasticsearch.cluster.name=es1
spring.elasticsearch.address=000.000.000.xx
spring.elasticsearch.zen.discovery.port=9200
spring.elasticsearch.port:9200
複製代碼
配置類 ESConfig.java
bash
@Component
public class ESConfig {
public static final Map<String,BulkProcessor> bulkProcessorHashMap = Maps.newHashMap();
//ES數據庫
@Value("${spring.elasticsearch.cluster.name}")
private String esCluseterName;
@Value("${spring.elasticsearch.address}")
private String esAddress;
@Value("${spring.elasticsearch.zen.discovery.port}")
private String esZenPort;
public String getEsCluseterName() {
return esCluseterName;
}
public String getEsAddress() {
return esAddress;
}
public String getEsZenPort() {
return esZenPort;
}
public static String generateIndexId(){
SimpleDateFormat simpledateformat = new SimpleDateFormat("yyyyMMddhhmmssSSS");
String dateformat = simpledateformat.format(new Date());
String uuid = UUID.randomUUID().toString().replaceAll("-","");
String indexid =dateformat+uuid;
return indexid;
}
}
複製代碼
構造BulkProcesso併發
setBulkActions(1000):每添加1000個request,執行一次bulk操做 setBulkSize(new ByteSizeValue(5, ByteSizeUnit.MB)):每達到5M的請求size時,執行一次bulk操做 setFlushInterval(TimeValue.timeValueSeconds(10)):每10s執行一次bulk操做 setConcurrentRequests(1):默認是1,表示積累bulk requests和發送bulk是異步的,其數值表示發送bulk的併發線程數,設置爲0表示兩者同步的 setBackoffPolicy(BackoffPolicy.exponentialBackoff(TimeValue.timeValueMillis(10),app
ESClient.java
dom
@Component
public class ESClient {
private static ESClient es = null;
private static TransportClient client = null;
private ESClient() {
}
public static ESClient getEsClient() {
if (es == null) {
synchronized (ESClient.class) {
if (es == null) {
es = new ESClient();
if (client == null) {
ESConfig baseConfig = SpringUtil.getBean(ESConfig.class);
String port = baseConfig.getEsZenPort();
Settings settings = Settings.builder()
.put("cluster.name", baseConfig.getEsCluseterName())
.put("client.transport.sniff", true).build();
client = new PreBuiltTransportClient(settings);
try {
String address = baseConfig.getEsAddress();
if(address!=null&&address.length()>0){
String[] str = address.split(",");
for(String tmp:str){
client.addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName(tmp), new Integer(port)));
}
}
} catch (UnknownHostException e) {
e.printStackTrace();
}
}
}
}
}
return es;
}
public void closeClient(TransportClient client) {
if (client != null) {
client.close();
}
}
public TransportClient getTclient() {
return client;
}
/** 初始化批量配置屬性,符合其中一些條件,即會觸發批量操做*/
public BulkProcessor getBulkProcessor(String messsionID) {
BulkProcessor bulkProcessor = ESConfig.bulkProcessorHashMap.get(messsionID);
if(bulkProcessor==null){
bulkProcessor = BulkProcessor.builder(client, new BulkProcessor.Listener() {
public void beforeBulk(long l, BulkRequest bulkRequest) {
//發送請求前,能夠作一些事情
//logger.info("---嘗試插入{}條數據---", bulkRequest.numberOfActions());
}
public void afterBulk(long l, BulkRequest bulkRequest, BulkResponse bulkResponse) {
//發送請求失敗,能夠作一些事情
//logger.info("---嘗試插入{}條數據---", bulkRequest.numberOfActions());
}
public void afterBulk(long l, BulkRequest bulkRequest, Throwable throwable) {
//發送請求成功後,能夠作一些事情
//logger.info("---嘗試插入{}條數據---", bulkRequest.numberOfActions());
}
})
.setBulkActions(10000)// //達到批量1萬請求處理一次
.setBulkSize(new ByteSizeValue(2048, ByteSizeUnit.KB))// 達到2M批量處理一次
.setFlushInterval(TimeValue.timeValueSeconds(10))//設置flush索引週期
.setConcurrentRequests(2)//設置多少個併發處理線程
.build();////構建BulkProcessor
ESConfig.bulkProcessorHashMap.put(messsionID,bulkProcessor);
}
return bulkProcessor;
}
}
複製代碼
ESUtil異步
ESUtil.java
elasticsearch
public class ESUtil {
public static IndexRequest indexRequest(String index,String indextype,String indexid,String jsonstring){
IndexRequest rindex = null;
try {
if(indexid!=null&&indexid.length()>0) {
rindex = new IndexRequest(index, indextype,indexid).source(jsonstring, XContentType.JSON);
}else{
rindex = new IndexRequest(index, indextype).source(jsonstring, XContentType.JSON);
}
}catch (Exception e){
e.printStackTrace();
}
return rindex;
}
public static DeleteRequest indexRequest(String index, String indextype, String indexid){
DeleteRequest rindex = null;
try {
rindex = new DeleteRequest(index, indextype,indexid);
}catch (Exception e){
e.printStackTrace();
}
return rindex;
}
}
複製代碼
Test
Test.java
JSONObject jsonObject = new JSONObject();
jsonObject.put("aa", 1);
jsonObject.put("bb", 2);
//保存到es
BulkProcessor bulkProcessor = ESClient.getEsClient().getBulkProcessor("gwrec");
bulkProcessor.add(ESUtil.indexRequest("gwreg", "edb", esId0000000001, jsonStr));
//刪除es
bulkProcessor.add(ESUtil.deleteRequest("gwreg", "edb", esId0000000001));
bulkProcessor.close();
複製代碼