You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@druid.apache.org by GitBox <gi...@apache.org> on 2019/11/22 05:44:22 UTC

[GitHub] [incubator-druid] suneet-amp commented on a change in pull request #8915: add TsvInputFormat

suneet-amp commented on a change in pull request #8915: add TsvInputFormat
URL: https://github.com/apache/incubator-druid/pull/8915#discussion_r349440447
 
 

 ##########
 File path: core/src/main/java/org/apache/druid/data/input/impl/SeparateValueInputFormat.java
 ##########
 @@ -0,0 +1,204 @@
+/*
+ * 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.druid.data.input.impl;
+
+import com.fasterxml.jackson.annotation.JsonCreator;
+import com.fasterxml.jackson.annotation.JsonProperty;
+import com.google.common.base.Preconditions;
+import com.google.common.collect.ImmutableList;
+import org.apache.druid.data.input.InputEntity;
+import org.apache.druid.data.input.InputEntityReader;
+import org.apache.druid.data.input.InputFormat;
+import org.apache.druid.data.input.InputRowSchema;
+import org.apache.druid.indexer.Checks;
+import org.apache.druid.indexer.Property;
+
+import javax.annotation.Nullable;
+import java.io.File;
+import java.util.Collections;
+import java.util.List;
+import java.util.Objects;
+
+public class SeparateValueInputFormat implements InputFormat
+{
+
+  public enum FlatTextFormat
+  {
+    CSV(","),
+    TSV("\t");
+
+    private final String defaultDelimiter;
+
+    FlatTextFormat(String defaultDelimiter)
+    {
+      this.defaultDelimiter = defaultDelimiter;
+    }
+
+    public String getDefaultDelimiter()
+    {
+
+      return defaultDelimiter;
+    }
+
+    public String getLiteral()
+    {
+      return ",".equals(defaultDelimiter) ? "comma" : "tab";
+    }
+  }
+
+  private final String listDelimiter;
+  private final List<String> columns;
+  private final boolean findColumnsFromHeader;
+  private final int skipHeaderRows;
+  private final FlatTextFormat format;
+
+  @JsonCreator
+  public SeparateValueInputFormat(
+      @JsonProperty("columns") @Nullable List<String> columns,
+      @JsonProperty("listDelimiter") @Nullable String listDelimiter,
+      @Deprecated @JsonProperty("hasHeaderRow") @Nullable Boolean hasHeaderRow,
+      @JsonProperty("findColumnsFromHeader") @Nullable Boolean findColumnsFromHeader,
+      @JsonProperty("skipHeaderRows") int skipHeaderRows,
+      FlatTextFormat format
+  )
+  {
+    this.listDelimiter = listDelimiter;
+    this.columns = columns == null ? Collections.emptyList() : columns;
+    //noinspection ConstantConditions
+    this.findColumnsFromHeader = Checks.checkOneNotNullOrEmpty(
+        ImmutableList.of(
+            new Property<>("hasHeaderRow", hasHeaderRow),
+            new Property<>("findColumnsFromHeader", findColumnsFromHeader)
+        )
+    ).getValue();
+    this.skipHeaderRows = skipHeaderRows;
+    this.format = format;
+
+    if (!this.columns.isEmpty()) {
+      for (String column : this.columns) {
+        Preconditions.checkArgument(
+            !column.contains(format.getDefaultDelimiter()),
+            "Column[%s] has a " + format.getLiteral() + ", it cannot",
+            column
+        );
+      }
+    } else {
+      Preconditions.checkArgument(
+          this.findColumnsFromHeader,
+          "If columns field is not set, the first row of your data must have your header"
+          + " and hasHeaderRow must be set to true."
+      );
+    }
+  }
+
+
+  @JsonProperty
+  public List<String> getColumns()
+  {
+    return columns;
+  }
+
+  @JsonProperty
+  public String getListDelimiter()
+  {
+    return listDelimiter;
+  }
+
+  @JsonProperty
+  public boolean isFindColumnsFromHeader()
+  {
+    return findColumnsFromHeader;
+  }
+
+  @JsonProperty
+  public int getSkipHeaderRows()
+  {
+    return skipHeaderRows;
+  }
+
+  @Override
+  public boolean isSplittable()
+  {
+    return true;
+  }
+
+  @Override
+  public InputEntityReader createReader(InputRowSchema inputRowSchema, InputEntity source, File temporaryDirectory)
+  {
+    return new SeparateValueReader(
+        inputRowSchema,
+        source,
+        temporaryDirectory,
+        listDelimiter,
+        columns,
+        findColumnsFromHeader,
+        skipHeaderRows,
+        format
+    );
+  }
+
+  public InputEntityReader createReader(
+      InputRowSchema inputRowSchema,
+      InputEntity source,
+      File temporaryDirectory,
+      FlatTextFormat format
+  )
+  {
+    return format == FlatTextFormat.TSV ? new TsvReader(
+        inputRowSchema,
+        source,
+        temporaryDirectory,
+        listDelimiter,
+        columns,
+        findColumnsFromHeader,
+        skipHeaderRows
+    ) : new CsvReader(
+        inputRowSchema,
+        source,
+        temporaryDirectory,
+        listDelimiter,
+        columns,
+        findColumnsFromHeader,
+        skipHeaderRows
+    );
+  }
+
+  @Override
+  public boolean equals(Object o)
+  {
+    if (this == o) {
+      return true;
+    }
+    if (o == null || getClass() != o.getClass()) {
+      return false;
+    }
+    SeparateValueInputFormat format = (SeparateValueInputFormat) o;
+    return findColumnsFromHeader == format.findColumnsFromHeader &&
+           skipHeaderRows == format.skipHeaderRows &&
+           Objects.equals(listDelimiter, format.listDelimiter) &&
+           Objects.equals(columns, format.columns);
+  }
+
+  @Override
+  public int hashCode()
+  {
+    return Objects.hash(listDelimiter, columns, findColumnsFromHeader, skipHeaderRows);
+  }
 
 Review comment:
   Looks like format is missing in equalsAndHashCode

----------------------------------------------------------------
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


With regards,
Apache Git Services

---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@druid.apache.org
For additional commands, e-mail: commits-help@druid.apache.org