You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@hudi.apache.org by GitBox <gi...@apache.org> on 2020/07/30 07:25:19 UTC

[GitHub] [hudi] yanghua commented on a change in pull request #1884: [HUDI-995] Use Transformations, Assertions and SchemaTestUtil

yanghua commented on a change in pull request #1884:
URL: https://github.com/apache/hudi/pull/1884#discussion_r462669484



##########
File path: hudi-common/src/test/java/org/apache/hudi/common/testutils/Transformations.java
##########
@@ -0,0 +1,100 @@
+/*
+ * 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.hudi.common.testutils;
+
+import org.apache.hudi.common.model.HoodieKey;
+import org.apache.hudi.common.model.HoodieRecord;
+import org.apache.hudi.common.util.Option;
+
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.Iterator;
+import java.util.List;
+import java.util.Set;
+import java.util.stream.Collectors;
+
+/**
+ * Common transformations in test cases.
+ */
+public final class Transformations {
+
+  public static <T> List<T> flatten(Iterator<List<T>> iteratorOfLists) {
+    List<T> flattened = new ArrayList<>();
+    iteratorOfLists.forEachRemaining(flattened::addAll);
+    return flattened;
+  }
+
+  public static <T> Iterator<T> flattenAsIterator(Iterator<List<T>> iteratorOfLists) {
+    return flatten(iteratorOfLists).iterator();
+  }
+
+  public static Set<String> recordsToRecordKeySet(List<HoodieRecord> records) {
+    return records.stream().map(HoodieRecord::getRecordKey).collect(Collectors.toSet());
+  }
+
+  public static List<HoodieKey> recordsToHoodieKeys(List<HoodieRecord> records) {
+    return records.stream().map(HoodieRecord::getKey).collect(Collectors.toList());
+  }
+
+  public static List<String> hoodieKeysToStrings(List<HoodieKey> keys) {
+    return keys.stream()
+        .map(hr -> "{\"_row_key\":\"" + hr.getRecordKey() + "\",\"partition\":\"" + hr.getPartitionPath() + "\"}")
+        .collect(Collectors.toList());
+  }
+
+  public static List<String> recordsToStrings(List<HoodieRecord> records) {
+    return records.stream().map(Transformations::recordToString).filter(Option::isPresent).map(Option::get)
+        .collect(Collectors.toList());
+  }
+
+  public static Option<String> recordToString(HoodieRecord record) {

Review comment:
       ditto

##########
File path: hudi-common/src/test/java/org/apache/hudi/common/testutils/Transformations.java
##########
@@ -0,0 +1,100 @@
+/*
+ * 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.hudi.common.testutils;
+
+import org.apache.hudi.common.model.HoodieKey;
+import org.apache.hudi.common.model.HoodieRecord;
+import org.apache.hudi.common.util.Option;
+
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.Iterator;
+import java.util.List;
+import java.util.Set;
+import java.util.stream.Collectors;
+
+/**
+ * Common transformations in test cases.
+ */
+public final class Transformations {
+
+  public static <T> List<T> flatten(Iterator<List<T>> iteratorOfLists) {
+    List<T> flattened = new ArrayList<>();
+    iteratorOfLists.forEachRemaining(flattened::addAll);
+    return flattened;
+  }
+
+  public static <T> Iterator<T> flattenAsIterator(Iterator<List<T>> iteratorOfLists) {
+    return flatten(iteratorOfLists).iterator();
+  }
+
+  public static Set<String> recordsToRecordKeySet(List<HoodieRecord> records) {
+    return records.stream().map(HoodieRecord::getRecordKey).collect(Collectors.toSet());
+  }
+
+  public static List<HoodieKey> recordsToHoodieKeys(List<HoodieRecord> records) {
+    return records.stream().map(HoodieRecord::getKey).collect(Collectors.toList());
+  }
+
+  public static List<String> hoodieKeysToStrings(List<HoodieKey> keys) {

Review comment:
       Considering `_row_key` is not a generic definition. IMO, we should put this method into a util class. The `xxxs` class should be more generic. WDYT?

##########
File path: hudi-common/src/test/java/org/apache/hudi/common/testutils/SchemaTestUtil.java
##########
@@ -66,12 +66,12 @@ public static Schema getSimpleSchema() throws IOException {
       throws IOException, URISyntaxException {
     GenericDatumReader<IndexedRecord> reader = new GenericDatumReader<>(writerSchema, readerSchema);
     // Required to register the necessary JAR:// file system
-    URI resource = SchemaTestUtil.class.getClass().getResource("/sample.data").toURI();
+    URI resource = SchemaTestUtil.class.getResource("/sample.data").toURI();

Review comment:
       It would be better to extract those file names into constant fields in this file. WDYT?

##########
File path: hudi-common/src/test/java/org/apache/hudi/common/testutils/Transformations.java
##########
@@ -0,0 +1,100 @@
+/*
+ * 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.hudi.common.testutils;
+
+import org.apache.hudi.common.model.HoodieKey;
+import org.apache.hudi.common.model.HoodieRecord;
+import org.apache.hudi.common.util.Option;
+
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.Iterator;
+import java.util.List;
+import java.util.Set;
+import java.util.stream.Collectors;
+
+/**
+ * Common transformations in test cases.
+ */
+public final class Transformations {
+
+  public static <T> List<T> flatten(Iterator<List<T>> iteratorOfLists) {
+    List<T> flattened = new ArrayList<>();
+    iteratorOfLists.forEachRemaining(flattened::addAll);
+    return flattened;
+  }
+
+  public static <T> Iterator<T> flattenAsIterator(Iterator<List<T>> iteratorOfLists) {
+    return flatten(iteratorOfLists).iterator();
+  }
+
+  public static Set<String> recordsToRecordKeySet(List<HoodieRecord> records) {
+    return records.stream().map(HoodieRecord::getRecordKey).collect(Collectors.toSet());
+  }
+
+  public static List<HoodieKey> recordsToHoodieKeys(List<HoodieRecord> records) {
+    return records.stream().map(HoodieRecord::getKey).collect(Collectors.toList());
+  }
+
+  public static List<String> hoodieKeysToStrings(List<HoodieKey> keys) {
+    return keys.stream()
+        .map(hr -> "{\"_row_key\":\"" + hr.getRecordKey() + "\",\"partition\":\"" + hr.getPartitionPath() + "\"}")
+        .collect(Collectors.toList());
+  }
+
+  public static List<String> recordsToStrings(List<HoodieRecord> records) {
+    return records.stream().map(Transformations::recordToString).filter(Option::isPresent).map(Option::get)
+        .collect(Collectors.toList());
+  }
+
+  public static Option<String> recordToString(HoodieRecord record) {

Review comment:
       Sounds good.




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