VanThaoNguyen VanThaoNguyen - 1 year ago 211
HTTP Question

Java client with Apache HttpClient to connect to Druid

I am working on ingesting and query data on Druid Server. 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. Thanks so much.

Answer Source

I have not tested this , but this should give you a fair idea of doing this

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 Code : " + response);

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

        StringBuffer result = new StringBuffer();
        String line = "";
        while ((line = rd.readLine()) != null) {

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download