• java批量添加数据到elasticsearch


    ship对象类

    public class Ship {
        private String mmsi;
        private String utc;
       
    
    
        public String getMmsi() {
            return mmsi;
        }
    
        public void setMmsi(String mmsi) {
            this.mmsi = mmsi;
        }
    
        public String getUtc() {
            return utc;
        }
    
        public void setUtc(String utc) {
            this.utc = utc;
        }
    }

    批量添加

    public static void insertAllBulk(List<Ship> dataList,String indexName) {
            TransportClient client = null;
            try {
                client = connectionPool.getConnection();
                BulkRequestBuilder builder = client.prepareBulk();
                //请求命令数量
                int z = 0;
                long l2 = System.currentTimeMillis();
                int n = dataList.size();
                if (n != 0) {
                    //遍历列表,每条数据生成一条请求命令
                    for (int i = 0; i < n; i++) {
                        Ship data = dataList.remove(0);
                        if (data != null) {
                            String utc = data.getUtc();
                            if (utc == null || utc.equals("")) {
                                continue;
                            } else {
                                IndexRequestBuilder request = client.prepareIndex(indexName, "_doc").setSource(
                                        XContentFactory.jsonBuilder()
                                                .startObject()
                                                .field("mmsi", data.getMmsi())
                                                .field("utc", utc)
                                                .endObject()
                                );
                                //添加到批量请求
                                builder.add(request);
                                z++;
                            }
                        }
                    }
                    //命令数不为零
                    if (z != 0) {
                        long l3 = System.currentTimeMillis();
                        logger2.info("生成请求耗时:" + (l3 - l2));
                        //执行批量请求,获取结果
                        BulkResponse bulkItemResponses = builder.get();
                        if (bulkItemResponses.hasFailures()) {
                            logger2.error(bulkItemResponses.buildFailureMessage());
                        }
                        long l4 = System.currentTimeMillis();
                        logger2.info("执行并返回结果耗时:" + (l4 - l3));
                        long millis = bulkItemResponses.getTook().getMillis();
                        dataList.clear();
                        logger2.info("船完成批量导入ES" + n + "个,耗时" + millis);
                    }
    
                }
            } catch (Exception e) {
                e.printStackTrace();
                logger2.error("insertBulk 错误", e);
            } finally {
                connectionPool.releaseConnection(client);
            }
        }
  • 相关阅读:
    传奇检测命令大全(常用命令)
    传奇版本中利用NPC迅速给人物加血脚本制作
    传奇泡点地图制作脚本
    双击包裹物品自动解包设置方法_传奇版本技术
    传奇地图事件触发脚本
    单机架设传奇服务器第47课:定时器OnTimer功能详解
    单机架设传奇服务器:机器人运行脚本文件
    检测某个地图某个怪物的数量的脚本
    检测当前人物是否在安全区脚本命令
    假人配置说明
  • 原文地址:https://www.cnblogs.com/asksk/p/15480841.html
Copyright © 2020-2023  润新知