Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DruidJerseyClient error #89

Closed
Lswks opened this issue Feb 26, 2019 · 8 comments
Closed

DruidJerseyClient error #89

Lswks opened this issue Feb 26, 2019 · 8 comments

Comments

@Lswks
Copy link

Lswks commented Feb 26, 2019

Hi:
I use druidry like this:

        DruidTimeSeriesQuery query = DruidTimeSeriesQuery.builder()
                .dataSource("druid_test_2")
                .granularity(granularity)
                .intervals(Collections.singletonList(interval))
                .descending(true)
                .filter(filter)
                .aggregators(Collections.singletonList(aggregator1))
                .intervals(Collections.singletonList(interval))
                .build();
        ObjectMapper mapper = new ObjectMapper();
        String requiredJson = mapper.writeValueAsString(query);
        DruidConfiguration config = DruidConfiguration
                .builder()
                .protocol(DruidQueryProtocol.HTTP)
                .host("my druid host")
                .port(8082)
                .endpoint("druid/v2/")
                .concurrentConnectionsRequired(5)
                .build();
        DruidClient client = new DruidJerseyClient(config);
        client.connect();
        List<DruidResponse> responses = client.query(query,DruidResponse.class);

and I get the follow error:

{"error":"Unknown exception","errorMessage":"Could not resolve type id 'TIMESERIES' into a subtype of [simple type, class org.apache.druid.query.Query]: known type ids = [Query, dataSourceMetadata, groupBy, scan, search, segmentMetadata, select, timeBoundary, timeseries, topN]\n at [Source: HttpInputOverHTTP@167674ce[c=1242,q=0,[0]=null,s=STREAM]; line: 1, column: 1217]","errorClass":"com.fasterxml.jackson.databind.JsonMappingException","host":null}

@GG-Zapr
Copy link
Member

GG-Zapr commented Feb 26, 2019

Could you try sending query Object instead of parsing as a string and sending it?
Druidry takes cares of it

`DruidTimeSeriesQuery query = DruidTimeSeriesQuery.builder()
.dataSource("druid_test_2")
.granularity(granularity)
.intervals(Collections.singletonList(interval))
.descending(true)
.filter(filter)
.aggregators(Collections.singletonList(aggregator1))
.intervals(Collections.singletonList(interval))
.build();

    DruidConfiguration config = DruidConfiguration
            .builder()
            .protocol(DruidQueryProtocol.HTTP)
            .host("my druid host")
            .port(8082)
            .endpoint("druid/v2/")
            .concurrentConnectionsRequired(5)
            .build();
    DruidClient client = new DruidJerseyClient(config);
    client.connect();
    List<DruidResponse> responses = client.query(query,DruidResponse.class);`

@Lswks
Copy link
Author

Lswks commented Feb 27, 2019

yes,I trying to parsing as a string and sending it like this

ObjectMapper mapper = new ObjectMapper();
        String requiredJson = mapper.writeValueAsString(query);
        String s = HttpUtil.post("http://host:8082/druid/v2/", requiredJson);

and it's work.

but why this way did not work ?

client.query(query,DruidResponse.class)

@ShawyerPeng
Copy link

ShawyerPeng commented Mar 13, 2019

@Lswks @GG-Zapr I've got the same problem when avoking client.query() method, and it threw a exception(500 Internal Server Error), could you please share your solution to this problem if you fixed it?

My code is as follows:

public class DruidSimpleQuery {
    private DruidConfiguration druidConfiguration;
    private DruidClient druidClient;

    @Before
    public void init() {
        druidConfiguration = DruidConfiguration
                .builder()
                .protocol(DruidQueryProtocol.HTTP)
                .host(DruidConfig.HOST)
                .port(DruidConfig.PORT)
                .endpoint("druid/v2/")
                .concurrentConnectionsRequired(8)
                .build();

        druidClient = new DruidJerseyClient(druidConfiguration);
    }

    @Test
    public void topNQuery() throws Exception {
        DateTime startTime = new DateTime(2015, 9, 12, 0, 0, 0, DateTimeZone.UTC);
        DateTime endTime = new DateTime(2015, 9, 13, 0, 0, 0, DateTimeZone.UTC);
        Interval interval = new Interval(startTime, endTime);

        Granularity granularity = new SimpleGranularity(PredefinedGranularity.ALL);

        DruidAggregator aggregator1 = new CountAggregator("count");
        DruidDimension dimension = new SimpleDimension("page");
        TopNMetric metric = new SimpleMetric("count");

        DruidTopNQuery query = DruidTopNQuery.builder()
                .dataSource("wikipedia")
                .dimension(dimension)
                .threshold(10)
                .topNMetric(metric)
                .granularity(granularity)
                .aggregators(Collections.singletonList(aggregator1))
                .intervals(Collections.singletonList(interval))
                .build();

        ObjectMapper mapper = new ObjectMapper();
        String requiredJson = mapper.writeValueAsString(query);
        System.out.println("Json query: " + requiredJson);

        try {
            this.druidClient.connect();
            String result = this.druidClient.query(query);
            System.out.println("Query result: " + result);
        } catch (ConnectionException e) {
            e.printStackTrace();
        } catch (QueryException e) {
            e.printStackTrace();
        } finally {
            try {
                this.druidClient.close();
            } catch (ConnectionException e) {
                e.printStackTrace();
            }
        }
    }
}

@abhi-zapr
Copy link
Contributor

@ShawyerPeng Your code worked fine for me, not able to reproduce the issue. I used the latest release. @Lswks I also tried Timeseries Query, that also worked.

Attaching screenshots for reference.

  • TopN Query

Screenshot 2019-03-17 at 12 43 44 AM

  • Timeseries Query

Screenshot 2019-03-17 at 12 49 32 AM

  • Code :
public class DruidSimpleQuery {
    private DruidConfiguration druidConfiguration;
    private DruidClient druidClient;

    @BeforeClass
    public void init() {
        druidConfiguration = DruidConfiguration
                .builder()
                .protocol(DruidQueryProtocol.HTTP)
                .host("localhost")
                .port(8082)
                .endpoint("druid/v2/")
                .concurrentConnectionsRequired(8)
                .build();

        druidClient = new DruidJerseyClient(druidConfiguration);
    }

    @Test
    public void topNQuery() throws Exception {
        DateTime startTime = new DateTime(2015, 9, 12, 0, 0, 0, DateTimeZone.UTC);
        DateTime endTime = new DateTime(2015, 9, 13, 0, 0, 0, DateTimeZone.UTC);
        Interval interval = new Interval(startTime, endTime);

        Granularity granularity = new SimpleGranularity(PredefinedGranularity.ALL);

        DruidAggregator aggregator1 = new CountAggregator("count");
        DruidDimension dimension = new SimpleDimension("page");
        TopNMetric metric = new SimpleMetric("count");

        DruidTopNQuery query = DruidTopNQuery.builder()
                .dataSource("wikipedia")
                .dimension(dimension)
                .threshold(10)
                .topNMetric(metric)
                .granularity(granularity)
                .aggregators(Collections.singletonList(aggregator1))
                .intervals(Collections.singletonList(interval))
                .build();

        ObjectMapper mapper = new ObjectMapper();
        String requiredJson = mapper.writeValueAsString(query);
        System.out.println("Json query: " + requiredJson);

        try {
            this.druidClient.connect();
            String result = this.druidClient.query(query);
            System.out.println("Query result: " + result);
        } catch (ConnectionException e) {
            e.printStackTrace();
        } catch (QueryException e) {
            e.printStackTrace();
        } finally {
            try {
                this.druidClient.close();
            } catch (ConnectionException e) {
                e.printStackTrace();
            }
        }
    }

    @Test
    public void timeserisQuery() throws Exception {
        DateTime startTime = new DateTime(2015, 9, 12, 0, 0, 0, DateTimeZone.UTC);
        DateTime endTime = new DateTime(2015, 9, 13, 0, 0, 0, DateTimeZone.UTC);
        Interval interval = new Interval(startTime, endTime);

        Granularity granularity = new SimpleGranularity(PredefinedGranularity.ALL);

        DruidAggregator aggregator1 = new CountAggregator("count");
        DruidDimension dimension = new SimpleDimension("page");
        TopNMetric metric = new SimpleMetric("count");

        DruidTimeSeriesQuery query = DruidTimeSeriesQuery
                .builder()
                .dataSource("wikipedia")
                .granularity(granularity)
                .aggregators(Collections.singletonList(aggregator1))
                .intervals(Collections.singletonList(interval))
                .build();

        ObjectMapper mapper = new ObjectMapper();
        String requiredJson = mapper.writeValueAsString(query);
        System.out.println("Json query: " + requiredJson);

        try {
            this.druidClient.connect();
            String result = this.druidClient.query(query);
            System.out.println("Query result: " + result);
        } catch (ConnectionException e) {
            e.printStackTrace();
        } catch (QueryException e) {
            e.printStackTrace();
        } finally {
            try {
                this.druidClient.close();
            } catch (ConnectionException e) {
                e.printStackTrace();
            }
        }
    }
}

@ShawyerPeng
Copy link

@abhi-zapr Thanks for replying, but I got the same error again. (By the way, I use the latest version 2.13)

Request Body:

"{"aggregations":[{"name":"count","type":"count"}],"dataSource":"wikipedia","granularity":{"granularity":"ALL"},"intervals":[{"endTime":"2015-09-13T00:00:00.000Z","startTime":"2015-09-12T00:00:00.000Z"}],"queryType":"TIMESERIES"}[\r][\n]"

Error Message:

"{"error":"Unknown exception","errorMessage":"Could not resolve type id 'TIMESERIES' into a subtype of [simple type, class org.apache.druid.query.Query]: known type ids = [Query, dataSourceMetadata, groupBy, scan, search, segmentMetadata, select, timeBoundary, timeseries, topN]\n at [Source: HttpInputOverHTTP@54996c8[c=228,q=0,[0]=null,s=STREAM]; line: 1, column: 203]","errorClass":"com.fasterxml.jackson.databind.JsonMappingException","host":null}"

It seems that queryType property is case sensitive, "TIMESERIES" should be replaced by "timeseries".

@abhi-zapr
Copy link
Contributor

In latest release you can see here we are setting QueryType as QueryType.TIMESERIES and this value of enum you can verify from here is timeseries. So query generated by druidry should have timeseries as queryType.

Can you share code thorough which you generated Request Body in above comment ? Because as per my code in this comment, it generates correctly which you can verify in screenshots.

@mir0y
Copy link

mir0y commented Apr 3, 2019

I met the problem too and found it was because that QueryType enum uses a Jackson annotation JsonValue while in my application jersey used FastJson as provider.
To solve this, I register Jackson in jersey client.

ClientConfig clientConfig = new ClientConfig();
clientConfig.register(JacksonFeature.class);
DruidJerseyClient client = new DruidJerseyClient(config, clientConfig);

Use string in place of enum may be better to avoid the Json serialization problem of Enum.

@abhi-zapr
Copy link
Contributor

I met the problem too and found it was because that QueryType enum uses a Jackson annotation JsonValue while in my application jersey used FastJson as provider.
To solve this, I register Jackson in jersey client.

ClientConfig clientConfig = new ClientConfig();
clientConfig.register(JacksonFeature.class);
DruidJerseyClient client = new DruidJerseyClient(config, clientConfig);

Use string in place of enum may be better to avoid the Json serialization problem of Enum.

Nice catch. Pinning this issue for future references.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants