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 小时前
spring-ai 第四多模态API
java·人工智能·spring
她说..7 小时前
Java 对象相关高频面试题
java·开发语言·spring·java-ee
庞轩px7 小时前
深入理解 sleep() 与 wait():从基础到监视器队列
java·开发语言·线程··wait·sleep·监视器
皮皮林5518 小时前
面试官:ZSet 的底层实现是什么?
java
码云数智-大飞8 小时前
C++ RAII机制:资源管理的“自动化”哲学
java·服务器·php
2601_949816588 小时前
Spring+Quartz实现定时任务的配置方法
java
计算机毕设指导69 小时前
基于SpringBoot校园学生健康监测管理系统【源码文末联系】
java·spring boot·后端·spring·tomcat·maven·intellij-idea
RInk7oBjo10 小时前
spring-事务管理
数据库·sql·spring
Elastic 中国社区官方博客10 小时前
通过自主 IT 平台和 Elastic 迈出可观测性的下一步
大数据·elasticsearch·搜索引擎·全文检索·可用性测试
mysuking10 小时前
springboot与springcloud对应版本
java·spring boot·spring cloud