Skip to content

Commit 68a5c33

Browse files
suryaguthikondagcf-owl-bot[bot]junkourata
authored andcommitted
feat(samples): add feature values apis samples (#341)
* added create-featurestore-sample.js and create-featurestore-sample.test.js * feat(samples): added createFeaturestore sample (#313) * feat(samples): added remaining featurestore samples (#313) * removed the extraneous files * feat(samples): add feature-values apis samples (#313) * feat(samples): add feature values apis samples (#313) * 🦉 Updates from OwlBot post-processor See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md * feat(samples): update dependency featurevalues apis (#313) * 🦉 Updates from OwlBot post-processor See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md Co-authored-by: Owl Bot <gcf-owl-bot[bot]@users.noreply.github.com> Co-authored-by: junkourata <[email protected]>
1 parent f08ea11 commit 68a5c33

7 files changed

+866
-1
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
/*
2+
* Copyright 2022 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+
* https://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+
17+
/*
18+
* Batch reads Feature values from a Featurestore.
19+
* See https://cloud.google.com/vertex-ai/docs/featurestore/setup before running
20+
* the code snippet
21+
*/
22+
23+
'use strict';
24+
25+
async function main(
26+
project,
27+
featurestoreId,
28+
inputCsvFile,
29+
destinationTableUri,
30+
location = 'us-central1',
31+
apiEndpoint = 'us-central1-aiplatform.googleapis.com',
32+
timeout = 300000
33+
) {
34+
// [START aiplatform_batch_read_feature_values_sample]
35+
/**
36+
* TODO(developer): Uncomment these variables before running the sample.\
37+
* (Not necessary if passing values as arguments)
38+
*/
39+
40+
// const project = 'YOUR_PROJECT_ID';
41+
// const featurestoreId = 'YOUR_FEATURESTORE_ID';
42+
// const inputCsvFile = 'YOUR_INPUT_CSV_FILE_URI';
43+
// const destinationTableUri = 'YOUR_BQ_DESTINATION_TABLE_URI';
44+
// const location = 'YOUR_PROJECT_LOCATION';
45+
// const apiEndpoint = 'YOUR_API_ENDPOINT';
46+
// const timeout = <TIMEOUT_IN_MILLI_SECONDS>;
47+
48+
// Imports the Google Cloud Featurestore Service Client library
49+
const {FeaturestoreServiceClient} = require('@google-cloud/aiplatform').v1;
50+
51+
// Specifies the location of the api endpoint
52+
const clientOptions = {
53+
apiEndpoint: apiEndpoint,
54+
};
55+
56+
// Instantiates a client
57+
const featurestoreServiceClient = new FeaturestoreServiceClient(
58+
clientOptions
59+
);
60+
61+
async function batchReadFeatureValues() {
62+
// Configure the featurestoreId resource
63+
const featurestore = `projects/${project}/locations/${location}/featurestores/${featurestoreId}`;
64+
const csvReadInstances = {
65+
gcsSource: {
66+
uris: [inputCsvFile],
67+
},
68+
};
69+
70+
const destination = {
71+
bigqueryDestination: {
72+
// # Output to BigQuery table created earlier
73+
outputUri: destinationTableUri,
74+
},
75+
};
76+
77+
const usersFeatureSelector = {
78+
idMatcher: {
79+
ids: [
80+
// features, use "*" if you want to select all features within this entity type
81+
'age',
82+
'gender',
83+
'liked_genres',
84+
],
85+
},
86+
};
87+
88+
const usersEntityTypeSpec = {
89+
// Read the 'age', 'gender' and 'liked_genres' features from the 'perm_users' entity
90+
entityTypeId: 'perm_users',
91+
featureSelector: usersFeatureSelector,
92+
};
93+
94+
const moviesFeatureSelector = {
95+
idMatcher: {
96+
ids: ['*'],
97+
},
98+
};
99+
100+
const moviesEntityTypeSpec = {
101+
// Read the all features from the 'perm_movies' entity
102+
entityTypeId: 'perm_movies',
103+
featureSelector: moviesFeatureSelector,
104+
};
105+
106+
const entityTypeSpecs = [usersEntityTypeSpec, moviesEntityTypeSpec];
107+
108+
// Construct request
109+
const request = {
110+
featurestore: featurestore,
111+
csvReadInstances: csvReadInstances,
112+
destination: destination,
113+
entityTypeSpecs: entityTypeSpecs,
114+
};
115+
116+
// Batch Read Feature Values Request
117+
const [operation] = await featurestoreServiceClient.batchReadFeatureValues(
118+
request,
119+
{timeout: Number(timeout)}
120+
);
121+
const [response] = await operation.promise();
122+
123+
console.log('Batch read feature values response');
124+
console.log('Raw response:');
125+
console.log(JSON.stringify(response, null, 2));
126+
}
127+
batchReadFeatureValues();
128+
// [END aiplatform_batch_read_feature_values_sample]
129+
}
130+
131+
process.on('unhandledRejection', err => {
132+
console.error(err.message);
133+
process.exitCode = 1;
134+
});
135+
136+
main(...process.argv.slice(2));
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
/*
2+
* Copyright 2022 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+
* https://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+
17+
/*
18+
* Exports Feature values from all the entities of a target EntityType.
19+
* See https://cloud.google.com/vertex-ai/docs/featurestore/setup before running
20+
* the code snippet
21+
*/
22+
23+
'use strict';
24+
25+
async function main(
26+
project,
27+
featurestoreId,
28+
entityTypeId,
29+
destinationTableUri,
30+
location = 'us-central1',
31+
apiEndpoint = 'us-central1-aiplatform.googleapis.com',
32+
timeout = 300000
33+
) {
34+
// [START aiplatform_export_feature_values_sample]
35+
/**
36+
* TODO(developer): Uncomment these variables before running the sample.\
37+
* (Not necessary if passing values as arguments)
38+
*/
39+
40+
// const project = 'YOUR_PROJECT_ID';
41+
// const featurestoreId = 'YOUR_FEATURESTORE_ID';
42+
// const entityTypeId = 'YOUR_ENTITY_TYPE_ID';
43+
// const destinationTableUri = 'YOUR_BQ_DESTINATION_TABLE_URI';
44+
// const location = 'YOUR_PROJECT_LOCATION';
45+
// const apiEndpoint = 'YOUR_API_ENDPOINT';
46+
// const timeout = <TIMEOUT_IN_MILLI_SECONDS>;
47+
48+
// Imports the Google Cloud Featurestore Service Client library
49+
const {FeaturestoreServiceClient} = require('@google-cloud/aiplatform').v1;
50+
51+
// Specifies the location of the api endpoint
52+
const clientOptions = {
53+
apiEndpoint: apiEndpoint,
54+
};
55+
56+
// Instantiates a client
57+
const featurestoreServiceClient = new FeaturestoreServiceClient(
58+
clientOptions
59+
);
60+
61+
async function exportFeatureValues() {
62+
// Configure the entityType resource
63+
const entityType = `projects/${project}/locations/${location}/featurestores/${featurestoreId}/entityTypes/${entityTypeId}`;
64+
65+
const destination = {
66+
bigqueryDestination: {
67+
// # Output to BigQuery table created earlier
68+
outputUri: destinationTableUri,
69+
},
70+
};
71+
72+
const featureSelector = {
73+
idMatcher: {
74+
ids: ['age', 'gender', 'liked_genres'],
75+
},
76+
};
77+
78+
const request = {
79+
entityType: entityType,
80+
destination: destination,
81+
featureSelector: featureSelector,
82+
fullExport: {},
83+
};
84+
85+
// Export Feature Values Request
86+
const [operation] = await featurestoreServiceClient.exportFeatureValues(
87+
request,
88+
{timeout: Number(timeout)}
89+
);
90+
const [response] = await operation.promise();
91+
92+
console.log('Export feature values response');
93+
console.log('Raw response:');
94+
console.log(JSON.stringify(response, null, 2));
95+
}
96+
exportFeatureValues();
97+
// [END aiplatform_export_feature_values_sample]
98+
}
99+
100+
process.on('unhandledRejection', err => {
101+
console.error(err.message);
102+
process.exitCode = 1;
103+
});
104+
105+
main(...process.argv.slice(2));
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
/*
2+
* Copyright 2022 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+
* https://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+
17+
/*
18+
* Exports Feature values with snapshot from all the entities of a target EntityType.
19+
* See https://cloud.google.com/vertex-ai/docs/featurestore/setup before running
20+
* the code snippet
21+
*/
22+
23+
'use strict';
24+
25+
async function main(
26+
project,
27+
featurestoreId,
28+
entityTypeId,
29+
destinationTableUri,
30+
timestamp,
31+
location = 'us-central1',
32+
apiEndpoint = 'us-central1-aiplatform.googleapis.com',
33+
timeout = 300000
34+
) {
35+
// [START aiplatform_export_feature_values_snapshot_sample]
36+
/**
37+
* TODO(developer): Uncomment these variables before running the sample.\
38+
* (Not necessary if passing values as arguments)
39+
*/
40+
41+
// const project = 'YOUR_PROJECT_ID';
42+
// const featurestoreId = 'YOUR_FEATURESTORE_ID';
43+
// const entityTypeId = 'YOUR_ENTITY_TYPE_ID';
44+
// const destinationTableUri = 'YOUR_BQ_DESTINATION_TABLE_URI';
45+
// const timestamp = <STARTING_TIMESTAMP_OF_SNAPSHOT_IN_SECONDS>;
46+
// const location = 'YOUR_PROJECT_LOCATION';
47+
// const apiEndpoint = 'YOUR_API_ENDPOINT';
48+
// const timeout = <TIMEOUT_IN_MILLI_SECONDS>;
49+
50+
// Imports the Google Cloud Featurestore Service Client library
51+
const {FeaturestoreServiceClient} = require('@google-cloud/aiplatform').v1;
52+
53+
// Specifies the location of the api endpoint
54+
const clientOptions = {
55+
apiEndpoint: apiEndpoint,
56+
};
57+
58+
// Instantiates a client
59+
const featurestoreServiceClient = new FeaturestoreServiceClient(
60+
clientOptions
61+
);
62+
63+
async function exportFeatureValuesSnapshot() {
64+
// Configure the entityType resource
65+
const entityType = `projects/${project}/locations/${location}/featurestores/${featurestoreId}/entityTypes/${entityTypeId}`;
66+
67+
const destination = {
68+
bigqueryDestination: {
69+
// # Output to BigQuery table created earlier
70+
outputUri: destinationTableUri,
71+
},
72+
};
73+
74+
const featureSelector = {
75+
idMatcher: {
76+
ids: ['age', 'gender', 'liked_genres'],
77+
},
78+
};
79+
80+
const snapshotExport = {
81+
startTime: {
82+
seconds: Number(timestamp),
83+
},
84+
};
85+
86+
const request = {
87+
entityType: entityType,
88+
destination: destination,
89+
featureSelector: featureSelector,
90+
snapshotExport: snapshotExport,
91+
};
92+
93+
// Export Feature Values Request
94+
const [operation] = await featurestoreServiceClient.exportFeatureValues(
95+
request,
96+
{timeout: Number(timeout)}
97+
);
98+
const [response] = await operation.promise();
99+
100+
console.log('Export feature values snapshot response');
101+
console.log('Raw response:');
102+
console.log(JSON.stringify(response, null, 2));
103+
}
104+
exportFeatureValuesSnapshot();
105+
// [END aiplatform_export_feature_values_snapshot_sample]
106+
}
107+
108+
process.on('unhandledRejection', err => {
109+
console.error(err.message);
110+
process.exitCode = 1;
111+
});
112+
113+
main(...process.argv.slice(2));

0 commit comments

Comments
 (0)