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