Search in sources :

Example 1 with DruidScanResponse

use of org.apache.drill.exec.store.druid.druid.DruidScanResponse in project drill by apache.

the class DruidRecordReader method next.

@Override
public int next() {
    writer.allocate();
    writer.reset();
    Stopwatch watch = Stopwatch.createStarted();
    try {
        String query = getQuery();
        DruidScanResponse druidScanResponse = druidQueryClient.executeQuery(query);
        setNextOffset(druidScanResponse);
        int docCount = 0;
        for (ObjectNode eventNode : druidScanResponse.getEvents()) {
            writer.setPosition(docCount);
            jsonReader.setSource(eventNode);
            try {
                jsonReader.write(writer);
            } catch (IOException e) {
                throw UserException.dataReadError(e).message("Failure while reading document").addContext("Failed Query", query).addContext("Parser was at record", eventNode.toString()).addContext(e.getMessage()).build(logger);
            }
            docCount++;
        }
        writer.setValueCount(docCount);
        logger.debug("Took {} ms to get {} records", watch.elapsed(TimeUnit.MILLISECONDS), docCount);
        return docCount;
    } catch (Exception e) {
        throw UserException.dataReadError(e).message("Failure while executing druid query").addContext(e.getMessage()).build(logger);
    }
}
Also used : ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) Stopwatch(org.apache.drill.shaded.guava.com.google.common.base.Stopwatch) DruidScanResponse(org.apache.drill.exec.store.druid.druid.DruidScanResponse) IOException(java.io.IOException) UserException(org.apache.drill.common.exceptions.UserException) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) IOException(java.io.IOException)

Example 2 with DruidScanResponse

use of org.apache.drill.exec.store.druid.druid.DruidScanResponse in project drill by apache.

the class DruidQueryClient method parseResponse.

private DruidScanResponse parseResponse(ArrayNode responses) {
    String segmentId = "empty";
    ArrayList<ObjectNode> events = new ArrayList<>();
    ArrayList<String> columns = new ArrayList<>();
    if (responses.size() > 0) {
        ObjectNode firstNode = (ObjectNode) responses.get(0);
        segmentId = firstNode.get("segmentId").textValue();
        ArrayNode columnsNode = (ArrayNode) firstNode.get("columns");
        ArrayNode eventsNode = (ArrayNode) firstNode.get("events");
        for (int i = 0; i < columnsNode.size(); i++) {
            String column = columnsNode.get(i).textValue();
            columns.add(column);
        }
        for (int i = 0; i < eventsNode.size(); i++) {
            ObjectNode eventNode = (ObjectNode) eventsNode.get(i);
            events.add(eventNode);
        }
    }
    return new DruidScanResponse(segmentId, columns, events);
}
Also used : ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) ArrayList(java.util.ArrayList) DruidScanResponse(org.apache.drill.exec.store.druid.druid.DruidScanResponse) ArrayNode(com.fasterxml.jackson.databind.node.ArrayNode)

Example 3 with DruidScanResponse

use of org.apache.drill.exec.store.druid.druid.DruidScanResponse in project drill by apache.

the class DruidQueryClientTest method executeQueryCalledSuccessfullyParseQueryResults.

@Test
public void executeQueryCalledSuccessfullyParseQueryResults() throws Exception {
    String result = "[{\"segmentId\":\"wikipedia_2016-06-27T14:00:00.000Z_2016-06-27T15:00:00.000Z_2021-12-11T11:12:16.106Z\",\"columns\":[\"__time\",\"channel\",\"cityName\",\"comment\",\"countryIsoCode\",\"countryName\",\"diffUrl\",\"flags\",\"isAnonymous\",\"isMinor\",\"isNew\",\"isRobot\",\"isUnpatrolled\",\"metroCode\",\"namespace\",\"page\",\"regionIsoCode\",\"regionName\",\"user\",\"sum_deleted\",\"sum_deltaBucket\",\"sum_added\",\"sum_commentLength\",\"count\",\"sum_delta\"],\"events\":[{\"__time\":1467036000000,\"channel\":\"#de.wikipedia\",\"cityName\":null,\"comment\":\"Bitte [[WP:Literatur]] beachten.\",\"countryIsoCode\":null,\"countryName\":null,\"diffUrl\":\"https://de.wikipedia.org/w/index.php?diff=155672392&oldid=155667393\",\"flags\":null,\"isAnonymous\":\"false\",\"isMinor\":\"false\",\"isNew\":\"false\",\"isRobot\":\"false\",\"isUnpatrolled\":\"false\",\"metroCode\":null,\"namespace\":\"Main\",\"page\":\"Walfang\",\"regionIsoCode\":null,\"regionName\":null,\"user\":\"Dansker\",\"sum_deleted\":133,\"sum_deltaBucket\":-200,\"sum_added\":0,\"sum_commentLength\":32,\"count\":1,\"sum_delta\":-133}]}]";
    InputStream inputStream = new ByteArrayInputStream(result.getBytes(StandardCharsets.UTF_8.name()));
    when(httpEntity.getContent()).thenReturn(inputStream);
    DruidScanResponse response = druidQueryClient.executeQuery(QUERY);
    assertThat(response.getEvents()).isNotEmpty();
    assertThat(response.getEvents().size()).isEqualTo(1);
    assertThat(response.getEvents().get(0).get("user").textValue()).isEqualTo("Dansker");
    assertThat(response.getEvents().get(0).get("sum_deleted").intValue()).isEqualTo(133);
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) DruidScanResponse(org.apache.drill.exec.store.druid.druid.DruidScanResponse) Test(org.junit.Test)

Example 4 with DruidScanResponse

use of org.apache.drill.exec.store.druid.druid.DruidScanResponse in project drill by apache.

the class DruidQueryClientTest method executeQueryCalledNoResponsesFoundReturnsEmptyEventList.

@Test
public void executeQueryCalledNoResponsesFoundReturnsEmptyEventList() throws Exception {
    InputStream inputStream = new ByteArrayInputStream("[]".getBytes(StandardCharsets.UTF_8.name()));
    when(httpEntity.getContent()).thenReturn(inputStream);
    DruidScanResponse response = druidQueryClient.executeQuery(QUERY);
    assertThat(response.getEvents()).isEmpty();
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) DruidScanResponse(org.apache.drill.exec.store.druid.druid.DruidScanResponse) Test(org.junit.Test)

Aggregations

DruidScanResponse (org.apache.drill.exec.store.druid.druid.DruidScanResponse)4 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)2 ByteArrayInputStream (java.io.ByteArrayInputStream)2 InputStream (java.io.InputStream)2 Test (org.junit.Test)2 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)1 ArrayNode (com.fasterxml.jackson.databind.node.ArrayNode)1 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 UserException (org.apache.drill.common.exceptions.UserException)1 Stopwatch (org.apache.drill.shaded.guava.com.google.common.base.Stopwatch)1