【问题标题】:Java client with Apache HttpClient to connect to Druid使用 Apache HttpClient 连接 Druid 的 Java 客户端
【发布时间】:2017-01-20 14:17:02
【问题描述】:

我正在 Druid Server 上提取和查询数据。但是,当我查询时,我只是使用如下命令行:

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 服务器以获得响应。非常感谢。

【问题讨论】:

    标签: java apache http druid


    【解决方案1】:

    我没有对此进行测试,但这应该让您对这样做有一个公平的想法

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

    【讨论】:

    • 我在上面运行了你的代码,但它有一个错误:线程“main”中的异常 java.lang.NoSuchFieldError: INSTANCE at org.apache.http.conn.ssl.SSLConnectionSocketFactory.( SSLConnectionSocketFactory.java:144) 在 org.apache.http.impl.client.HttpClientBuilder.build(HttpClientBuilder.java:955) 在 java 行 HttpClient client = HttpClientBuilder.create().build();
    • 我使用了最新的 Apache HTTP 客户端库。对于您的错误,请参考stackoverflow.com/questions/22330848/…
    • 您是使用 Httpclient 还是同时使用 Httpclient 和 Httpcore ?我刚刚使用了Httpclient
    • 谢谢,它运作良好。在我的项目中,pom.xml 文件中的 apache tika 可能与其他依赖项有关。因此,我创建了新项目并且效果很好。考虑
    猜你喜欢
    • 2014-02-10
    • 1970-01-01
    • 2020-07-07
    • 2020-09-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-02-08
    • 2017-11-22
    相关资源
    最近更新 更多