1

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.

VanThaoNguyen
  • 792
  • 9
  • 20

1 Answers1

3

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

    }
}
Ramachandran.A.G
  • 4,788
  • 1
  • 12
  • 24
  • I run your code above, but it have an error: Exception in thread "main" java.lang.NoSuchFieldError: INSTANCE at org.apache.http.conn.ssl.SSLConnectionSocketFactory.(SSLConnectionSocketFactory.java:144) at org.apache.http.impl.client.HttpClientBuilder.build(HttpClientBuilder.java:955) in java line HttpClient client = HttpClientBuilder.create().build(); – VanThaoNguyen Sep 13 '16 at 05:39
  • I used the latest libraries of Apache HTTP Client. For your error please refer https://stackoverflow.com/questions/22330848/httpclient-example-exception-in-thread-main-java-lang-nosuchfielderror-inst – Ramachandran.A.G Sep 13 '16 at 05:44
  • Did You use Httpclient or both of Httpclient and Httpcore ? I just used Httpclient – VanThaoNguyen Sep 13 '16 at 06:44
  • Thanks, It work well. I my project, The apache tika in pom.xml file may be confilg with others depedency. Thus, I created new project and it works well. regard – VanThaoNguyen Sep 13 '16 at 07:02