es增删改查

 

es增删改查

es增删改查

  /**
     * 存储ES数据  可用
     */
    @Test
    public void saveDataToEs(UploadInfo uploadInfo) {
        // 指定ES集群
        Settings settings = Settings.builder().put("cluster.name","jtcn-es").build();
        System.out.println(settings);

        TransportClient client = null;
        XContentBuilder doc = null;
        try {
            if (uploadInfo != null && uploadInfo.getData() != null) {
                // 创建ES客户端,客户端端口号默认9300,不要写web端口9200
                client = new PreBuiltTransportClient(settings)
                        .addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("34.115.11.11"), Integer.parseInt("9200")));

                String[] array = uploadInfo.getData().split(',');

                // 构建文档数据
                doc = XContentFactory.jsonBuilder().startObject()
                        .field("id", UUID.randomUUID())
                        .field("gpsNbr", array[1])
                        .field("gpsInfoType", 1)
                        .field("policeId", array[2])
                        .field("policeType", array[3])
                        .field("lng", array[4])
                        .field("lat", array[5])
                        .array("localtion", array[4], array[5])
                        .field("updateTime", new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()))
                        .field("speed", 0)
                        .field("publishTime", new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()))
                        .field("orgCode", array[10])
                        .field("isContain", 0)
                        .field("distance", 0)
                        .field("updateMin", System.currentTimeMillis()).endObject();
                String nowDate = DateUtil.formatDate(DateUtil.DATE_FORMAT_YYYYMMDD);
                // 添加文档
                IndexResponse response = client.prepareIndex("tongling_police_gps_data_"+nowDate, "info", UUID.randomUUID().toString()).setSource(doc).get();
//+nowDate
                // 查看执行状态,创建成功后,之前没有CREATED,之前存在OK
                System.out.println("response.status():" + response.status());
            }
        } catch (Exception ex) {
            System.out.print("保存数据到es发生异常: " + ex + "\n");
        }
        client.close();

    }

  

 

 /**
     * 从es中获取数据  可用
     *
     * @throws
     */
    @Test
    public void queryEs() {
        // 指定ES集群
        Settings settings = Settings.builder().put("cluster.name", "jtcn-es").build();
        System.out.println(settings);
        // 创建ES客户端,客户端端口号默认9300,不要写web端口9200
        TransportClient client = null;
        try {
            client = new PreBuiltTransportClient(settings)
                    .addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("34.115.121.46"), 9300));
        } catch (Exception ex) {
            System.out.print("Consumer Kafka have an ERROR: " + ex + "\n");
        }

        // 数据查询
        GetResponse response = client.prepareGet("tonglingIndex", "user", "100").execute().actionGet();

        System.out.println("response:" + response);
        System.out.println("response.getSourceAsString():" + response.getSourceAsString());

        client.close();

    }

  

posted on 2020-09-03 11:25  shenhaha  阅读(374)  评论(0编辑  收藏  举报

有用的话麻烦点个推荐哈