Skip to content

Commit

Permalink
Data Catalog: add create fileset Entry sample (#1568)
Browse files Browse the repository at this point in the history
* Add create fileset Entry sample

* Upgrade Data Catalog library

Co-authored-by: F. Hinkelmann <franziska.hinkelmann@gmail.com>
Co-authored-by: Marcelo Costa <mycelo19@gmail.com>
  • Loading branch information
3 people authored Feb 13, 2020
1 parent 6f31b84 commit c5883ca
Show file tree
Hide file tree
Showing 3 changed files with 225 additions and 0 deletions.
132 changes: 132 additions & 0 deletions datacatalog/quickstart/createFilesetEntry.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
/* eslint-disable no-warning-comments */

// Copyright 2019 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.

'use strict';

/**
* This application demonstrates how to create an Entry Group and a fileset
* Entry with the Cloud Data Catalog API.
* For more information, see the README.md under /datacatalog and the
* documentation at https://cloud.google.com/data-catalog/docs.
*/
const main = async (
projectId = process.env.GCLOUD_PROJECT,
entryGroupId,
entryId
) => {
// [START datacatalog_create_fileset_quickstart_tag]
// -------------------------------
// Import required modules.
// -------------------------------
const {DataCatalogClient} = require('@google-cloud/datacatalog').v1beta1;
const datacatalog = new DataCatalogClient();

// -------------------------------
// Currently, Data Catalog stores metadata in the
// us-central1 region.
// -------------------------------
const location = 'us-central1';

// TODO(developer): Uncomment the following lines before running the sample.
// const projectId = 'my-project'
// const entryGroupId = 'my-entry-group'
// const entryId = 'my-entry'

// -------------------------------
// 1. Create an Entry Group.
// -------------------------------
// Construct the EntryGroup for the EntryGroup request.
const entryGroup = {
displayName: 'My Fileset Entry Group',
description: 'This Entry Group consists of ...',
};

// Construct the EntryGroup request to be sent by the client.
const entryGroupRequest = {
parent: datacatalog.locationPath(projectId, location),
entryGroupId: entryGroupId,
entryGroup: entryGroup,
};

// Use the client to send the API request.
await datacatalog.createEntryGroup(entryGroupRequest);

// -------------------------------
// 2. Create a Fileset Entry.
// -------------------------------
// Construct the Entry for the Entry request.
const FILESET_TYPE = 4;

const entry = {
displayName: 'My Fileset',
description: 'This fileset consists of ...',
gcsFilesetSpec: {filePatterns: ['gs://my_bucket/*']},
schema: {
columns: [
{
column: 'city',
description: 'City',
mode: 'NULLABLE',
type: 'STRING',
},
{
column: 'state',
description: 'State',
mode: 'NULLABLE',
type: 'STRING',
},
{
column: 'addresses',
description: 'Addresses',
mode: 'REPEATED',
subcolumns: [
{
column: 'city',
description: 'City',
mode: 'NULLABLE',
type: 'STRING',
},
{
column: 'state',
description: 'State',
mode: 'NULLABLE',
type: 'STRING',
},
],
type: 'RECORD',
},
],
},
type: FILESET_TYPE,
};

// Construct the Entry request to be sent by the client.
const request = {
parent: datacatalog.entryGroupPath(projectId, location, entryGroupId),
entryId: entryId,
entry: entry,
};

// Use the client to send the API request.
const [response] = await datacatalog.createEntry(request);

console.log(response);
};
// [END datacatalog_create_fileset_quickstart_tag]

// node createFilesetEntry.js <projectId> <entryGroupId> <entryId>
main(...process.argv.slice(2));
22 changes: 22 additions & 0 deletions datacatalog/quickstart/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
"name": "@google-cloud/datacatalog-quickstart",
"version": "0.0.1",
"private": true,
"description": "Quickstart guides for the Cloud Data Catalog client library for Node.js",
"license": "Apache-2.0",
"author": "Google LLC",
"repository": "GoogleCloudPlatform/nodejs-docs-samples",
"engines": {
"node": ">=8.0.0"
},
"scripts": {
"test": "mocha system-test/*.test.js --timeout=60000"
},
"devDependencies": {
"mocha": "^6.0.0",
"uuid": "^3.1.0"
},
"dependencies": {
"@google-cloud/datacatalog": "^1.7.0"
}
}
71 changes: 71 additions & 0 deletions datacatalog/quickstart/system-test/createFilesetEntry.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
// Copyright 2019 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.

'use strict';

const path = require('path');
const assert = require('assert');
const uuid = require('uuid');
const cwd = path.join(__dirname, '..');
const {exec} = require('child_process');

const projectId = process.env.GCLOUD_PROJECT;
const location = 'us-central1';
// Use unique id to avoid conflicts between concurrent test runs
const entryGroupId = `fileset_entry_group_${uuid.v4().substr(0, 8)}`;
const entryId = `fileset_entry_id_${uuid.v4().substr(0, 8)}`;

const {DataCatalogClient} = require('@google-cloud/datacatalog').v1beta1;
const datacatalog = new DataCatalogClient();

before(() => {
assert(
process.env.GCLOUD_PROJECT,
'Must set GCLOUD_PROJECT environment variable!'
);
assert(
process.env.GOOGLE_APPLICATION_CREDENTIALS,
'Must set GOOGLE_APPLICATION_CREDENTIALS environment variable!'
);
});

describe('createFilesetEntry', () => {
it('should create a fileset entry', done => {
const expectedLinkedResource = `//datacatalog.googleapis.com/projects/${projectId}/locations/${location}/entryGroups/${entryGroupId}/entries/${entryId}`;
exec(
`node createFilesetEntry.js ${projectId} ${entryGroupId} ${entryId}`,
{cwd},
(err, stdout) => {
assert.ok(stdout.includes(expectedLinkedResource));
done();
}
);
});

after(async () => {
const entryPath = datacatalog.entryPath(
projectId,
location,
entryGroupId,
entryId
);
await datacatalog.deleteEntry({name: entryPath});
const entryGroupPath = datacatalog.entryGroupPath(
projectId,
location,
entryGroupId
);
await datacatalog.deleteEntryGroup({name: entryGroupPath});
});
});

0 comments on commit c5883ca

Please sign in to comment.