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

}

}

}

相关推荐
Lansonli1 小时前
大数据Spark(八十八):Spark任务资源之Standalone集群测试
java·服务器·spark
budingxiaomoli1 小时前
Spring AOP
java·spring·java-ee
特立独行的猫A1 小时前
Ionic + 仓颉语言的 cj-tauri 开发框架实战:开发精美移动端 APP
java·后端·python
nice_lcj5202 小时前
LRU 缓存入门:它是什么、怎么用、面试怎么手写
java·数据结构·缓存·面试·lru
Elasticsearch2 小时前
过犹不及:为什么搜索结果重排序时,20 个候选结果胜过 200 个
elasticsearch
文丑颜不良啊2 小时前
LeetCode2741~2760题目解析
java·leetcode
wno7043 小时前
常用的Git命令清单
大数据·git·elasticsearch
星恒随风12 小时前
MySQL 从零入门(五):MySQL 内置函数详解——日期、字符串、数学函数与实战案例
linux·数据库·笔记·sql·mysql
金銀銅鐵12 小时前
带您看懂字节码指令(二):空循环里有什么
java·后端·ai编程
吴名氏.14 小时前
Java转AI Agent教程(第十三周)混合检索与重排
java·数据库·人工智能·ai·ai agent