|
| 1 | +# Cloud Dataflow Templates |
| 2 | + |
| 3 | +Samples showing how to create and run an [Apache Beam] on [Google Cloud Dataflow]. |
| 4 | + |
| 5 | +## Before you begin |
| 6 | + |
| 7 | +1. Install the [Cloud SDK]. |
| 8 | + |
| 9 | +1. [Create a new project]. |
| 10 | + |
| 11 | +1. [Enable billing]. |
| 12 | + |
| 13 | +1. [Enable the APIs](https://console.cloud.google.com/flows/enableapi?apiid=dataflow,compute_component,logging,storage_component,storage_api,bigquery,pubsub,datastore.googleapis.com,cloudresourcemanager.googleapis.com): Dataflow, Compute Engine, Stackdriver Logging, Cloud Storage, Cloud Storage JSON, BigQuery, Pub/Sub, Datastore, and Cloud Resource Manager. |
| 14 | + |
| 15 | +1. Setup the Cloud SDK to your GCP project. |
| 16 | + |
| 17 | + ```bash |
| 18 | + gcloud init |
| 19 | + ``` |
| 20 | + |
| 21 | +1. [Create a service account key] as a JSON file. |
| 22 | + For more information, see [Creating and managing service accounts]. |
| 23 | + |
| 24 | + * From the **Service account** list, select **New service account**. |
| 25 | + * In the **Service account name** field, enter a name. |
| 26 | + * From the **Role** list, select **Project > Owner**. |
| 27 | + |
| 28 | + > **Note**: The **Role** field authorizes your service account to access resources. |
| 29 | + > You can view and change this field later by using the [GCP Console IAM page]. |
| 30 | + > If you are developing a production app, specify more granular permissions than **Project > Owner**. |
| 31 | + > For more information, see [Granting roles to service accounts]. |
| 32 | +
|
| 33 | + * Click **Create**. A JSON file that contains your key downloads to your computer. |
| 34 | + |
| 35 | +1. Set your `GOOGLE_APPLICATION_CREDENTIALS` environment variable to point to your service account key file. |
| 36 | + |
| 37 | + ```bash |
| 38 | + export GOOGLE_APPLICATION_CREDENTIALS=path/to/your/credentials.json |
| 39 | + ``` |
| 40 | + |
| 41 | +1. Create a Cloud Storage bucket. |
| 42 | + |
| 43 | + ```bash |
| 44 | + gsutil mb gs://your-gcs-bucket |
| 45 | + ``` |
| 46 | + |
| 47 | +## Setup |
| 48 | + |
| 49 | +The following instructions will help you prepare your development environment. |
| 50 | + |
| 51 | +1. Download and install the [Java Development Kit (JDK)]. |
| 52 | + Verify that the [JAVA_HOME] environment variable is set and points to your JDK installation. |
| 53 | + |
| 54 | +1. Download and install [Apache Maven] by following the [Maven installation guide] for your specific operating system. |
| 55 | + |
| 56 | +1. Clone the `java-docs-samples` repository. |
| 57 | + |
| 58 | + ```bash |
| 59 | + git clone https://github.com/GoogleCloudPlatform/java-docs-samples.git |
| 60 | + ``` |
| 61 | + |
| 62 | +1. Navigate to the sample code directory. |
| 63 | + |
| 64 | + ```bash |
| 65 | + cd java-docs-samples/dataflow/templates |
| 66 | + ``` |
| 67 | + |
| 68 | +## Templates |
| 69 | + |
| 70 | +### WordCount |
| 71 | + |
| 72 | +* [WordCount.java](src/main/java/com/example/dataflow/templates/WordCount.java) |
| 73 | +* [WordCount_metadata](WordCount_metadata) |
| 74 | + |
| 75 | +First, select the project and template location. |
| 76 | + |
| 77 | +```bash |
| 78 | +PROJECT=$(gcloud config get-value project) |
| 79 | +BUCKET=your-gcs-bucket |
| 80 | +TEMPLATE_LOCATION=gs://$BUCKET/dataflow/templates/WordCount |
| 81 | +``` |
| 82 | + |
| 83 | +Then, to create the template in the desired Cloud Storage location. |
| 84 | + |
| 85 | +```bash |
| 86 | +# Create the template. |
| 87 | +mvn compile exec:java \ |
| 88 | + -Dexec.mainClass=com.example.dataflow.templates.WordCount \ |
| 89 | + -Dexec.args="\ |
| 90 | + --isCaseSensitive=false \ |
| 91 | + --project=$PROJECT \ |
| 92 | + --templateLocation=$TEMPLATE_LOCATION \ |
| 93 | + --runner=DataflowRunner" |
| 94 | +
|
| 95 | +# Upload the metadata file. |
| 96 | +gsutil cp WordCount_metadata "$TEMPLATE_LOCATION"_metadata |
| 97 | +``` |
| 98 | + |
| 99 | +> For more information, see [Creating templates]. |
| 100 | + |
| 101 | +Finally, you can run the template via `gcloud` or through the [GCP Console create Dataflow job page]. |
| 102 | + |
| 103 | +```bash |
| 104 | +JOB_NAME=wordcount-$(date +'%Y%m%d-%H%M%S') |
| 105 | +INPUT=gs://apache-beam-samples/shakespeare/kinglear.txt |
| 106 | +
|
| 107 | +gcloud dataflow jobs run $JOB_NAME \ |
| 108 | + --gcs-location $TEMPLATE_LOCATION \ |
| 109 | + --parameters inputFile=$INPUT,outputBucket=$BUCKET |
| 110 | +``` |
| 111 | + |
| 112 | +> For more information, see [Executing templates]. |
| 113 | + |
| 114 | +You can check your submitted jobs in the [GCP Console Dataflow page]. |
| 115 | + |
| 116 | +## Cleanup |
| 117 | + |
| 118 | +To avoid incurring charges to your GCP account for the resources used: |
| 119 | + |
| 120 | +```bash |
| 121 | +# Delete only the files created by this sample. |
| 122 | +gsutil -m rm -rf \ |
| 123 | + "gs://$BUCKET/dataflow/templates/WordCount*" \ |
| 124 | + "gs://$BUCKET/dataflow/wordcount/" |
| 125 | +
|
| 126 | +# [optional] Remove the entire dataflow Cloud Storage directory. |
| 127 | +gsutil -m rm -rf gs://$BUCKET/dataflow |
| 128 | +
|
| 129 | +# [optional] Remove the Cloud Storage bucket. |
| 130 | +gsutil rb gs://$BUCKET |
| 131 | +``` |
| 132 | + |
| 133 | +[Apache Beam]: https://beam.apache.org/ |
| 134 | +[Google Cloud Dataflow]: https://cloud.google.com/dataflow/docs/ |
| 135 | + |
| 136 | +[Cloud SDK]: https://cloud.google.com/sdk/docs/ |
| 137 | +[Create a new project]: https://console.cloud.google.com/projectcreate |
| 138 | +[Enable billing]: https://cloud.google.com/billing/docs/how-to/modify-project |
| 139 | +[Create a service account key]: https://console.cloud.google.com/apis/credentials/serviceaccountkey |
| 140 | +[Creating and managing service accounts]: https://cloud.google.com/iam/docs/creating-managing-service-accounts |
| 141 | +[GCP Console IAM page]: https://console.cloud.google.com/iam-admin/iam |
| 142 | +[Granting roles to service accounts]: https://cloud.google.com/iam/docs/granting-roles-to-service-accounts |
| 143 | + |
| 144 | +[Java Development Kit (JDK)]: https://www.oracle.com/technetwork/java/javase/downloads/index.html |
| 145 | +[JAVA_HOME]: https://docs.oracle.com/javase/8/docs/technotes/guides/troubleshoot/envvars001.html |
| 146 | +[Apache Maven]: http://maven.apache.org/download.cgi |
| 147 | +[Maven installation guide]: http://maven.apache.org/install.html |
| 148 | + |
| 149 | +[Creating templates]: https://cloud.google.com/dataflow/docs/guides/templates/creating-templates |
| 150 | +[GCP Console create Dataflow job page]: https://console.cloud.google.com/dataflow/createjob |
| 151 | +[Executing templates]: https://cloud.google.com/dataflow/docs/guides/templates/executing-templates |
| 152 | +[GCP Console Dataflow page]: https://console.cloud.google.com/dataflow |
0 commit comments