beam-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "ASF GitHub Bot (JIRA)" <j...@apache.org>
Subject [jira] [Work logged] (BEAM-4461) Create a library of useful transforms that use schemas
Date Wed, 12 Sep 2018 04:31:00 GMT

     [ https://issues.apache.org/jira/browse/BEAM-4461?focusedWorklogId=143365&page=com.atlassian.jira.plugin.system.issuetabpanels:worklog-tabpanel#worklog-143365
]

ASF GitHub Bot logged work on BEAM-4461:
----------------------------------------

                Author: ASF GitHub Bot
            Created on: 12/Sep/18 04:30
            Start Date: 12/Sep/18 04:30
    Worklog Time Spent: 10m 
      Work Description: reuvenlax commented on a change in pull request #6316: [BEAM-4461]
Add Unnest transform.
URL: https://github.com/apache/beam/pull/6316#discussion_r216893853
 
 

 ##########
 File path: sdks/java/core/src/main/java/org/apache/beam/sdk/schemas/transforms/Unnest.java
 ##########
 @@ -0,0 +1,182 @@
+/*
+ * 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.beam.sdk.schemas.transforms;
+
+import com.google.auto.value.AutoValue;
+import com.google.common.collect.Lists;
+import java.util.List;
+import org.apache.beam.sdk.annotations.Experimental;
+import org.apache.beam.sdk.annotations.Experimental.Kind;
+import org.apache.beam.sdk.schemas.Schema;
+import org.apache.beam.sdk.schemas.Schema.Field;
+import org.apache.beam.sdk.transforms.DoFn;
+import org.apache.beam.sdk.transforms.PTransform;
+import org.apache.beam.sdk.transforms.ParDo;
+import org.apache.beam.sdk.transforms.SerializableFunction;
+import org.apache.beam.sdk.values.PCollection;
+import org.apache.beam.sdk.values.Row;
+/**
+ * A {@link PTransform} to unnest nested rows.
+ *
+ * <p>For example, consider a Row with the following nestedschema:
+ *
+ * <p>UserEvent Schema: userid: INT64 timestamp: DATETIME location: LatLong
+ *
+ * <p>LatLong Schema: latitude: DOUBLE longitude: DOUBLE
+ *
+ * <p>After unnesting, all of the rows will be converted to rows satisfying the following
schema:
+ *
+ * <p>UserEvent Schema: userid: INT64 timestamp: DATETIME location.latitude: DOUBLE
+ * location.longitude: DOUBLE
+ *
+ * <p>By default nested names are concatenated to generated the unnested name, however
{@link
+ * Unnest.Inner#withFieldNameFunction} can be used to specify a custom naming policy.
+ *
+ * <p>Note that currently array and map values are not unnested.
+ */
+@Experimental(Kind.SCHEMAS)
+public class Unnest {
+  public static <T> Inner<T> create() {
+    return new AutoValue_Unnest_Inner.Builder<T>()
+        .setMaxLevels(Integer.MAX_VALUE)
+        .setFieldNameFunction(CONCAT_FIELD_NAMES)
+        .build();
+  }
+  /**
+   * This is the default naming policy for naming fields. Every field name in the path to
a given
+   * field is concated with . characters.
+   */
+  public static final SerializableFunction<List<String>, String> CONCAT_FIELD_NAMES
=
+      l -> {
+        return String.join(".", l);
+      };
+  /**
+   * This policy keeps the raw nested field name. If two differently-nested fields have the
same
+   * name, unnesting will fail with this policy.
+   */
+  public static final SerializableFunction<List<String>, String> KEEP_NESTED_NAME
=
+      l -> {
+        return l.get(l.size() - 1);
+      };
+  /** Returns the result of unnesting the given schema. The default naming policy is used.
*/
+  static Schema getUnnestedSchema(Schema schema, int maxLevels) {
+    List<String> nameComponents = Lists.newArrayList();
+    return getUnnestedSchema(schema, nameComponents, CONCAT_FIELD_NAMES, maxLevels, 0);
+  }
+  /** Returns the result of unnesting the given schema with the given naming policy. */
+  static Schema getUnnestedSchema(
+      Schema schema, int maxLevels, SerializableFunction<List<String>, String>
fn) {
+    List<String> nameComponents = Lists.newArrayList();
+    return getUnnestedSchema(schema, nameComponents, fn, maxLevels, 0);
+  }
+
+  private static Schema getUnnestedSchema(
+      Schema schema,
+      List<String> nameComponents,
+      SerializableFunction<List<String>, String> fn,
+      int maxLevel,
+      int currentLevel) {
+    Schema.Builder builder = Schema.builder();
+    for (Field field : schema.getFields()) {
+      nameComponents.add(field.getName());
+      if (field.getType().getTypeName().isCompositeType() && currentLevel < maxLevel)
{
+        Schema nestedSchema =
+            getUnnestedSchema(
+                field.getType().getRowSchema(), nameComponents, fn, maxLevel, currentLevel
+ 1);
+        for (Field nestedField : nestedSchema.getFields()) {
+          builder.addField(nestedField);
+        }
+      } else {
+        String name = fn.apply(nameComponents);
+        Field newField = field.toBuilder().setName(name).build();
+        builder.addField(newField);
+      }
+      nameComponents.remove(nameComponents.size() - 1);
+    }
+    return builder.build();
+  }
+  /** Unnest a row. */
+  static Row unnestRow(Row input, Schema unnestedSchema, int maxLevel) {
+    Row.Builder builder = Row.withSchema(unnestedSchema);
+    unnestRow(input, builder, maxLevel, 0);
+    return builder.build();
+  }
+
+  private static void unnestRow(Row input, Row.Builder output, int maxLevel, int currentLevel)
{
+    for (int i = 0; i < input.getSchema().getFieldCount(); ++i) {
+      Field field = input.getSchema().getField(i);
+      if (field.getType().getTypeName().isCompositeType() && currentLevel < maxLevel)
{
+        unnestRow(input.getRow(i), output, maxLevel, currentLevel + 1);
+      } else {
+        output.addValue(input.getValue(i));
+      }
+    }
+  }
+  /** A {@link PTransform} that unnests nested row. */
+  @AutoValue
+  public abstract static class Inner<T> extends PTransform<PCollection<T>,
PCollection<Row>> {
+    abstract Builder<T> toBuilder();
+
+    @AutoValue.Builder
+    abstract static class Builder<T> {
+      abstract Builder<T> setMaxLevels(int maxLevels);
+
+      abstract Builder<T> setFieldNameFunction(SerializableFunction<List<String>,
String> fn);
+
+      abstract Inner<T> build();
+    };
+
+    abstract int getMaxLevels();
 
 Review comment:
   Good point. Removed for now

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on 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


Issue Time Tracking
-------------------

    Worklog Id:     (was: 143365)
    Time Spent: 7.5h  (was: 7h 20m)

> Create a library of useful transforms that use schemas
> ------------------------------------------------------
>
>                 Key: BEAM-4461
>                 URL: https://issues.apache.org/jira/browse/BEAM-4461
>             Project: Beam
>          Issue Type: Sub-task
>          Components: sdk-java-core
>            Reporter: Reuven Lax
>            Assignee: Reuven Lax
>            Priority: Major
>          Time Spent: 7.5h
>  Remaining Estimate: 0h
>
> e.g. JoinBy(fields). Project, Filter, etc.



--
This message was sent by Atlassian JIRA
(v7.6.3#76005)

Mime
View raw message