简体   繁体   English

Java客户端和Apache HttpClient连接到Druid

[英]Java client with Apache HttpClient to connect to Druid

I am working on ingesting and query data on Druid Server. 我正在Druid服务器上提取和查询数据。 But, when I query I just using the command line as below: 但是,当我查询时,我只使用如下命令行:

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

Can anyone tell me the way of utilizing Java client with Apache HttpClient to send that query to Druid server so as to get response. 谁能告诉我将Java客户端与Apache HttpClient结合使用以将该查询发送到Druid服务器以获得响应的方法。 Thanks so much. 非常感谢。

I have not tested this , but this should give you a fair idea of doing this 我尚未对此进行测试,但这应该可以使您对此有所了解

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

    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM