apache / druid

Apache Druid: a high performance real-time analytics database.
https://druid.apache.org/
Apache License 2.0
13.43k stars 3.69k forks source link

Java client for Apache Druid #14338

Closed iamtomkeen closed 2 months ago

iamtomkeen commented 1 year ago

Apparently druidry is not maintained anymore.

Can anyone advise the best common approach to querying Druid from Java application at the moment?

aleksi75 commented 1 year ago

At first I would recommend to use 'Druid SQL' and not 'Native queries', but in both cases you just send JSON to a REST endpoint (Broker).

Following a simple example with an 'org.eclipse.jetty.client.HttpClient'

public String callDruid() throws Exception {
    HttpClient httpClient = new HttpClient();
    httpClient.start();

    String result = "{}";   // emtpy JSON as default

    try {
        final String body = "{ \"query\" : \"SELECT * from your_datasource\ LIMIT 10" }";   // SQL Query  as JSON
        final String url = DRUID_BROKER_URL_INCL_PORT + "/druid/v2/sql";
        final Request request = httpClient.newRequest(url);

        request.timeout(60000, TimeUnit.MILLISECONDS);
        request.method(HttpMethod.POST);
        request.content(new BytesContentProvider(body.getBytes()), MediaType.APPLICATION_JSON_VALUE);
        request.header(HttpHeader.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE);

        final ContentResponse response = request.send();

        if (HttpStatus.OK_200 == response.getStatus()) {
            result = response.getContentAsString();
        } else {
            LOGGER.warn("callDruid - query received unexpected response status " + response.getStatus() + " and content " + response.getContentAsString());
        }
    } catch (final InterruptedException | TimeoutException | ExecutionException e) {
        LOGGER.error("callDruid - query got exception processing call: " + e.getMessage(), e);
    }

    httpClient.stop();

    return result;
}

Note: This is not an issue, for questions like this better use Google Druid User Group.

github-actions[bot] commented 3 months ago

This issue has been marked as stale due to 280 days of inactivity. It will be closed in 4 weeks if no further activity occurs. If this issue is still relevant, please simply write any comment. Even if closed, you can still revive the issue at any time or discuss it on the dev@druid.apache.org list. Thank you for your contributions.

github-actions[bot] commented 2 months ago

This issue has been closed due to lack of activity. If you think that is incorrect, or the issue requires additional review, you can revive the issue at any time.