Running druid Query through java

Hi, I am trying to execute druid queries using java.So, far I am able to write the following query.But, i am not able to create a connection with druid.

How can i create a connection and fire the query?

dependencies used

io.druid

druid-processing

0.8.2

io.druid

druid-api

0.3.15

package com.analytics.druid;

import io.druid.jackson.DefaultObjectMapper;

import io.druid.query.Query;

import io.druid.query.aggregation.AggregatorFactory;

import io.druid.query.aggregation.LongSumAggregatorFactory;

import io.druid.query.topn.TopNQuery;

import io.druid.query.topn.TopNQueryBuilder;

import java.io.IOException;

import com.fasterxml.jackson.databind.ObjectMapper;

import com.google.common.collect.Lists;

public class DruidConnection {

@SuppressWarnings(“rawtypes”)

public static void main(String args) throws IOException {

ObjectMapper jsonMapper = new DefaultObjectMapper();

TopNQuery query = new TopNQueryBuilder()

.dataSource(“test”)

.dimension(“query”)

.metric(“volume”)

.threshold(10)

.aggregators(

Lists. newArrayList(Lists

.newArrayList(new LongSumAggregatorFactory(

“volume”, “count”))))

.intervals(“2014-01-01/2015-12-31”).build();

String json = jsonMapper.writeValueAsString(query);

Query serdeQuery = jsonMapper.readValue(json, Query.class);

}

}

On checking the serdeQuery it looks like this

TopNQuery {

dataSource = ‘test’, dimensionSpec = DefaultDimensionSpec {

dimension = ‘query’, outputName = ‘query’

}, topNMetricSpec = NumericTopNMetricSpec {

metric = ‘volume’

}, threshold = 10, querySegmentSpec = LegacySegmentSpec {

intervals = [2014 - 01 - 01 T00: 00: 00.000 + 05: 30 / 2015 - 12 - 31 T00: 00: 00.000 + 05: 30]

}, dimFilter = null, granularity = ‘AllGranularity’, aggregatorSpecs = [LongSumAggregatorFactory {

fieldName = ‘count’, name = ‘volume’

}], postAggregatorSpecs =

}

Which looks correct. I just need to know how to fire it on druid.

ps : i am able to query druid using python.

io.druid.client.DirectDruidClient is the closest thing in the druid codebase.

Druid native query language is JSON over HTTP. The directDruidclient is what is used to fire queries from brokers to compute (historical/realtime) servers. You can also look into writing one of your own if that doesn’t work. You can also look at using Druids.java for creating queries.