ElasticSearch 批量处理 API

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/zhang18024666607/article/details/88739473

es里面提供了两种批量建索引的方法:
1,使用 Bulk Api 特点是:使用比较简单,但控制不够灵活
2,使用Bulk Processor 特点是:使用稍麻烦,控制非常灵活
使用Bulk Processor处理也比较简单,注意参数的设置,会影响索引的性能: BulkProcessor实例初始化之后,就可以直接
游标读取添加就行

application.properties

#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

@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),

ESClient.java

@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

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();

猜你喜欢

转载自blog.csdn.net/zhang18024666607/article/details/88739473