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

}

}

}

相关推荐
短剑重铸之日14 分钟前
《ShardingSphere解读》07 读写分离:如何集成分库分表+数据库主从架构?
java·数据库·后端·架构·shardingsphere·分库分表
知我Deja_Vu14 分钟前
【避坑指南】ConcurrentHashMap 并发计数优化实战
java·开发语言·python
daidaidaiyu1 小时前
Spring IOC 源码学习 事务相关的 BeanDefinition 解析过程 (XML)
java·spring
麦聪聊数据2 小时前
QuickAPI 在系统数据 API 化中的架构选型与集成
数据库·sql·低代码·微服务·架构
鬼蛟2 小时前
Spring————事务
android·java·spring
西门吹-禅3 小时前
【sap fiori cds up error】
java·服务器·sap cap cds
敲代码的嘎仔3 小时前
Java后端面试——SSM框架面试题
java·面试·职场和发展·mybatis·ssm·springboot·八股
大傻^3 小时前
Spring AI Alibaba RAG实战:基于向量存储的检索增强生成
java·人工智能·spring
大傻^3 小时前
Spring AI Alibaba 快速入门:基于通义千问的AI应用开发环境搭建
java·人工智能·后端·spring·springai·springaialibaba
伯恩bourne4 小时前
Google Guava:Java 核心工具库的卓越之选
java·开发语言·guava