You are viewing a plain text version of this content. The canonical link for it is here.
Posted to github@beam.apache.org by GitBox <gi...@apache.org> on 2022/08/18 11:31:18 UTC

[GitHub] [beam] Amar3tto commented on a diff in pull request #17828: [BEAM-14378] [CdapIO] SparkReceiverIO Read via SDF

Amar3tto commented on code in PR #17828:
URL: https://github.com/apache/beam/pull/17828#discussion_r949016993


##########
sdks/java/io/sparkreceiver/src/test/java/org/apache/beam/sdk/io/sparkreceiver/SparkReceiverIOTest.java:
##########
@@ -0,0 +1,125 @@
+/*
+ * 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.io.sparkreceiver;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertThrows;
+
+import java.util.List;
+import org.apache.beam.runners.direct.DirectOptions;
+import org.apache.beam.runners.direct.DirectRunner;
+import org.apache.beam.sdk.Pipeline;
+import org.apache.beam.sdk.coders.StringUtf8Coder;
+import org.apache.beam.sdk.options.PipelineOptionsFactory;
+import org.apache.beam.sdk.transforms.ParDo;
+import org.apache.beam.sdk.transforms.SerializableFunction;
+import org.joda.time.Duration;
+import org.joda.time.Instant;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.junit.runners.JUnit4;
+
+/** Test class for {@link SparkReceiverIO}. */
+@RunWith(JUnit4.class)
+public class SparkReceiverIOTest {
+
+  @Test
+  public void testReadBuildsCorrectly() {
+    ReceiverBuilder<String, CustomReceiverWithOffset> receiverBuilder =
+        new ReceiverBuilder<>(CustomReceiverWithOffset.class).withConstructorArgs();
+    SerializableFunction<String, Long> offsetFn = Long::valueOf;
+    SerializableFunction<String, Instant> watermarkFn = Instant::parse;
+
+    SparkReceiverIO.Read<String> read =
+        SparkReceiverIO.<String>read()
+            .withValueClass(String.class)
+            .withGetOffsetFn(offsetFn)
+            .withWatermarkFn(watermarkFn)
+            .withSparkReceiverBuilder(receiverBuilder);
+
+    assertEquals(offsetFn, read.getGetOffsetFn());
+    assertEquals(receiverBuilder, read.getSparkReceiverBuilder());
+    assertEquals(String.class, read.getValueClass());
+  }
+
+  @Test
+  public void testReadObjectCreationFailsIfReceiverBuilderIsNull() {
+    assertThrows(
+        IllegalArgumentException.class,
+        () -> SparkReceiverIO.<String>read().withSparkReceiverBuilder(null));
+  }
+
+  @Test
+  public void testReadObjectCreationFailsIfGetOffsetFnIsNull() {
+    assertThrows(
+        IllegalArgumentException.class, () -> SparkReceiverIO.<String>read().withGetOffsetFn(null));
+  }
+
+  @Test
+  public void testReadObjectCreationFailsIfWatermarkFnIsNull() {
+    assertThrows(
+        IllegalArgumentException.class, () -> SparkReceiverIO.<String>read().withWatermarkFn(null));
+  }
+
+  @Test
+  public void testReadObjectCreationFailsIfValueClassIsNull() {
+    assertThrows(
+        IllegalArgumentException.class, () -> SparkReceiverIO.<String>read().withValueClass(null));
+  }
+
+  @Test
+  public void testReadValidationFailsMissingReceiverBuilder() {
+    SparkReceiverIO.Read<String> read = SparkReceiverIO.read();
+    assertThrows(IllegalStateException.class, read::validateTransform);
+  }
+
+  @Test
+  public void testReadValidationFailsMissingSparkConsumer() {
+    ReceiverBuilder<String, CustomReceiverWithOffset> receiverBuilder =
+        new ReceiverBuilder<>(CustomReceiverWithOffset.class).withConstructorArgs();
+    SparkReceiverIO.Read<String> read =
+        SparkReceiverIO.<String>read().withSparkReceiverBuilder(receiverBuilder);
+    assertThrows(IllegalStateException.class, read::validateTransform);
+  }
+
+  @Test
+  public void testReadFromCustomReceiverWithOffset() {
+    DirectOptions options = PipelineOptionsFactory.as(DirectOptions.class);
+    options.setBlockOnRun(false);
+    options.setRunner(DirectRunner.class);

Review Comment:
   Removed



##########
sdks/java/io/sparkreceiver/src/test/java/org/apache/beam/sdk/io/sparkreceiver/SparkReceiverIOTest.java:
##########
@@ -0,0 +1,125 @@
+/*
+ * 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.io.sparkreceiver;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertThrows;
+
+import java.util.List;
+import org.apache.beam.runners.direct.DirectOptions;
+import org.apache.beam.runners.direct.DirectRunner;
+import org.apache.beam.sdk.Pipeline;
+import org.apache.beam.sdk.coders.StringUtf8Coder;
+import org.apache.beam.sdk.options.PipelineOptionsFactory;
+import org.apache.beam.sdk.transforms.ParDo;
+import org.apache.beam.sdk.transforms.SerializableFunction;
+import org.joda.time.Duration;
+import org.joda.time.Instant;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.junit.runners.JUnit4;
+
+/** Test class for {@link SparkReceiverIO}. */
+@RunWith(JUnit4.class)
+public class SparkReceiverIOTest {
+
+  @Test
+  public void testReadBuildsCorrectly() {
+    ReceiverBuilder<String, CustomReceiverWithOffset> receiverBuilder =
+        new ReceiverBuilder<>(CustomReceiverWithOffset.class).withConstructorArgs();
+    SerializableFunction<String, Long> offsetFn = Long::valueOf;
+    SerializableFunction<String, Instant> watermarkFn = Instant::parse;
+
+    SparkReceiverIO.Read<String> read =
+        SparkReceiverIO.<String>read()
+            .withValueClass(String.class)
+            .withGetOffsetFn(offsetFn)
+            .withWatermarkFn(watermarkFn)
+            .withSparkReceiverBuilder(receiverBuilder);
+
+    assertEquals(offsetFn, read.getGetOffsetFn());
+    assertEquals(receiverBuilder, read.getSparkReceiverBuilder());
+    assertEquals(String.class, read.getValueClass());
+  }
+
+  @Test
+  public void testReadObjectCreationFailsIfReceiverBuilderIsNull() {
+    assertThrows(
+        IllegalArgumentException.class,
+        () -> SparkReceiverIO.<String>read().withSparkReceiverBuilder(null));
+  }
+
+  @Test
+  public void testReadObjectCreationFailsIfGetOffsetFnIsNull() {
+    assertThrows(
+        IllegalArgumentException.class, () -> SparkReceiverIO.<String>read().withGetOffsetFn(null));
+  }
+
+  @Test
+  public void testReadObjectCreationFailsIfWatermarkFnIsNull() {
+    assertThrows(
+        IllegalArgumentException.class, () -> SparkReceiverIO.<String>read().withWatermarkFn(null));
+  }
+
+  @Test
+  public void testReadObjectCreationFailsIfValueClassIsNull() {
+    assertThrows(
+        IllegalArgumentException.class, () -> SparkReceiverIO.<String>read().withValueClass(null));
+  }
+
+  @Test
+  public void testReadValidationFailsMissingReceiverBuilder() {
+    SparkReceiverIO.Read<String> read = SparkReceiverIO.read();
+    assertThrows(IllegalStateException.class, read::validateTransform);
+  }
+
+  @Test
+  public void testReadValidationFailsMissingSparkConsumer() {
+    ReceiverBuilder<String, CustomReceiverWithOffset> receiverBuilder =
+        new ReceiverBuilder<>(CustomReceiverWithOffset.class).withConstructorArgs();
+    SparkReceiverIO.Read<String> read =
+        SparkReceiverIO.<String>read().withSparkReceiverBuilder(receiverBuilder);
+    assertThrows(IllegalStateException.class, read::validateTransform);
+  }
+
+  @Test
+  public void testReadFromCustomReceiverWithOffset() {
+    DirectOptions options = PipelineOptionsFactory.as(DirectOptions.class);
+    options.setBlockOnRun(false);
+    options.setRunner(DirectRunner.class);
+    Pipeline p = Pipeline.create(options);

Review Comment:
   Done



##########
sdks/java/io/sparkreceiver/src/test/java/org/apache/beam/sdk/io/sparkreceiver/SparkReceiverIOTest.java:
##########
@@ -0,0 +1,125 @@
+/*
+ * 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.io.sparkreceiver;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertThrows;
+
+import java.util.List;
+import org.apache.beam.runners.direct.DirectOptions;
+import org.apache.beam.runners.direct.DirectRunner;
+import org.apache.beam.sdk.Pipeline;
+import org.apache.beam.sdk.coders.StringUtf8Coder;
+import org.apache.beam.sdk.options.PipelineOptionsFactory;
+import org.apache.beam.sdk.transforms.ParDo;
+import org.apache.beam.sdk.transforms.SerializableFunction;
+import org.joda.time.Duration;
+import org.joda.time.Instant;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.junit.runners.JUnit4;
+
+/** Test class for {@link SparkReceiverIO}. */
+@RunWith(JUnit4.class)
+public class SparkReceiverIOTest {
+
+  @Test
+  public void testReadBuildsCorrectly() {
+    ReceiverBuilder<String, CustomReceiverWithOffset> receiverBuilder =
+        new ReceiverBuilder<>(CustomReceiverWithOffset.class).withConstructorArgs();
+    SerializableFunction<String, Long> offsetFn = Long::valueOf;
+    SerializableFunction<String, Instant> watermarkFn = Instant::parse;
+
+    SparkReceiverIO.Read<String> read =
+        SparkReceiverIO.<String>read()
+            .withValueClass(String.class)
+            .withGetOffsetFn(offsetFn)
+            .withWatermarkFn(watermarkFn)
+            .withSparkReceiverBuilder(receiverBuilder);
+
+    assertEquals(offsetFn, read.getGetOffsetFn());
+    assertEquals(receiverBuilder, read.getSparkReceiverBuilder());
+    assertEquals(String.class, read.getValueClass());
+  }
+
+  @Test
+  public void testReadObjectCreationFailsIfReceiverBuilderIsNull() {
+    assertThrows(
+        IllegalArgumentException.class,
+        () -> SparkReceiverIO.<String>read().withSparkReceiverBuilder(null));
+  }
+
+  @Test
+  public void testReadObjectCreationFailsIfGetOffsetFnIsNull() {
+    assertThrows(
+        IllegalArgumentException.class, () -> SparkReceiverIO.<String>read().withGetOffsetFn(null));
+  }
+
+  @Test
+  public void testReadObjectCreationFailsIfWatermarkFnIsNull() {
+    assertThrows(
+        IllegalArgumentException.class, () -> SparkReceiverIO.<String>read().withWatermarkFn(null));
+  }
+
+  @Test
+  public void testReadObjectCreationFailsIfValueClassIsNull() {
+    assertThrows(
+        IllegalArgumentException.class, () -> SparkReceiverIO.<String>read().withValueClass(null));
+  }
+
+  @Test
+  public void testReadValidationFailsMissingReceiverBuilder() {
+    SparkReceiverIO.Read<String> read = SparkReceiverIO.read();
+    assertThrows(IllegalStateException.class, read::validateTransform);
+  }
+
+  @Test
+  public void testReadValidationFailsMissingSparkConsumer() {
+    ReceiverBuilder<String, CustomReceiverWithOffset> receiverBuilder =
+        new ReceiverBuilder<>(CustomReceiverWithOffset.class).withConstructorArgs();
+    SparkReceiverIO.Read<String> read =
+        SparkReceiverIO.<String>read().withSparkReceiverBuilder(receiverBuilder);
+    assertThrows(IllegalStateException.class, read::validateTransform);
+  }
+
+  @Test
+  public void testReadFromCustomReceiverWithOffset() {
+    DirectOptions options = PipelineOptionsFactory.as(DirectOptions.class);
+    options.setBlockOnRun(false);
+    options.setRunner(DirectRunner.class);
+    Pipeline p = Pipeline.create(options);
+
+    ReceiverBuilder<String, CustomReceiverWithOffset> receiverBuilder =
+        new ReceiverBuilder<>(CustomReceiverWithOffset.class).withConstructorArgs();
+    SparkReceiverIO.Read<String> reader =
+        SparkReceiverIO.<String>read()
+            .withValueClass(String.class)
+            .withGetOffsetFn(Long::valueOf)
+            .withWatermarkFn(Instant::parse)
+            .withSparkReceiverBuilder(receiverBuilder);
+
+    List<String> storedRecords = CustomReceiverWithOffset.getStoredRecords();
+    List<String> outputRecords = TestOutputDoFn.getRecords();
+    outputRecords.clear();
+
+    p.apply(reader).setCoder(StringUtf8Coder.of()).apply(ParDo.of(new TestOutputDoFn()));
+    p.run().waitUntilFinish(Duration.standardSeconds(15));
+
+    assertEquals(outputRecords, storedRecords);

Review Comment:
   Done



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

To unsubscribe, e-mail: github-unsubscribe@beam.apache.org

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