[ 
https://issues.apache.org/jira/browse/DRILL-5956?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=17110771#comment-17110771
 ] 

ASF GitHub Bot commented on DRILL-5956:
---------------------------------------

akkapur commented on a change in pull request #1888:
URL: https://github.com/apache/drill/pull/1888#discussion_r426987466



##########
File path: 
contrib/storage-druid/src/main/java/org/apache/drill/exec/store/druid/DruidRecordReader.java
##########
@@ -0,0 +1,173 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.drill.exec.store.druid;
+
+import com.fasterxml.jackson.databind.ObjectMapper;
+import com.fasterxml.jackson.databind.node.ObjectNode;
+import org.apache.drill.common.exceptions.DrillRuntimeException;
+import org.apache.drill.common.exceptions.ExecutionSetupException;
+import org.apache.drill.common.expression.SchemaPath;
+import org.apache.drill.exec.ops.FragmentContext;
+import org.apache.drill.exec.ops.OperatorContext;
+import org.apache.drill.exec.physical.impl.OutputMutator;
+import org.apache.drill.exec.store.AbstractRecordReader;
+import org.apache.drill.exec.store.druid.druid.DruidSelectResponse;
+import org.apache.drill.exec.store.druid.druid.PagingIdentifier;
+import org.apache.drill.exec.store.druid.druid.PagingSpec;
+import org.apache.drill.exec.store.druid.druid.SelectQuery;
+import org.apache.drill.exec.vector.complex.fn.JsonReader;
+import org.apache.drill.exec.vector.complex.impl.VectorContainerWriter;
+import org.apache.drill.shaded.guava.com.google.common.collect.ImmutableList;
+import org.apache.drill.shaded.guava.com.google.common.collect.Sets;
+
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.List;
+import java.util.Set;
+
+public class DruidRecordReader extends AbstractRecordReader {
+
+    private static final org.slf4j.Logger logger = 
org.slf4j.LoggerFactory.getLogger(DruidRecordReader.class);
+    private DruidStoragePlugin plugin;
+    private final DruidSubScan.DruidSubScanSpec scanSpec;
+    private List<String> dimensions;
+    private String filters;
+    private ArrayList<PagingIdentifier> pagingIdentifiers = new ArrayList<>();
+
+    private JsonReader jsonReader;
+    private VectorContainerWriter writer;
+
+    private OutputMutator output;
+    private OperatorContext context;
+    private final FragmentContext fragmentContext;
+
+    private ObjectMapper objectMapper = new ObjectMapper();
+
+    public DruidRecordReader(DruidSubScan.DruidSubScanSpec subScanSpec, 
List<SchemaPath> projectedColumns,
+                             FragmentContext context, DruidStoragePlugin 
plugin) {
+        dimensions = new ArrayList<String>();
+        setColumns(projectedColumns);
+        this.plugin = plugin;
+        scanSpec = subScanSpec;
+        fragmentContext = context;
+        this.filters = subScanSpec.getFilter();
+    }
+
+    @Override
+    protected Collection<SchemaPath> transformColumns(Collection<SchemaPath> 
projectedColumns) {
+        Set<SchemaPath> transformed = Sets.newLinkedHashSet();
+        if (isStarQuery()) {
+            transformed.add(SchemaPath.STAR_COLUMN);
+        } else {
+            for (SchemaPath column : projectedColumns) {
+                String fieldName = column.getRootSegment().getPath();
+                transformed.add(column);
+                this.dimensions.add(fieldName);
+            }
+        }
+        return transformed;
+    }
+
+    @Override
+    public void setup(OperatorContext context, OutputMutator output) throws 
ExecutionSetupException {
+        this.context = context;
+        this.output = output;
+        this.writer = new VectorContainerWriter(output);
+
+        //Lists.newArrayList(getColumns()), true, false, false
+        this.jsonReader =
+                new JsonReader.Builder(fragmentContext.getManagedBuffer())
+                        .schemaPathColumns(ImmutableList.copyOf(getColumns()))
+                        .skipOuterList(true)
+                        .build();
+        logger.debug(" Initialized JsonRecordReader. ");
+    }
+
+    @Override
+    public int next() {
+
+        writer.allocate();
+        writer.reset();
+        SelectQuery selectQuery = new SelectQuery(scanSpec.dataSourceName);
+        selectQuery.setDimensions(this.dimensions);
+        selectQuery.setFilter(this.filters);
+
+        ObjectNode paging = objectMapper.createObjectNode();
+        if (this.pagingIdentifiers != null && 
!this.pagingIdentifiers.isEmpty()) {
+            for (PagingIdentifier pagingIdentifier : this.pagingIdentifiers) {
+                paging.put(pagingIdentifier.getSegmentName(), 
pagingIdentifier.getSegmentOffset());
+            }
+        }
+
+        PagingSpec pagingSpec = new PagingSpec(paging);
+        selectQuery.setPagingSpec(pagingSpec);
+
+        DruidQueryClient druidQueryClient = plugin.getDruidQueryClient();
+
+        try {
+            String query = selectQuery.toJson();
+            logger.debug("Executing DRUID query - " + query);
+            DruidSelectResponse druidSelectResponse = 
druidQueryClient.ExecuteQuery(query);
+            ArrayList<PagingIdentifier> newPagingIdentifiers = 
druidSelectResponse.getPagingIdentifiers();
+
+            ArrayList<String> newPagingIdentifierNames = new ArrayList<>();
+            for (PagingIdentifier pagingIdentifier : newPagingIdentifiers) {
+                
newPagingIdentifierNames.add(pagingIdentifier.getSegmentName());
+            }
+
+            for (PagingIdentifier pagingIdentifier : this.pagingIdentifiers) {
+                if 
(!newPagingIdentifierNames.contains(pagingIdentifier.getSegmentName())) {
+                    newPagingIdentifiers.add(
+                            new 
PagingIdentifier(pagingIdentifier.getSegmentName(),
+                                    pagingIdentifier.getSegmentOffset() + 1)
+                    );
+                }
+            }
+
+            //update the paging identifiers
+            this.pagingIdentifiers = newPagingIdentifiers;
+
+            int docCount = 0;
+            for (ObjectNode eventNode : druidSelectResponse.getEvents()) {
+                writer.setPosition(docCount);
+                jsonReader.setSource(eventNode);
+                try {
+                    jsonReader.write(writer);
+                } catch (IOException e) {
+                    String msg = "Failure while reading document. - Parser was 
at record: " + eventNode.toString();
+                    logger.error(msg, e);
+                    throw new DrillRuntimeException(msg, e);
+                }
+                docCount++;
+            }
+
+            writer.setValueCount(docCount);
+            return docCount;
+        } catch (IOException e) {
+            String msg = "Failure while reading documents";
+            logger.error(msg, e);
+            throw new DrillRuntimeException(msg, e);
+        }
+    }
+
+    @Override
+    public void close() throws Exception {
+

Review comment:
       I think this was changes earlier.




----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


> Add Storage Plugin for Apache Druid
> -----------------------------------
>
>                 Key: DRILL-5956
>                 URL: https://issues.apache.org/jira/browse/DRILL-5956
>             Project: Apache Drill
>          Issue Type: Wish
>          Components: Storage - Other
>            Reporter: Jiaqi Liu
>            Priority: Major
>              Labels: Enhancement, Storage-Plugin
>             Fix For: 1.18.0
>
>
> As more and more companies are using Druid for mission-critical industrial 
> products, Drill could gain much more popularity with Druid as one of its 
> supported storage plugin so that uses could easily bind Druid cluster to 
> running Drill instance



--
This message was sent by Atlassian Jira
(v8.3.4#803005)

Reply via email to