com.google.cloud.dataflow.sdk.coders.StringUtf8Coder Java Examples

The following examples show how to use com.google.cloud.dataflow.sdk.coders.StringUtf8Coder. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: RemoveDuplicatesITCase.java    From flink-dataflow with Apache License 2.0 6 votes vote down vote up
@Override
protected void testProgram() throws Exception {

	List<String> strings = Arrays.asList("k1", "k5", "k5", "k2", "k1", "k2", "k3");

	Pipeline p = FlinkTestPipeline.createForBatch();

	PCollection<String> input =
			p.apply(Create.of(strings))
					.setCoder(StringUtf8Coder.of());

	PCollection<String> output =
			input.apply(RemoveDuplicates.<String>create());

	output.apply(TextIO.Write.to(resultPath));
	p.run();
}
 
Example #2
Source File: RemoveDuplicatesEmptyITCase.java    From flink-dataflow with Apache License 2.0 6 votes vote down vote up
@Override
protected void testProgram() throws Exception {

	List<String> strings = Collections.emptyList();

	Pipeline p = FlinkTestPipeline.createForBatch();

	PCollection<String> input =
			p.apply(Create.of(strings))
					.setCoder(StringUtf8Coder.of());

	PCollection<String> output =
			input.apply(RemoveDuplicates.<String>create());

	output.apply(TextIO.Write.to(resultPath));
	p.run();
}
 
Example #3
Source File: WriteSinkITCase.java    From flink-dataflow with Apache License 2.0 5 votes vote down vote up
private static void runProgram(String resultPath) {
	Pipeline p = FlinkTestPipeline.createForBatch();

	p.apply(Create.of(EXPECTED_RESULT)).setCoder(StringUtf8Coder.of())
		.apply("CustomSink", Write.to(new MyCustomSink(resultPath)));

	p.run();
}
 
Example #4
Source File: WordCountITCase.java    From flink-dataflow with Apache License 2.0 5 votes vote down vote up
@Override
protected void testProgram() throws Exception {

	Pipeline p = FlinkTestPipeline.createForBatch();

	PCollection<String> input = p.apply(Create.of(WORDS)).setCoder(StringUtf8Coder.of());

	input
			.apply(new WordCount.CountWords())
			.apply(MapElements.via(new WordCount.FormatAsTextFn()))
			.apply(TextIO.Write.to(resultPath));

	p.run();
}
 
Example #5
Source File: GCSFilesSource.java    From policyscanner with Apache License 2.0 4 votes vote down vote up
/**
 * Get the default coder to use for this source's output.
 * @return The default coder to use for this source's output.
 */
@Override
public Coder<KV<List<String>, String>> getDefaultOutputCoder() {
  return KvCoder.of(ListCoder.of(StringUtf8Coder.of()), StringUtf8Coder.of());
}
 
Example #6
Source File: TFIDF.java    From flink-dataflow with Apache License 2.0 4 votes vote down vote up
@Override
public Coder<?> getDefaultOutputCoder() {
	return KvCoder.of(StringDelegateCoder.of(URI.class), StringUtf8Coder.of());
}
 
Example #7
Source File: WriteSinkITCase.java    From flink-dataflow with Apache License 2.0 4 votes vote down vote up
@Override
public Coder<String> getWriterResultCoder() {
	return StringUtf8Coder.of();
}
 
Example #8
Source File: GroupAlsoByWindowTest.java    From flink-dataflow with Apache License 2.0 4 votes vote down vote up
@Test
public void testWithLateness() throws Exception {
	WindowingStrategy strategy = WindowingStrategy.of(FixedWindows.of(Duration.standardSeconds(2)))
			.withMode(WindowingStrategy.AccumulationMode.ACCUMULATING_FIRED_PANES)
			.withAllowedLateness(Duration.millis(1000));
	long initialTime = 0L;
	Pipeline pipeline = FlinkTestPipeline.createForStreaming();

	KvCoder<String, Integer> inputCoder = KvCoder.of(StringUtf8Coder.of(), VarIntCoder.of());

	FlinkGroupAlsoByWindowWrapper gbwOperaror =
			FlinkGroupAlsoByWindowWrapper.createForTesting(
					pipeline.getOptions(),
					pipeline.getCoderRegistry(),
					strategy,
					inputCoder,
					combiner.<String>asKeyedFn());

	OneInputStreamOperatorTestHarness<WindowedValue<KV<String, Integer>>, WindowedValue<KV<String, Integer>>> testHarness =
			new OneInputStreamOperatorTestHarness<>(gbwOperaror);
	testHarness.open();

	testHarness.processElement(new StreamRecord<>(makeWindowedValue(strategy, KV.of("key1", 1), new Instant(initialTime + 1), null, PaneInfo.NO_FIRING), initialTime + 20));
	testHarness.processElement(new StreamRecord<>(makeWindowedValue(strategy, KV.of("key1", 1), new Instant(initialTime + 1), null, PaneInfo.NO_FIRING), initialTime + 20));
	testHarness.processElement(new StreamRecord<>(makeWindowedValue(strategy, KV.of("key1", 1), new Instant(initialTime + 1000), null, PaneInfo.NO_FIRING), initialTime + 20));
	testHarness.processElement(new StreamRecord<>(makeWindowedValue(strategy, KV.of("key1", 1), new Instant(initialTime + 1200), null, PaneInfo.NO_FIRING), initialTime + 20));
	testHarness.processWatermark(new Watermark(initialTime + 2000));
	testHarness.processElement(new StreamRecord<>(makeWindowedValue(strategy, KV.of("key1", 1), new Instant(initialTime + 1200), null, PaneInfo.NO_FIRING), initialTime + 20));
	testHarness.processElement(new StreamRecord<>(makeWindowedValue(strategy, KV.of("key1", 1), new Instant(initialTime + 1200), null, PaneInfo.NO_FIRING), initialTime + 20));
	testHarness.processWatermark(new Watermark(initialTime + 4000));

	ConcurrentLinkedQueue<Object> expectedOutput = new ConcurrentLinkedQueue<>();
	expectedOutput.add(new StreamRecord<>(
			WindowedValue.of(KV.of("key1", 4),
					new Instant(initialTime + 1),
					new IntervalWindow(new Instant(0), new Instant(2000)),
					PaneInfo.createPane(true, false, PaneInfo.Timing.ON_TIME, 0, 0))
			, initialTime + 1));
	expectedOutput.add(new Watermark(initialTime + 2000));

	expectedOutput.add(new StreamRecord<>(
			WindowedValue.of(KV.of("key1", 5),
					new Instant(initialTime + 1999),
					new IntervalWindow(new Instant(0), new Instant(2000)),
					PaneInfo.createPane(false, false, PaneInfo.Timing.LATE, 1, 1))
			, initialTime + 1999));


	expectedOutput.add(new StreamRecord<>(
			WindowedValue.of(KV.of("key1", 6),
					new Instant(initialTime + 1999),
					new IntervalWindow(new Instant(0), new Instant(2000)),
					PaneInfo.createPane(false, false, PaneInfo.Timing.LATE, 2, 2))
			, initialTime + 1999));
	expectedOutput.add(new Watermark(initialTime + 4000));

	TestHarnessUtil.assertOutputEqualsSorted("Output was not correct.", expectedOutput, testHarness.getOutput(), new ResultSortComparator());
	testHarness.close();
}
 
Example #9
Source File: GroupAlsoByWindowTest.java    From flink-dataflow with Apache License 2.0 4 votes vote down vote up
@Test
public void testSessionWindows() throws Exception {
	WindowingStrategy strategy = sessionWindowingStrategy;

	long initialTime = 0L;
	Pipeline pipeline = FlinkTestPipeline.createForStreaming();

	KvCoder<String, Integer> inputCoder = KvCoder.of(StringUtf8Coder.of(), VarIntCoder.of());

	FlinkGroupAlsoByWindowWrapper gbwOperaror =
			FlinkGroupAlsoByWindowWrapper.createForTesting(
					pipeline.getOptions(),
					pipeline.getCoderRegistry(),
					strategy,
					inputCoder,
					combiner.<String>asKeyedFn());

	OneInputStreamOperatorTestHarness<WindowedValue<KV<String, Integer>>, WindowedValue<KV<String, Integer>>> testHarness =
			new OneInputStreamOperatorTestHarness<>(gbwOperaror);
	testHarness.open();

	testHarness.processElement(new StreamRecord<>(makeWindowedValue(strategy, KV.of("key1", 1), new Instant(initialTime + 1), null, PaneInfo.NO_FIRING), initialTime + 20));
	testHarness.processElement(new StreamRecord<>(makeWindowedValue(strategy, KV.of("key1", 1), new Instant(initialTime + 1), null, PaneInfo.NO_FIRING), initialTime + 20));
	testHarness.processElement(new StreamRecord<>(makeWindowedValue(strategy, KV.of("key1", 1), new Instant(initialTime + 1000), null, PaneInfo.NO_FIRING), initialTime + 20));
	testHarness.processElement(new StreamRecord<>(makeWindowedValue(strategy, KV.of("key1", 1), new Instant(initialTime + 3500), null, PaneInfo.NO_FIRING), initialTime + 20));
	testHarness.processElement(new StreamRecord<>(makeWindowedValue(strategy, KV.of("key1", 1), new Instant(initialTime + 3700), null, PaneInfo.NO_FIRING), initialTime + 20));
	testHarness.processElement(new StreamRecord<>(makeWindowedValue(strategy, KV.of("key1", 1), new Instant(initialTime + 2700), null, PaneInfo.NO_FIRING), initialTime + 20));
	testHarness.processWatermark(new Watermark(initialTime + 6000));

	testHarness.processElement(new StreamRecord<>(makeWindowedValue(strategy, KV.of("key1", 1), new Instant(initialTime + 6700), null, PaneInfo.NO_FIRING), initialTime + 20));
	testHarness.processElement(new StreamRecord<>(makeWindowedValue(strategy, KV.of("key1", 1), new Instant(initialTime + 6800), null, PaneInfo.NO_FIRING), initialTime + 20));
	testHarness.processElement(new StreamRecord<>(makeWindowedValue(strategy, KV.of("key1", 1), new Instant(initialTime + 8900), null, PaneInfo.NO_FIRING), initialTime + 20));
	testHarness.processElement(new StreamRecord<>(makeWindowedValue(strategy, KV.of("key1", 1), new Instant(initialTime + 7600), null, PaneInfo.NO_FIRING), initialTime + 20));
	testHarness.processElement(new StreamRecord<>(makeWindowedValue(strategy, KV.of("key1", 1), new Instant(initialTime + 5600), null, PaneInfo.NO_FIRING), initialTime + 20));

	testHarness.processWatermark(new Watermark(initialTime + 12000));

	ConcurrentLinkedQueue<Object> expectedOutput = new ConcurrentLinkedQueue<>();
	expectedOutput.add(new StreamRecord<>(
			WindowedValue.of(KV.of("key1", 6),
					new Instant(initialTime + 1),
					new IntervalWindow(new Instant(1), new Instant(5700)),
					PaneInfo.createPane(true, false, PaneInfo.Timing.ON_TIME, 0, 0))
			, initialTime + 1));
	expectedOutput.add(new Watermark(initialTime + 6000));

	expectedOutput.add(new StreamRecord<>(
			WindowedValue.of(KV.of("key1", 11),
					new Instant(initialTime + 6700),
					new IntervalWindow(new Instant(1), new Instant(10900)),
					PaneInfo.createPane(true, false, PaneInfo.Timing.ON_TIME, 0, 0))
			, initialTime + 6700));
	expectedOutput.add(new Watermark(initialTime + 12000));

	TestHarnessUtil.assertOutputEqualsSorted("Output was not correct.", expectedOutput, testHarness.getOutput(), new ResultSortComparator());
	testHarness.close();
}
 
Example #10
Source File: GroupAlsoByWindowTest.java    From flink-dataflow with Apache License 2.0 4 votes vote down vote up
private OneInputStreamOperatorTestHarness createTestingOperatorAndState(WindowingStrategy strategy, long initialTime) throws Exception {
	Pipeline pipeline = FlinkTestPipeline.createForStreaming();

	KvCoder<String, Integer> inputCoder = KvCoder.of(StringUtf8Coder.of(), VarIntCoder.of());

	FlinkGroupAlsoByWindowWrapper gbwOperaror =
			FlinkGroupAlsoByWindowWrapper.createForTesting(
					pipeline.getOptions(),
					pipeline.getCoderRegistry(),
					strategy,
					inputCoder,
					combiner.<String>asKeyedFn());

	OneInputStreamOperatorTestHarness<WindowedValue<KV<String, Integer>>, WindowedValue<KV<String, Integer>>> testHarness =
			new OneInputStreamOperatorTestHarness<>(gbwOperaror);
	testHarness.open();

	testHarness.processElement(new StreamRecord<>(makeWindowedValue(strategy, KV.of("key1", 1), new Instant(initialTime + 1), null, PaneInfo.NO_FIRING), initialTime + 20));
	testHarness.processElement(new StreamRecord<>(makeWindowedValue(strategy, KV.of("key1", 1), new Instant(initialTime + 1), null, PaneInfo.NO_FIRING), initialTime + 20));
	testHarness.processElement(new StreamRecord<>(makeWindowedValue(strategy, KV.of("key1", 1), new Instant(initialTime + 1000), null, PaneInfo.NO_FIRING), initialTime + 20));
	testHarness.processElement(new StreamRecord<>(makeWindowedValue(strategy, KV.of("key1", 1), new Instant(initialTime + 1200), null, PaneInfo.NO_FIRING), initialTime + 20));
	testHarness.processElement(new StreamRecord<>(makeWindowedValue(strategy, KV.of("key1", 1), new Instant(initialTime + 1200), null, PaneInfo.NO_FIRING), initialTime + 20));
	testHarness.processElement(new StreamRecord<>(makeWindowedValue(strategy, KV.of("key1", 1), new Instant(initialTime + 1200), null, PaneInfo.NO_FIRING), initialTime + 20));

	testHarness.processElement(new StreamRecord<>(makeWindowedValue(strategy, KV.of("key1", 1), new Instant(initialTime + 10000), null, PaneInfo.NO_FIRING), initialTime + 20));
	testHarness.processElement(new StreamRecord<>(makeWindowedValue(strategy, KV.of("key1", 1), new Instant(initialTime + 12100), null, PaneInfo.NO_FIRING), initialTime + 20));
	testHarness.processElement(new StreamRecord<>(makeWindowedValue(strategy, KV.of("key1", 1), new Instant(initialTime + 14200), null, PaneInfo.NO_FIRING), initialTime + 20));
	testHarness.processElement(new StreamRecord<>(makeWindowedValue(strategy, KV.of("key1", 1), new Instant(initialTime + 15300), null, PaneInfo.NO_FIRING), initialTime + 20));
	testHarness.processElement(new StreamRecord<>(makeWindowedValue(strategy, KV.of("key1", 1), new Instant(initialTime + 16500), null, PaneInfo.NO_FIRING), initialTime + 20));
	testHarness.processElement(new StreamRecord<>(makeWindowedValue(strategy, KV.of("key1", 1), new Instant(initialTime + 19500), null, PaneInfo.NO_FIRING), initialTime + 20));
	testHarness.processElement(new StreamRecord<>(makeWindowedValue(strategy, KV.of("key1", 1), new Instant(initialTime + 19500), null, PaneInfo.NO_FIRING), initialTime + 20));
	testHarness.processElement(new StreamRecord<>(makeWindowedValue(strategy, KV.of("key1", 1), new Instant(initialTime + 19500), null, PaneInfo.NO_FIRING), initialTime + 20));
	testHarness.processElement(new StreamRecord<>(makeWindowedValue(strategy, KV.of("key1", 1), new Instant(initialTime + 19500), null, PaneInfo.NO_FIRING), initialTime + 20));
	testHarness.processElement(new StreamRecord<>(makeWindowedValue(strategy, KV.of("key1", 1), new Instant(initialTime + 19500), null, PaneInfo.NO_FIRING), initialTime + 20));
	testHarness.processElement(new StreamRecord<>(makeWindowedValue(strategy, KV.of("key1", 1), new Instant(initialTime + 19500), null, PaneInfo.NO_FIRING), initialTime + 20));

	testHarness.processElement(new StreamRecord<>(makeWindowedValue(strategy, KV.of("key2", 1), new Instant(initialTime + 19500), null, PaneInfo.NO_FIRING), initialTime + 20));

	testHarness.processWatermark(new Watermark(initialTime + 10000));
	testHarness.processWatermark(new Watermark(initialTime + 20000));

	return testHarness;
}
 
Example #11
Source File: CoinbaseSource.java    From cloud-bigtable-examples with Apache License 2.0 4 votes vote down vote up
@Override
public Coder<String> getDefaultOutputCoder() {
  return StringUtf8Coder.of();
}