drill-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From GitBox <...@apache.org>
Subject [GitHub] [drill] sanel commented on a change in pull request #2085: DRILL-7746: Add REST API Ability to Accept CSV Input
Date Tue, 02 Jun 2020 18:14:01 GMT

sanel commented on a change in pull request #2085:
URL: https://github.com/apache/drill/pull/2085#discussion_r434080421



##########
File path: contrib/storage-http/src/main/java/org/apache/drill/exec/store/http/HttpCSVBatchReader.java
##########
@@ -0,0 +1,186 @@
+/*
+ * 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.http;
+
+import com.univocity.parsers.csv.CsvParser;
+import com.univocity.parsers.csv.CsvParserSettings;
+import okhttp3.HttpUrl;
+import org.apache.drill.common.exceptions.ChildErrorContext;
+import org.apache.drill.common.exceptions.CustomErrorContext;
+import org.apache.drill.common.exceptions.UserException;
+import org.apache.drill.common.types.TypeProtos;
+import org.apache.drill.exec.ExecConstants;
+import org.apache.drill.exec.physical.impl.scan.framework.SchemaNegotiator;
+import org.apache.drill.exec.physical.resultSet.ResultSetLoader;
+import org.apache.drill.exec.physical.resultSet.RowSetLoader;
+import org.apache.drill.exec.record.metadata.SchemaBuilder;
+import org.apache.drill.exec.record.metadata.TupleMetadata;
+import org.apache.drill.exec.store.http.util.SimpleHttp;
+import org.apache.drill.exec.vector.accessor.ScalarWriter;
+import org.apache.drill.shaded.guava.com.google.common.base.Strings;
+
+import java.io.File;
+import java.io.InputStream;
+import java.util.ArrayList;
+import java.util.List;
+
+public class HttpCSVBatchReader extends HttpBatchReader {
+  private final HttpSubScan subScan;
+  private final CsvParserSettings csvSettings;
+  private CsvParser csvReader;
+  private List<StringColumnWriter> columnWriters;
+  private String[] firstRow;
+  private SchemaBuilder builder;
+  private RowSetLoader rowWriter;
+
+
+  public HttpCSVBatchReader(HttpSubScan subScan) {
+    super(subScan);
+    this.subScan = subScan;
+
+    this.csvSettings = new CsvParserSettings();
+    csvSettings.setLineSeparatorDetectionEnabled(true);
+  }
+
+  @Override
+  public boolean open(SchemaNegotiator negotiator) {
+
+    // Result set loader setup
+    String tempDirPath = negotiator.drillConfig().getString(ExecConstants.DRILL_TMP_DIR);
+
+    HttpUrl url = buildUrl();
+
+    CustomErrorContext errorContext = new ChildErrorContext(negotiator.parentErrorContext())
{
+      @Override
+      public void addContext(UserException.Builder builder) {
+        super.addContext(builder);
+        builder.addContext("URL", url.toString());
+      }
+    };
+    negotiator.setErrorContext(errorContext);
+
+    // Http client setup
+    SimpleHttp http = new SimpleHttp(subScan, url, new File(tempDirPath), proxySettings(negotiator.drillConfig(),
url), errorContext);
+
+    // CSV loader setup
+    InputStream inStream = http.getInputStream();
+
+    this.csvReader = new CsvParser(csvSettings);
+    csvReader.beginParsing(inStream);
+
+    // Build the Schema
+    builder = new SchemaBuilder();
+    TupleMetadata drillSchema = buildSchema();

Review comment:
       If `buildSchema()` is called only once, I presume `csvReader.beginParsing(inStream)`
can go in it, to localize state; something like:
   
   ```java
   buildSchema(http.getInputStream());
   ``` 




----------------------------------------------------------------
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:
users@infra.apache.org



Mime
View raw message