|
| 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] |
0 commit comments