gpt4 book ai didi

带有 Apache HttpClient 的 Java 客户端连接到 Druid

转载 作者:可可西里 更新时间:2023-11-01 17:06:30 26 4
gpt4 key购买 nike

我正致力于在 Druid 服务器上摄取和查询数据。但是,当我查询时,我只使用如下命令行:

curl -X 'POST' -H 'Content-Type:application/json' -d @quickstart/ingest_statistic_hourly_generate.json localhost:8090/druid/indexer/v1/task

谁能告诉我如何使用带有 Apache HttpClient 的 Java 客户端将该查询发送到 Druid 服务器以获得响应。非常感谢。

最佳答案

我还没有测试过这个,但是这应该让你对这样做有一个很好的了解

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.nio.file.Files;
import java.nio.file.Paths;

import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.HttpClientBuilder;

public class HTTPTestClient {

public static void main(String[] args) throws Exception {
String url = "http://localhost:8090/druid/indexer/v1/task";
String content = new String(Files.readAllBytes(Paths.get("quickstart/ingest_statistic_hourly_generate.json")));

HttpClient client = HttpClientBuilder.create().build();

HttpPost post = new HttpPost(url);

post.addHeader("Accept", "application/json");
post.addHeader("charset", "UTF-8");
post.addHeader("Content-Type", "application/json");
post.setEntity(new StringEntity(content));

HttpResponse response = client.execute(post);
System.out.println(response.getStatusLine());
System.out.println("Response Code : " + response);

BufferedReader rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));

StringBuffer result = new StringBuffer();
String line = "";
while ((line = rd.readLine()) != null) {
result.append(line);
}
System.out.println(result);

}
}

关于带有 Apache HttpClient 的 Java 客户端连接到 Druid,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39462238/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com