forked from GoogleCloudPlatform/DataflowJavaSDK
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Demonstrate new lambda-friendly transforms in MinimalWordCount
Java 8 lambda enables some more concise expression of word count via Filter, MapElements, and FlatMapElements transforms. This change adds a Java 8 profile to our examples module and includes in it a concise version of the MinimalWordCount example. The Java 7 MinimalWordCount example is also updated to reflect the use of these transforms without a lambda. ----Release Notes---- [] ------------- Created by MOE: https://github.com/google/moe MOE_MIGRATED_REVID=103782162
- Loading branch information
1 parent
3315791
commit 7d18d58
Showing
4 changed files
with
257 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
68 changes: 68 additions & 0 deletions
68
examples/src/main/java8/com/google/cloud/dataflow/examples/MinimalWordCountJava8.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
/* | ||
* Copyright (C) 2015 Google Inc. | ||
* | ||
* Licensed 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 com.google.cloud.dataflow.examples; | ||
|
||
import com.google.cloud.dataflow.sdk.Pipeline; | ||
import com.google.cloud.dataflow.sdk.io.TextIO; | ||
import com.google.cloud.dataflow.sdk.options.DataflowPipelineOptions; | ||
import com.google.cloud.dataflow.sdk.options.PipelineOptionsFactory; | ||
import com.google.cloud.dataflow.sdk.runners.BlockingDataflowPipelineRunner; | ||
import com.google.cloud.dataflow.sdk.transforms.Count; | ||
import com.google.cloud.dataflow.sdk.transforms.Filter; | ||
import com.google.cloud.dataflow.sdk.transforms.FlatMapElements; | ||
import com.google.cloud.dataflow.sdk.transforms.MapElements; | ||
import com.google.cloud.dataflow.sdk.values.KV; | ||
import com.google.cloud.dataflow.sdk.values.TypeDescriptor; | ||
|
||
import java.util.Arrays; | ||
|
||
/** | ||
* An example that counts words in Shakespeare, using Java 8 language features. | ||
* | ||
* <p>See {@link MinimalWordCount} for a comprehensive explanation. | ||
*/ | ||
public class MinimalWordCountJava8 { | ||
|
||
public static void main(String[] args) { | ||
DataflowPipelineOptions options = PipelineOptionsFactory.create() | ||
.as(DataflowPipelineOptions.class); | ||
|
||
options.setRunner(BlockingDataflowPipelineRunner.class); | ||
|
||
// CHANGE 1 of 3: Your project ID is required in order to run your pipeline on the Google Cloud. | ||
options.setProject("SET_YOUR_PROJECT_ID_HERE"); | ||
|
||
// CHANGE 2 of 3: Your Google Cloud Storage path is required for staging local files. | ||
options.setStagingLocation("gs://SET_YOUR_BUCKET_NAME_HERE/AND_STAGING_DIRECTORY"); | ||
|
||
Pipeline p = Pipeline.create(options); | ||
|
||
p.apply(TextIO.Read.from("gs://dataflow-samples/shakespeare/*")) | ||
.apply(FlatMapElements.via((String word) -> Arrays.asList(word.split("[^a-zA-Z']+"))) | ||
.withOutputType(new TypeDescriptor<String>() {})) | ||
.apply(Filter.byPredicate((String word) -> !word.isEmpty())) | ||
.apply(Count.<String>perElement()) | ||
.apply(MapElements | ||
.via((KV<String, Long> wordCount) -> wordCount.getKey() + ": " + wordCount.getValue()) | ||
.withOutputType(new TypeDescriptor<String>() {})) | ||
|
||
// CHANGE 3 of 3: The Google Cloud Storage path is required for outputting the results to. | ||
.apply(TextIO.Write.to("gs://YOUR_OUTPUT_BUCKET/AND_OUTPUT_PREFIX")); | ||
|
||
p.run(); | ||
} | ||
} |
60 changes: 60 additions & 0 deletions
60
examples/src/test/java8/com/google/cloud/dataflow/examples/MinimalWordCountJava8Test.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
/* | ||
* Copyright (C) 2015 Google Inc. | ||
* | ||
* Licensed 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 com.google.cloud.dataflow.examples; | ||
|
||
import com.google.cloud.dataflow.sdk.Pipeline; | ||
import com.google.cloud.dataflow.sdk.io.TextIO; | ||
import com.google.cloud.dataflow.sdk.testing.TestPipeline; | ||
import com.google.cloud.dataflow.sdk.transforms.Count; | ||
import com.google.cloud.dataflow.sdk.transforms.Filter; | ||
import com.google.cloud.dataflow.sdk.transforms.FlatMapElements; | ||
import com.google.cloud.dataflow.sdk.transforms.MapElements; | ||
import com.google.cloud.dataflow.sdk.values.KV; | ||
import com.google.cloud.dataflow.sdk.values.TypeDescriptor; | ||
|
||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
import org.junit.runners.JUnit4; | ||
|
||
import java.io.Serializable; | ||
import java.util.Arrays; | ||
|
||
/** | ||
* To keep {@link MinimalWordCountJava8} simple, it is not factored or testable. This test | ||
* file should be maintained with a copy of its code for a basic smoke test. | ||
*/ | ||
@RunWith(JUnit4.class) | ||
public class MinimalWordCountJava8Test implements Serializable { | ||
|
||
/** | ||
* A basic smoke test that ensures there is no crash at pipeline construction time. | ||
*/ | ||
@Test | ||
public void testMinimalWordCountJava8() { | ||
Pipeline p = TestPipeline.create(); | ||
|
||
p.apply(TextIO.Read.from("gs://dataflow-samples/shakespeare/*")) | ||
.apply(FlatMapElements.via((String word) -> Arrays.asList(word.split("[^a-zA-Z']+"))) | ||
.withOutputType(new TypeDescriptor<String>() {})) | ||
.apply(Filter.byPredicate((String word) -> !word.isEmpty())) | ||
.apply(Count.<String>perElement()) | ||
.apply(MapElements | ||
.via((KV<String, Long> wordCount) -> wordCount.getKey() + ": " + wordCount.getValue()) | ||
.withOutputType(new TypeDescriptor<String>() {})) | ||
.apply(TextIO.Write.to("gs://YOUR_OUTPUT_BUCKET/AND_OUTPUT_PREFIX")); | ||
} | ||
} |