|
1 | 1 | package com.simplaex.clients.druid;
|
2 | 2 |
|
3 |
| - |
4 | 3 | import io.druid.query.Druids;
|
5 | 4 | import io.druid.query.Result;
|
| 5 | +import io.druid.query.filter.DimFilters; |
6 | 6 | import io.druid.query.metadata.metadata.AllColumnIncluderator;
|
7 | 7 | import io.druid.query.metadata.metadata.SegmentAnalysis;
|
8 | 8 | import io.druid.query.metadata.metadata.SegmentMetadataQuery;
|
| 9 | +import io.druid.query.scan.ScanQuery; |
| 10 | +import io.druid.query.scan.ScanResultValue; |
9 | 11 | import io.druid.query.select.EventHolder;
|
10 | 12 | import io.druid.query.select.PagingSpec;
|
11 | 13 | import io.druid.query.select.SelectQuery;
|
|
15 | 17 | import org.joda.time.Interval;
|
16 | 18 | import org.junit.*;
|
17 | 19 | import org.mockserver.integration.ClientAndServer;
|
| 20 | +import org.mockserver.matchers.JsonStringMatcher; |
| 21 | +import org.mockserver.matchers.MatchType; |
| 22 | +import org.mockserver.matchers.Matcher; |
18 | 23 | import org.mockserver.model.HttpRequest;
|
19 | 24 | import org.mockserver.model.HttpResponse;
|
20 | 25 |
|
@@ -138,7 +143,8 @@ public void shouldExecuteASelectQuery() throws IOException {
|
138 | 143 | final SelectQuery query = new Druids.SelectQueryBuilder()
|
139 | 144 | .dataSource("player_explorer_s3")
|
140 | 145 | .dimensions(Collections.singletonList("deviceType"))
|
141 |
| - .intervals(new MultipleIntervalSegmentSpec(Collections.singletonList(new Interval(from, to)))) |
| 146 | + .intervals(new MultipleIntervalSegmentSpec( |
| 147 | + Collections.singletonList(new Interval(from, to)))) |
142 | 148 | .pagingSpec(new PagingSpec(Collections.emptyMap(), 100))
|
143 | 149 | .build();
|
144 | 150 |
|
@@ -169,4 +175,52 @@ public void shouldExecuteASelectQuery() throws IOException {
|
169 | 175 | );
|
170 | 176 | }
|
171 | 177 |
|
| 178 | + @Test |
| 179 | + public void shouldExecuteAScanQuery() throws IOException { |
| 180 | + |
| 181 | + setResponse(load("scan_query_response.json")); |
| 182 | + |
| 183 | + final DruidClient client = |
| 184 | + DruidClient.create("localhost", mockServer.getPort()); |
| 185 | + |
| 186 | + final long from = Instant.parse("2017-06-30T00:00:00Z").toEpochMilli(); |
| 187 | + final long to = Instant.parse("2020-10-02T00:00:00Z").toEpochMilli(); |
| 188 | + |
| 189 | + final ScanQuery query = new ScanQuery.ScanQueryBuilder() |
| 190 | + .dataSource("player_explorer_s3") |
| 191 | + .filters(DimFilters.dimEquals("version", "All")) |
| 192 | + .resultFormat("compactList") |
| 193 | + .columns(Arrays.asList("__time", "sessionCount", "deviceType")) |
| 194 | + .intervals(new MultipleIntervalSegmentSpec( |
| 195 | + Collections.singletonList(new Interval(from, to)))) |
| 196 | + .build(); |
| 197 | + |
| 198 | + final DruidResult<ScanResultValue> result = client.run(query); |
| 199 | + |
| 200 | + // The http request body isn't marked as json, so can't use automatic json body matcher. |
| 201 | + HttpRequest[] requests = mockServer.retrieveRecordedRequests(HttpRequest.request()); |
| 202 | + Assert.assertEquals("Single request should be recorded", 1, requests.length); |
| 203 | + Matcher<String> jsonMatcher = new JsonStringMatcher(load("scan_query.json"), MatchType.ONLY_MATCHING_FIELDS); |
| 204 | + String requestedJson = (String)requests[0].getBody().getValue(); |
| 205 | + Assert.assertTrue( |
| 206 | + String.format("expected request json should match %s, but was %s", jsonMatcher, requestedJson), |
| 207 | + jsonMatcher.matches(requestedJson)); |
| 208 | + |
| 209 | + final List<ScanResultValue> resultList = result.toList(); |
| 210 | + |
| 211 | + Assert.assertEquals("Number of results should be 11", 11, resultList.size()); |
| 212 | + ScanResultValue scanResult = resultList.get(10); |
| 213 | + |
| 214 | + Assert.assertEquals("should have 3 columns", 3, scanResult.getColumns().size()); |
| 215 | + Assert.assertEquals( |
| 216 | + "second columns should be sessionCount", |
| 217 | + "sessionCount", |
| 218 | + scanResult.getColumns().get(1)); |
| 219 | + |
| 220 | + final List<List<Object>> events = (List<List<Object>>) scanResult.getEvents(); |
| 221 | + Assert.assertEquals("should have 2 events", 2, events.size()); |
| 222 | + |
| 223 | + final List<Object> values = events.get(1); |
| 224 | + Assert.assertEquals("the last event should have a sessionCount of 25", 25, values.get(1)); |
| 225 | + } |
172 | 226 | }
|
0 commit comments