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

}

}

}

相关推荐
程序员黑豆2 小时前
Java字符串拼接全解析:6种方式性能对比与实战指南
java·前端·ai编程
程序员黑豆8 小时前
Java字符串详解
java·前端·ai编程
EntyIU10 小时前
NFS离线部署
java
Csvn11 小时前
📊 SQL 入门 Day 18:索引原理
后端·sql
DBA小马哥11 小时前
关系型数据库核心概念手册:SQL、事务与存储引擎的技术脉络
数据库·sql
灯澜忆梦12 小时前
【MySQL12】进阶篇 | SQL优化
数据库·sql·mysql·性能优化
OH_TPC13 小时前
HarmonyOS APP开发---“滤镜大师“图像处理App,需要用到这个库
java·图像处理·华为·harmonyos·鸿蒙
imaol114 小时前
链表 -- 环链表
java·前端·链表
imaol114 小时前
链表 -- 双向链表
java·前端·链表
2601_9638699515 小时前
【计算机毕业设计】基于Java的相框定制系统的设计与实现
java·开发语言·课程设计