Skip to content

Commit 413b908

Browse files
authored
samples: ucaip samples batch 3 of 6 (#18)
* samples:samples: ucaip samples batch 3 of 6 * made requested the changes * changed all instance of tables into tabular * fixed the lint * reversed some comments
1 parent 9688a76 commit 413b908

23 files changed

+2161
-2
lines changed

aiplatform/snippets/pom.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,14 @@
2323
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
2424
</properties>
2525

26+
27+
<!-- [START aiplatform_install_with_bom] -->
2628
<dependencies>
27-
<!-- TODO: switch to libraries-bom after this artifact is included -->
2829
<dependency>
2930
<groupId>com.google.cloud</groupId>
3031
<artifactId>google-cloud-aiplatform</artifactId>
3132
<version>0.0.1-SNAPSHOT</version>
3233
</dependency>
33-
3434
<dependency>
3535
<groupId>com.google.cloud</groupId>
3636
<artifactId>google-cloud-storage</artifactId>
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
/*
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+
17+
package aiplatform;
18+
19+
// [START aiplatform_cancel_training_pipeline_sample]
20+
21+
import com.google.cloud.aiplatform.v1beta1.PipelineServiceClient;
22+
import com.google.cloud.aiplatform.v1beta1.PipelineServiceSettings;
23+
import com.google.cloud.aiplatform.v1beta1.TrainingPipelineName;
24+
import java.io.IOException;
25+
26+
public class CancelTrainingPipelineSample {
27+
28+
public static void main(String[] args) throws IOException {
29+
// TODO(developer): Replace these variables before running the sample.
30+
String trainingPipelineId = "YOUR_TRAINING_PIPELINE_ID";
31+
String project = "YOUR_PROJECT_ID";
32+
cancelTrainingPipelineSample(project, trainingPipelineId);
33+
}
34+
35+
static void cancelTrainingPipelineSample(String project, String trainingPipelineId)
36+
throws IOException {
37+
PipelineServiceSettings pipelineServiceSettings =
38+
PipelineServiceSettings.newBuilder()
39+
.setEndpoint("us-central1-aiplatform.googleapis.com:443")
40+
.build();
41+
42+
// Initialize client that will be used to send requests. This client only needs to be created
43+
// once, and can be reused for multiple requests. After completing all of your requests, call
44+
// the "close" method on the client to safely clean up any remaining background resources.
45+
try (PipelineServiceClient pipelineServiceClient =
46+
PipelineServiceClient.create(pipelineServiceSettings)) {
47+
String location = "us-central1";
48+
TrainingPipelineName trainingPipelineName =
49+
TrainingPipelineName.of(project, location, trainingPipelineId);
50+
51+
pipelineServiceClient.cancelTrainingPipeline(trainingPipelineName);
52+
53+
System.out.println("Cancelled the Training Pipeline");
54+
}
55+
}
56+
}
57+
// [END aiplatform_cancel_training_pipeline_sample]
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
/*
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+
17+
package aiplatform;
18+
19+
// [START aiplatform_create_dataset_tabular_bigquery_sample]
20+
21+
import com.google.api.gax.longrunning.OperationFuture;
22+
import com.google.cloud.aiplatform.v1beta1.CreateDatasetOperationMetadata;
23+
import com.google.cloud.aiplatform.v1beta1.Dataset;
24+
import com.google.cloud.aiplatform.v1beta1.DatasetServiceClient;
25+
import com.google.cloud.aiplatform.v1beta1.DatasetServiceSettings;
26+
import com.google.cloud.aiplatform.v1beta1.LocationName;
27+
import com.google.protobuf.Value;
28+
import com.google.protobuf.util.JsonFormat;
29+
import java.io.IOException;
30+
import java.util.concurrent.ExecutionException;
31+
import java.util.concurrent.TimeUnit;
32+
import java.util.concurrent.TimeoutException;
33+
34+
public class CreateDatasetTabularBigquerySample {
35+
36+
public static void main(String[] args)
37+
throws InterruptedException, ExecutionException, TimeoutException, IOException {
38+
// TODO(developer): Replace these variables before running the sample.
39+
String project = "YOUR_PROJECT_ID";
40+
String bigqueryDisplayName = "YOUR_DATASET_DISPLAY_NAME";
41+
String bigqueryUri =
42+
"bq://YOUR_GOOGLE_CLOUD_PROJECT_ID.BIGQUERY_DATASET_ID.BIGQUERY_TABLE_OR_VIEW_ID";
43+
createDatasetTableBigquery(project, bigqueryDisplayName, bigqueryUri);
44+
}
45+
46+
static void createDatasetTableBigquery(
47+
String project, String bigqueryDisplayName, String bigqueryUri)
48+
throws IOException, ExecutionException, InterruptedException, TimeoutException {
49+
DatasetServiceSettings settings =
50+
DatasetServiceSettings.newBuilder()
51+
.setEndpoint("us-central1-aiplatform.googleapis.com:443")
52+
.build();
53+
54+
// Initialize client that will be used to send requests. This client only needs to be created
55+
// once, and can be reused for multiple requests. After completing all of your requests, call
56+
// the "close" method on the client to safely clean up any remaining background resources.
57+
try (DatasetServiceClient datasetServiceClient = DatasetServiceClient.create(settings)) {
58+
String location = "us-central1";
59+
String metadataSchemaUri =
60+
"gs://google-cloud-aiplatform/schema/dataset/metadata/tables_1.0.0.yaml";
61+
LocationName locationName = LocationName.of(project, location);
62+
63+
String jsonString =
64+
"{\"input_config\": {\"bigquery_source\": {\"uri\": \"" + bigqueryUri + "\"}}}";
65+
Value.Builder metaData = Value.newBuilder();
66+
JsonFormat.parser().merge(jsonString, metaData);
67+
68+
Dataset dataset =
69+
Dataset.newBuilder()
70+
.setDisplayName(bigqueryDisplayName)
71+
.setMetadataSchemaUri(metadataSchemaUri)
72+
.setMetadata(metaData)
73+
.build();
74+
75+
OperationFuture<Dataset, CreateDatasetOperationMetadata> datasetFuture =
76+
datasetServiceClient.createDatasetAsync(locationName, dataset);
77+
System.out.format("Operation name: %s\n", datasetFuture.getInitialFuture().get().getName());
78+
System.out.println("Waiting for operation to finish...");
79+
Dataset datasetResponse = datasetFuture.get(300, TimeUnit.SECONDS);
80+
81+
System.out.println("Create Dataset Table Bigquery sample");
82+
System.out.format("Name: %s\n", datasetResponse.getName());
83+
System.out.format("Display Name: %s\n", datasetResponse.getDisplayName());
84+
System.out.format("Metadata Schema Uri: %s\n", datasetResponse.getMetadataSchemaUri());
85+
System.out.format("Metadata: %s\n", datasetResponse.getMetadata());
86+
}
87+
}
88+
}
89+
// [END aiplatform_create_dataset_tabular_bigquery_sample]
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
/*
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+
17+
package aiplatform;
18+
19+
// [START aiplatform_create_dataset_tabular_gcs_sample]
20+
21+
import com.google.api.gax.longrunning.OperationFuture;
22+
import com.google.cloud.aiplatform.v1beta1.CreateDatasetOperationMetadata;
23+
import com.google.cloud.aiplatform.v1beta1.Dataset;
24+
import com.google.cloud.aiplatform.v1beta1.DatasetServiceClient;
25+
import com.google.cloud.aiplatform.v1beta1.DatasetServiceSettings;
26+
import com.google.cloud.aiplatform.v1beta1.LocationName;
27+
import com.google.protobuf.Value;
28+
import com.google.protobuf.util.JsonFormat;
29+
import java.io.IOException;
30+
import java.util.concurrent.ExecutionException;
31+
import java.util.concurrent.TimeUnit;
32+
import java.util.concurrent.TimeoutException;
33+
34+
public class CreateDatasetTabularGcsSample {
35+
36+
public static void main(String[] args)
37+
throws InterruptedException, ExecutionException, TimeoutException, IOException {
38+
// TODO(developer): Replace these variables before running the sample.
39+
String project = "YOUR_PROJECT_ID";
40+
String datasetDisplayName = "YOUR_DATASET_DISPLAY_NAME";
41+
String gcsSourceUri = "gs://YOUR_GCS_SOURCE_BUCKET/path_to_your_gcs_table/file.csv";
42+
;
43+
createDatasetTableGcs(project, datasetDisplayName, gcsSourceUri);
44+
}
45+
46+
static void createDatasetTableGcs(String project, String datasetDisplayName, String gcsSourceUri)
47+
throws IOException, ExecutionException, InterruptedException, TimeoutException {
48+
DatasetServiceSettings settings =
49+
DatasetServiceSettings.newBuilder()
50+
.setEndpoint("us-central1-aiplatform.googleapis.com:443")
51+
.build();
52+
53+
// Initialize client that will be used to send requests. This client only needs to be created
54+
// once, and can be reused for multiple requests. After completing all of your requests, call
55+
// the "close" method on the client to safely clean up any remaining background resources.
56+
try (DatasetServiceClient datasetServiceClient = DatasetServiceClient.create(settings)) {
57+
String location = "us-central1";
58+
String metadataSchemaUri =
59+
"gs://google-cloud-aiplatform/schema/dataset/metadata/tables_1.0.0.yaml";
60+
LocationName locationName = LocationName.of(project, location);
61+
62+
String jsonString =
63+
"{\"input_config\": {\"gcs_source\": {\"uri\": [\"" + gcsSourceUri + "\"]}}}";
64+
Value.Builder metaData = Value.newBuilder();
65+
JsonFormat.parser().merge(jsonString, metaData);
66+
67+
Dataset dataset =
68+
Dataset.newBuilder()
69+
.setDisplayName(datasetDisplayName)
70+
.setMetadataSchemaUri(metadataSchemaUri)
71+
.setMetadata(metaData)
72+
.build();
73+
74+
OperationFuture<Dataset, CreateDatasetOperationMetadata> datasetFuture =
75+
datasetServiceClient.createDatasetAsync(locationName, dataset);
76+
System.out.format("Operation name: %s\n", datasetFuture.getInitialFuture().get().getName());
77+
System.out.println("Waiting for operation to finish...");
78+
Dataset datasetResponse = datasetFuture.get(300, TimeUnit.SECONDS);
79+
80+
System.out.println("Create Dataset Table GCS sample");
81+
System.out.format("Name: %s\n", datasetResponse.getName());
82+
System.out.format("Display Name: %s\n", datasetResponse.getDisplayName());
83+
System.out.format("Metadata Schema Uri: %s\n", datasetResponse.getMetadataSchemaUri());
84+
System.out.format("Metadata: %s\n", datasetResponse.getMetadata());
85+
}
86+
}
87+
}
88+
// [END aiplatform_create_dataset_tabular_gcs_sample]

0 commit comments

Comments
 (0)