Skip to content

Commit 85290bf

Browse files
authored
feat: add node sample for cloud composer tutorial (GoogleCloudPlatform#1708)
* feat: add node sample for cloud composer tutorial
1 parent 30f851a commit 85290bf

File tree

5 files changed

+93
-0
lines changed

5 files changed

+93
-0
lines changed

CODEOWNERS

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ datastore/functions @benwhitehead
2727

2828
# One-offs
2929
cloud-sql @kurtisvg
30+
composer @leahecole @sofisl
3031
healthcare @noerog
3132
iot @gguuss
3233
jobs @happyhuman
34+

composer/README.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# [Node sample for Cloud Composer-Dataflow Tutorial][tutorial-link]
2+
3+
This is the code for the transform CSV-to-JSON code sample for the [Using the DataflowTemplateOperator][tutorial-link] tutorial.
4+
5+
This tutorial shows how to use the DataflowTemplateOperator to launch Dataflow Pipelines from Cloud Composer. The Cloud Storage Text to BigQuery (Stream) pipeline is a streaming pipeline that allows you to stream text files stored in Cloud Storage, transform them using a JavaScript User Defined Function (UDF) that you provide, and output the results to BigQuery. We'll use Cloud Composer to kick off a Cloud Dataflow pipeline that will apply the User-Defined Function to our .txt file and format it according to the JSON schema.
6+
7+
The code held in this repo is a User Defined Function written in JavaScript that will transform each line of a .txt file into the relevant columns for a BigQuery table.
8+
9+
[tutorial-link]: https://cloud.devsite.corp.google.com/composer/docs/how-to/using/using-dataflow-template-operator?auto_signin=false
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/* eslint-disable func-style */
2+
// Copyright 2020 Google LLC
3+
//
4+
// Licensed under the Apache License, Version 2.0 (the "License");
5+
// you may not use this file except in compliance with the License.
6+
// You may obtain a copy of the License at
7+
//
8+
// http://www.apache.org/licenses/LICENSE-2.0
9+
//
10+
// Unless required by applicable law or agreed to in writing, software
11+
// distributed under the License is distributed on an "AS IS" BASIS,
12+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
// See the License for the specific language governing permissions and
14+
// limitations under the License.
15+
16+
module.exports = function main(
17+
line = 'tampa, 106, january, null, null, 08-17-2019'
18+
) {
19+
// [START composer_transform_csv_to_json]
20+
21+
function transformCSVtoJSON(line) {
22+
const values = line.split(',');
23+
const properties = [
24+
'location',
25+
'average_temperature',
26+
'month',
27+
'inches_of_rain',
28+
'is_current',
29+
'latest_measurement',
30+
];
31+
const weatherInCity = {};
32+
33+
for (let count = 0; count < values.length; count++) {
34+
if (values[count] !== 'null') {
35+
weatherInCity[properties[count]] = values[count];
36+
}
37+
}
38+
39+
const jsonString = JSON.stringify(weatherInCity);
40+
return jsonString;
41+
}
42+
43+
transformCSVtoJSON(line);
44+
// [END composer_transform_csv_to_json]
45+
return transformCSVtoJSON(line);
46+
};

composer/package.json

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
{
2+
"name": "nodejs-docs-samples-composer",
3+
"description": "Node.js Google Composer sample.",
4+
"version": "0.0.1",
5+
"private": true,
6+
"license": "Apache-2.0",
7+
"author": "Google Inc.",
8+
"repository": {
9+
"type": "git",
10+
"url": "https://github.com/GoogleCloudPlatform/nodejs-docs-samples.git"
11+
},
12+
"engines": {
13+
"node": ">=8.0.0"
14+
},
15+
"scripts": {
16+
"test": "mocha --exit test/*.test.js"
17+
},
18+
"devDependencies": {
19+
"chai": "^4.2.0",
20+
"mocha": "^7.0.0"
21+
}
22+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
const {assert} = require('chai');
2+
const tutorial = require('../composer_transform_csv_to_json.js');
3+
4+
const lineTest = 'tampa, 106, january, null, null, 08-17-2019';
5+
6+
it('should separate out a line and turn into a json string', () => {
7+
const output = tutorial(lineTest);
8+
assert.match(
9+
output,
10+
new RegExp(
11+
'{"location":"tampa","average_temperature":" 106","month":" january","inches_of_rain":" null","is_current":" null","latest_measurement":" 08-17-2019"}'
12+
)
13+
);
14+
});

0 commit comments

Comments
 (0)