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

}

}

}

相关推荐
m0_651593919 分钟前
构建智能SKU系统:编码规则、元数据设计与DDD实战指南
java·大数据·数据库
乐之者v11 分钟前
统计数据时,sql执行超时,如何处理
服务器·数据库·sql
电商API&Tina12 分钟前
【京东item_getAPI 】高稳定:API 、非爬虫、不封号、不掉线、大促稳跑
大数据·网络·人工智能·爬虫·python·sql·json
crack_comet18 分钟前
Spring Boot 3.5.11 分离打包(无参数启动+Jar瘦身)完整配置文档
java·spring boot·后端·maven·intellij-idea·jar
xxjj998a21 分钟前
sql实战解析-sum()over(partition by xx order by xx)
数据库·sql
无极低码21 分钟前
Oracle 常用运维SQL整理,改字段类型改表名创建基础用户授权等
数据库·sql·oracle
weixin_4080996722 分钟前
身份证正反面合并+识别OCR接口调用
java·人工智能·后端·python·ocr·api·身份证ocr
流觞 无依24 分钟前
Linux下SQLite数据库空间管理 查看表占用空间+清理优化
java·jvm·oracle
光泽雨28 分钟前
查询语句select
数据库·sql·mysql
Java成神之路-34 分钟前
Spring 注解开发进阶实战:Bean 生命周期、 依赖注入及Properties配置(Spring系列4)
java·后端·spring