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

}

}

}

相关推荐
tmacfrank31 分钟前
网络编程中的直接内存与零拷贝
java·linux·网络
weixin_472339461 小时前
Maven 下载安装与配置教程
java·maven
Magnum Lehar2 小时前
3d游戏引擎EngineTest的系统实现3
java·开发语言·游戏引擎
就叫飞六吧3 小时前
Spring Security 集成指南:避免 CORS 跨域问题
java·后端·spring
Mcworld8573 小时前
java集合
java·开发语言·windows
天黑请闭眼3 小时前
IDEA:程序编译报错:java: Compilation failed: internal java compiler error
java·intellij-idea
苍煜4 小时前
Maven构建流程详解:如何正确管理微服务间的依赖关系-当依赖的模块更新后,我应该如何重新构建主项目
java·微服务·maven
冼紫菜4 小时前
[特殊字符]CentOS 7.6 安装 JDK 11(适配国内服务器环境)
java·linux·服务器·后端·centos
isyangli_blog4 小时前
(1-4)Java Object类、Final、注解、设计模式、抽象类、接口、内部类
java·开发语言
秋野酱5 小时前
Spring Boot 项目的计算机专业论文参考文献
java·spring boot·后端