Skip to content

Commit e2f2c97

Browse files
Add create filset Entry sample
1 parent 7ddce11 commit e2f2c97

File tree

3 files changed

+225
-0
lines changed

3 files changed

+225
-0
lines changed
Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
/* eslint-disable no-warning-comments */
2+
3+
// Copyright 2019 Google LLC
4+
//
5+
// Licensed under the Apache License, Version 2.0 (the "License");
6+
// you may not use this file except in compliance with the License.
7+
// You may obtain a copy of the License at
8+
//
9+
// http://www.apache.org/licenses/LICENSE-2.0
10+
//
11+
// Unless required by applicable law or agreed to in writing, software
12+
// distributed under the License is distributed on an "AS IS" BASIS,
13+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
// See the License for the specific language governing permissions and
15+
// limitations under the License.
16+
17+
'use strict';
18+
19+
/**
20+
* This application demonstrates how to create an Entry Group and a fileset
21+
* Entry with the Cloud Data Catalog API.
22+
23+
* For more information, see the README.md under /datacatalog and the
24+
* documentation at https://cloud.google.com/data-catalog/docs.
25+
*/
26+
const main = async (
27+
projectId = process.env.GCLOUD_PROJECT,
28+
entryGroupId,
29+
entryId
30+
) => {
31+
// [START datacatalog_create_fileset_quickstart_tag]
32+
// -------------------------------
33+
// Import required modules.
34+
// -------------------------------
35+
const {DataCatalogClient} = require('@google-cloud/datacatalog').v1beta1;
36+
const datacatalog = new DataCatalogClient();
37+
38+
// -------------------------------
39+
// Currently, Data Catalog stores metadata in the
40+
// us-central1 region.
41+
// -------------------------------
42+
const location = 'us-central1';
43+
44+
// TODO(developer): Uncomment the following lines before running the sample.
45+
// const projectId = 'my-project'
46+
// const entryGroupId = 'my-entry-group'
47+
// const entryId = 'my-entry'
48+
49+
// -------------------------------
50+
// 1. Create an Entry Group.
51+
// -------------------------------
52+
// Construct the EntryGroup for the EntryGroup request.
53+
const entryGroup = {
54+
displayName: 'My Fileset Entry Group',
55+
description: 'This Entry Group consists of ...',
56+
};
57+
58+
// Construct the EntryGroup request to be sent by the client.
59+
const entryGroupRequest = {
60+
parent: datacatalog.locationPath(projectId, location),
61+
entryGroupId: entryGroupId,
62+
entryGroup: entryGroup,
63+
};
64+
65+
// Use the client to send the API request.
66+
await datacatalog.createEntryGroup(entryGroupRequest);
67+
68+
// -------------------------------
69+
// 2. Create a Fileset Entry.
70+
// -------------------------------
71+
// Construct the Entry for the Entry request.
72+
const FILESET_TYPE = 4;
73+
74+
const entry = {
75+
displayName: 'My Fileset',
76+
description: 'This fileset consists of ...',
77+
gcsFilesetSpec: {filePatterns: ['gs://my_bucket/*']},
78+
schema: {
79+
columns: [
80+
{
81+
column: 'city',
82+
description: 'City',
83+
mode: 'NULLABLE',
84+
type: 'STRING',
85+
},
86+
{
87+
column: 'state',
88+
description: 'State',
89+
mode: 'NULLABLE',
90+
type: 'STRING',
91+
},
92+
{
93+
column: 'addresses',
94+
description: 'Addresses',
95+
mode: 'REPEATED',
96+
subcolumns: [
97+
{
98+
column: 'city',
99+
description: 'City',
100+
mode: 'NULLABLE',
101+
type: 'STRING',
102+
},
103+
{
104+
column: 'state',
105+
description: 'State',
106+
mode: 'NULLABLE',
107+
type: 'STRING',
108+
},
109+
],
110+
type: 'RECORD',
111+
},
112+
],
113+
},
114+
type: FILESET_TYPE,
115+
};
116+
117+
// Construct the Entry request to be sent by the client.
118+
const request = {
119+
parent: datacatalog.entryGroupPath(projectId, location, entryGroupId),
120+
entryId: entryId,
121+
entry: entry,
122+
};
123+
124+
// Use the client to send the API request.
125+
const [response] = await datacatalog.createEntry(request);
126+
127+
console.log(response);
128+
};
129+
// [END datacatalog_create_fileset_quickstart_tag]
130+
131+
// node createFilesetEntry.js <projectId> <entryGroupId> <entryId>
132+
main(...process.argv.slice(2));
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
{
2+
"name": "@google-cloud/datacatalog-quickstart",
3+
"version": "0.0.1",
4+
"private": true,
5+
"description": "Quickstart guides for the Cloud Data Catalog client library for Node.js",
6+
"license": "Apache-2.0",
7+
"author": "Google LLC",
8+
"repository": "GoogleCloudPlatform/nodejs-docs-samples",
9+
"engines": {
10+
"node": ">=8.0.0"
11+
},
12+
"scripts": {
13+
"test": "mocha system-test/*.test.js --timeout=60000"
14+
},
15+
"devDependencies": {
16+
"mocha": "^6.0.0",
17+
"uuid": "^3.1.0"
18+
},
19+
"dependencies": {
20+
"@google-cloud/datacatalog": "^1.5.0"
21+
}
22+
}
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
// Copyright 2019 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
'use strict';
16+
17+
const path = require('path');
18+
const assert = require('assert');
19+
const uuid = require('uuid');
20+
const cwd = path.join(__dirname, '..');
21+
const {exec} = require('child_process');
22+
23+
const projectId = process.env.GCLOUD_PROJECT;
24+
const location = 'us-central1';
25+
// Use unique id to avoid conflicts between concurrent test runs
26+
const entryGroupId = `fileset_entry_group_${uuid.v4().substr(0, 8)}`;
27+
const entryId = `fileset_entry_id_${uuid.v4().substr(0, 8)}`;
28+
29+
const {DataCatalogClient} = require('@google-cloud/datacatalog').v1beta1;
30+
const datacatalog = new DataCatalogClient();
31+
32+
before(() => {
33+
assert(
34+
process.env.GCLOUD_PROJECT,
35+
'Must set GCLOUD_PROJECT environment variable!'
36+
);
37+
assert(
38+
process.env.GOOGLE_APPLICATION_CREDENTIALS,
39+
'Must set GOOGLE_APPLICATION_CREDENTIALS environment variable!'
40+
);
41+
});
42+
43+
describe('createFilesetEntry', () => {
44+
it('should create a fileset entry', done => {
45+
const expectedLinkedResource = `//datacatalog.googleapis.com/projects/${projectId}/locations/${location}/entryGroups/${entryGroupId}/entries/${entryId}`;
46+
exec(
47+
`node createFilesetEntry.js ${projectId} ${entryGroupId} ${entryId}`,
48+
{cwd},
49+
(err, stdout) => {
50+
assert.ok(stdout.includes(expectedLinkedResource));
51+
done();
52+
}
53+
);
54+
});
55+
56+
after(async () => {
57+
const entryPath = datacatalog.entryPath(
58+
projectId,
59+
location,
60+
entryGroupId,
61+
entryId
62+
);
63+
await datacatalog.deleteEntry({name: entryPath});
64+
const entryGroupPath = datacatalog.entryGroupPath(
65+
projectId,
66+
location,
67+
entryGroupId
68+
);
69+
await datacatalog.deleteEntryGroup({name: entryGroupPath});
70+
});
71+
});

0 commit comments

Comments
 (0)