Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add node sample for cloud composer tutorial #1708

Merged
merged 13 commits into from
Apr 16, 2020
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CODEOWNERS
Validating CODEOWNERS rules …
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,7 @@ cloud-sql @kurtisvg
healthcare @noerog
iot @gguuss
jobs @happyhuman

#Composer samples
@sofisl
sofisl marked this conversation as resolved.
Show resolved Hide resolved
@leahecole
9 changes: 9 additions & 0 deletions composer/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# [Node sample for Cloud Composer-Dataflow Tutorial][tutorial-link]

This is the code for the transform CSV-to-JSON code sample for the [Using the DataflowTemplateOperator][tutorial-link] tutorial.

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.

* 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.
sofisl marked this conversation as resolved.
Show resolved Hide resolved

[tutorial-link]: https://cloud.devsite.corp.google.com/composer/docs/how-to/using/using-dataflow-template-operator?auto_signin=false
46 changes: 46 additions & 0 deletions composer/composer_transform_csv_to_json.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/* eslint-disable func-style */
fhinkel marked this conversation as resolved.
Show resolved Hide resolved
// Copyright 2020 Google LLC
//
// 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.

module.exports = function main(
line = 'tampa, 106, january, null, null, 08-17-2019'
) {
// [START composer_transform_csv_to_json]

function transformCSVtoJSON(line) {
const values = line.split(',');
const properties = [
'location',
'average_temperature',
'month',
'inches_of_rain',
'is_current',
'latest_measurement',
];
const weatherInCity = {};

for (let count = 0; count < values.length; count++) {
if (values[count] !== 'null') {
weatherInCity[properties[count]] = values[count];
}
}

const jsonString = JSON.stringify(weatherInCity);
return jsonString;
}

transformCSVtoJSON(line);
// [END composer_transform_csv_to_json]
return transformCSVtoJSON(line);
};
22 changes: 22 additions & 0 deletions composer/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
"name": "nodejs-docs-samples-composer",
"description": "Node.js Google Composer sample.",
"version": "0.0.1",
"private": true,
"license": "Apache-2.0",
"author": "Google Inc.",
"repository": {
"type": "git",
"url": "https://github.com/GoogleCloudPlatform/nodejs-docs-samples.git"
},
"engines": {
"node": ">=8.0.0"
},
"scripts": {
"test": "mocha --exit test/*.test.js"
},
"devDependencies": {
"chai": "^4.2.0",
"mocha": "^7.0.0"
}
}
14 changes: 14 additions & 0 deletions composer/test/composer_transform_csv_to_json.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
const {assert} = require('chai');
const tutorial = require('../composerJSTutorial.js');

const lineTest = 'tampa, 106, january, null, null, 08-17-2019';

it('should separate out a line and turn into a json string', () => {
const output = tutorial(lineTest);
assert.match(
output,
new RegExp(
'{"location":"tampa","average_temperature":" 106","month":" january","inches_of_rain":" null","is_current":" null","latest_measurement":" 08-17-2019"}'
)
);
});