-
Notifications
You must be signed in to change notification settings - Fork 2.8k
/
CreateFilesetEntry.java
118 lines (109 loc) · 5.22 KB
/
CreateFilesetEntry.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
/*
* Copyright 2019 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.example.datacatalog;
// [START datacatalog_create_fileset_tag]
import com.google.api.gax.rpc.AlreadyExistsException;
import com.google.cloud.datacatalog.v1.ColumnSchema;
import com.google.cloud.datacatalog.v1.CreateEntryRequest;
import com.google.cloud.datacatalog.v1.DataCatalogClient;
import com.google.cloud.datacatalog.v1.Entry;
import com.google.cloud.datacatalog.v1.EntryGroupName;
import com.google.cloud.datacatalog.v1.EntryType;
import com.google.cloud.datacatalog.v1.GcsFilesetSpec;
import com.google.cloud.datacatalog.v1.Schema;
import java.io.IOException;
public class CreateFilesetEntry {
public static void createEntry() {
// TODO(developer): Replace these variables before running the sample.
String projectId = "my-project-id";
String entryGroupId = "fileset_entry_group";
String entryId = "fileset_entry_id";
createEntry(projectId, entryGroupId, entryId);
}
// Create Fileset Entry.
public static void createEntry(String projectId, String entryGroupId, String entryId) {
// Currently, Data Catalog stores metadata in the us-central1 region.
String location = "us-central1";
// Initialize client that will be used to send requests. This client only needs to be created
// once, and can be reused for multiple requests. After completing all of your requests, call
// the "close" method on the client to safely clean up any remaining background resources.
try (DataCatalogClient dataCatalogClient = DataCatalogClient.create()) {
// Construct the Entry for the Entry request.
Entry entry =
Entry.newBuilder()
.setDisplayName("My Fileset")
.setDescription("This fileset consists of ....")
.setGcsFilesetSpec(
GcsFilesetSpec.newBuilder().addFilePatterns("gs://cloud-samples-data/*").build())
.setSchema(
Schema.newBuilder()
.addColumns(
ColumnSchema.newBuilder()
.setColumn("first_name")
.setDescription("First name")
.setMode("REQUIRED")
.setType("STRING")
.build())
.addColumns(
ColumnSchema.newBuilder()
.setColumn("last_name")
.setDescription("Last name")
.setMode("REQUIRED")
.setType("STRING")
.build())
.addColumns(
ColumnSchema.newBuilder()
.setColumn("addresses")
.setDescription("Addresses")
.setMode("REPEATED")
.setType("RECORD")
.addSubcolumns(
ColumnSchema.newBuilder()
.setColumn("city")
.setDescription("City")
.setMode("NULLABLE")
.setType("STRING")
.build())
.addSubcolumns(
ColumnSchema.newBuilder()
.setColumn("state")
.setDescription("State")
.setMode("NULLABLE")
.setType("STRING")
.build())
.build())
.build())
.setType(EntryType.FILESET)
.build();
// Construct the Entry request to be sent by the client.
CreateEntryRequest entryRequest =
CreateEntryRequest.newBuilder()
.setParent(EntryGroupName.of(projectId, location, entryGroupId).toString())
.setEntryId(entryId)
.setEntry(entry)
.build();
// Use the client to send the API request.
Entry entryResponse = dataCatalogClient.createEntry(entryRequest);
System.out.printf("\nEntry created with name: %s\n", entryResponse.getName());
} catch (AlreadyExistsException | IOException e) {
// AlreadyExistsException's are thrown if EntryGroup or Entry already exists.
// IOException's are thrown when unable to create the DataCatalogClient,
// for example an invalid Service Account path.
System.out.println("Error in create entry process:\n" + e.toString());
}
}
}
// [END datacatalog_create_fileset_tag]