|
| 1 | +/* |
| 2 | + * Copyright 2020 Google Inc. |
| 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 com.example.datacatalog; |
| 18 | + |
| 19 | +// [START data_catalog_create_custom_entry] |
| 20 | +import com.google.cloud.datacatalog.v1.ColumnSchema; |
| 21 | +import com.google.cloud.datacatalog.v1.CreateEntryGroupRequest; |
| 22 | +import com.google.cloud.datacatalog.v1.CreateEntryRequest; |
| 23 | +import com.google.cloud.datacatalog.v1.DataCatalogClient; |
| 24 | +import com.google.cloud.datacatalog.v1.Entry; |
| 25 | +import com.google.cloud.datacatalog.v1.EntryGroup; |
| 26 | +import com.google.cloud.datacatalog.v1.LocationName; |
| 27 | +import com.google.cloud.datacatalog.v1.Schema; |
| 28 | +import java.io.IOException; |
| 29 | + |
| 30 | +// Sample to create custom entry |
| 31 | +public class CreateCustomEntry { |
| 32 | + |
| 33 | + public static void main(String[] args) throws IOException { |
| 34 | + // TODO(developer): Replace these variables before running the sample. |
| 35 | + String projectId = "my-project"; |
| 36 | + String entryGroupId = "onprem_entry_group"; |
| 37 | + String entryId = "onprem_entry_id"; |
| 38 | + createCustomEntry(projectId, entryGroupId, entryId); |
| 39 | + } |
| 40 | + |
| 41 | + public static void createCustomEntry(String projectId, String entryGroupId, String entryId) |
| 42 | + throws IOException { |
| 43 | + // Currently, Data Catalog stores metadata in the us-central1 region. |
| 44 | + String location = "us-central1"; |
| 45 | + |
| 46 | + // Initialize client that will be used to send requests. This client only needs to be created |
| 47 | + // once, and can be reused for multiple requests. After completing all of your requests, call |
| 48 | + // the "close" method on the client to safely clean up any remaining background resources. |
| 49 | + try (DataCatalogClient dataCatalogClient = DataCatalogClient.create()) { |
| 50 | + // Construct the EntryGroup for the EntryGroup request. |
| 51 | + EntryGroup entryGroup = |
| 52 | + EntryGroup.newBuilder() |
| 53 | + .setDisplayName("My awesome Entry Group") |
| 54 | + .setDescription("This Entry Group represents an external system") |
| 55 | + .build(); |
| 56 | + |
| 57 | + // Construct the EntryGroup request to be sent by the client. |
| 58 | + CreateEntryGroupRequest entryGroupRequest = |
| 59 | + CreateEntryGroupRequest.newBuilder() |
| 60 | + .setParent(LocationName.of(projectId, location).toString()) |
| 61 | + .setEntryGroupId(entryGroupId) |
| 62 | + .setEntryGroup(entryGroup) |
| 63 | + .build(); |
| 64 | + |
| 65 | + // Use the client to send the API request. |
| 66 | + EntryGroup createdEntryGroup = dataCatalogClient.createEntryGroup(entryGroupRequest); |
| 67 | + |
| 68 | + // Construct the Entry for the Entry request. |
| 69 | + Entry entry = |
| 70 | + Entry.newBuilder() |
| 71 | + .setUserSpecifiedSystem("onprem_data_system") |
| 72 | + .setUserSpecifiedType("onprem_data_asset") |
| 73 | + .setDisplayName("My awesome data asset") |
| 74 | + .setDescription("This data asset is managed by an external system.") |
| 75 | + .setLinkedResource("//my-onprem-server.com/dataAssets/my-awesome-data-asset") |
| 76 | + .setSchema( |
| 77 | + Schema.newBuilder() |
| 78 | + .addColumns( |
| 79 | + ColumnSchema.newBuilder() |
| 80 | + .setColumn("first_column") |
| 81 | + .setDescription("This columns consists of ....") |
| 82 | + .setMode("NULLABLE") |
| 83 | + .setType("DOUBLE") |
| 84 | + .build()) |
| 85 | + .addColumns( |
| 86 | + ColumnSchema.newBuilder() |
| 87 | + .setColumn("second_column") |
| 88 | + .setDescription("This columns consists of ....") |
| 89 | + .setMode("REQUIRED") |
| 90 | + .setType("STRING") |
| 91 | + .build()) |
| 92 | + .build()) |
| 93 | + .build(); |
| 94 | + |
| 95 | + // Construct the Entry request to be sent by the client. |
| 96 | + CreateEntryRequest entryRequest = |
| 97 | + CreateEntryRequest.newBuilder() |
| 98 | + .setParent(createdEntryGroup.getName()) |
| 99 | + .setEntryId(entryId) |
| 100 | + .setEntry(entry) |
| 101 | + .build(); |
| 102 | + |
| 103 | + // Use the client to send the API request. |
| 104 | + Entry createdEntry = dataCatalogClient.createEntry(entryRequest); |
| 105 | + System.out.printf("Custom entry created with name: %s", createdEntry.getName()); |
| 106 | + } |
| 107 | + } |
| 108 | +} |
| 109 | +// [END data_catalog_create_custom_entry] |
0 commit comments