java 执行es中的sql

import org.apache.http.HttpHost;

import org.elasticsearch.client.Request;

import org.elasticsearch.client.Response;

import org.elasticsearch.client.RestClient;

import org.elasticsearch.client.RestHighLevelClient;

import java.io.*;

import java.nio.charset.StandardCharsets;

import java.util.HashSet;

public class ReadEsSql {

public static void main(String[] args) {

try (RestHighLevelClient client = new RestHighLevelClient(

RestClient.builder(

new HttpHost("host1", 9200, "http"),

new HttpHost("host2", 9200, "http"),

new HttpHost("host3", 9200, "http")

))) {

//写本地

BufferedWriter writer = new BufferedWriter(new FileWriter("data/result.txt", true));

String sql = String.format("{\"query\": \"select * from \\\"index\\\" \" } ,\"fetch_size\":100000");

Request request = new Request("POST", "/_sql?format=txt");

request.setJsonEntity(sql);

Response response = client.getLowLevelClient().performRequest(request);

// 正确处理响应实体

BufferedReader reader = new BufferedReader(new InputStreamReader(response.getEntity().getContent(), StandardCharsets.UTF_8));

String data;

HashSet<String> set = new HashSet<>();

while ((data = reader.readLine()) != null) {

set.add(data);

}

set.forEach(it -> {

try {

writer.write(it + "\n");

} catch (IOException e) {

throw new RuntimeException(e);

}

});

writer.flush();

} catch (IOException e) {

e.printStackTrace();

}

}

}

相关推荐
爱丽_4 分钟前
深入理解 Java Socket 编程与线程池:从阻塞 I/O 到高并发处理
java·开发语言
济南壹软网络科技有限公司7 分钟前
云脉IM的高性能消息路由与离线推送机制摘要:消息的“零丢失、低延迟”之道
java·即时通讯源码·开源im·企业im
Seven9713 分钟前
剑指offer-46、孩⼦们的游戏(圆圈中最后剩下的数)
java
serendipity_hky28 分钟前
互联网大厂Java面试故事:核心技术栈与场景化业务问题实战解析
java·spring boot·redis·elasticsearch·微服务·消息队列·内容社区
我真不会起名字啊28 分钟前
C、C++中的sprintf和stringstream的使用
java·c语言·c++
十点摆码29 分钟前
Spring Boot2 使用 Flyway 管理数据库版本
java·flyway·数据库脚本·springboo2·数据库脚本自动管理
毕设源码-钟学长1 小时前
【开题答辩全过程】以 基于Javaweb的电动汽车充电桩管理系统为例,包含答辩的问题和答案
java·spring boot
多敲代码防脱发1 小时前
为何引入Spring-cloud以及远程调用(RestTemplate)
java·开发语言
毕设源码-邱学长1 小时前
【开题答辩全过程】以 基于JavaWeb的家庭理财管理系统的设计与实现为例,包含答辩的问题和答案
java
sailing-data1 小时前
【SE】接口标准化
java·开发语言