1
+ /*
2
+ * Copyright 2019 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 datacatalog_create_fileset_tag]
20
+
21
+ import com .google .api .gax .rpc .AlreadyExistsException ;
22
+ import com .google .cloud .datacatalog .ColumnSchema ;
23
+ import com .google .cloud .datacatalog .CreateEntryRequest ;
24
+ import com .google .cloud .datacatalog .Entry ;
25
+ import com .google .cloud .datacatalog .EntryGroupName ;
26
+ import com .google .cloud .datacatalog .EntryType ;
27
+ import com .google .cloud .datacatalog .GcsFilesetSpec ;
28
+ import com .google .cloud .datacatalog .Schema ;
29
+ import com .google .cloud .datacatalog .v1beta1 .DataCatalogClient ;
30
+ import java .io .IOException ;
31
+
32
+ public class CreateFilesetEntry {
33
+
34
+ public static void createEntry () {
35
+ // TODO(developer): Replace these variables before running the sample.
36
+ String projectId = "my-project-id" ;
37
+ String entryGroupId = "fileset_entry_group" ;
38
+ String entryId = "fileset_entry_id" ;
39
+ createEntry (projectId , entryGroupId , entryId );
40
+ }
41
+
42
+ // Create Fileset Entry.
43
+ public static void createEntry (String projectId , String entryGroupId , String entryId ) {
44
+ // Currently, Data Catalog stores metadata in the us-central1 region.
45
+ String location = "us-central1" ;
46
+
47
+ // Initialize client that will be used to send requests. This client only needs to be created
48
+ // once, and can be reused for multiple requests. After completing all of your requests, call
49
+ // the "close" method on the client to safely clean up any remaining background resources.
50
+ try (DataCatalogClient dataCatalogClient = DataCatalogClient .create ()) {
51
+ // Construct the Entry for the Entry request.
52
+ Entry entry =
53
+ Entry .newBuilder ()
54
+ .setDisplayName ("My Fileset" )
55
+ .setDescription ("This fileset consists of ...." )
56
+ .setGcsFilesetSpec (
57
+ GcsFilesetSpec .newBuilder ().addFilePatterns ("gs://my_bucket/*" ).build ())
58
+ .setSchema (
59
+ Schema .newBuilder ()
60
+ .addColumns (
61
+ ColumnSchema .newBuilder ()
62
+ .setColumn ("first_name" )
63
+ .setDescription ("First name" )
64
+ .setMode ("REQUIRED" )
65
+ .setType ("STRING" )
66
+ .build ())
67
+ .addColumns (
68
+ ColumnSchema .newBuilder ()
69
+ .setColumn ("last_name" )
70
+ .setDescription ("Last name" )
71
+ .setMode ("REQUIRED" )
72
+ .setType ("STRING" )
73
+ .build ())
74
+ .addColumns (
75
+ ColumnSchema .newBuilder ()
76
+ .setColumn ("addresses" )
77
+ .setDescription ("Addresses" )
78
+ .setMode ("REPEATED" )
79
+ .setType ("RECORD" )
80
+ .addSubcolumns (
81
+ ColumnSchema .newBuilder ()
82
+ .setColumn ("city" )
83
+ .setDescription ("City" )
84
+ .setMode ("NULLABLE" )
85
+ .setType ("STRING" )
86
+ .build ())
87
+ .addSubcolumns (
88
+ ColumnSchema .newBuilder ()
89
+ .setColumn ("state" )
90
+ .setDescription ("State" )
91
+ .setMode ("NULLABLE" )
92
+ .setType ("STRING" )
93
+ .build ())
94
+ .build ())
95
+ .build ())
96
+ .setType (EntryType .FILESET )
97
+ .build ();
98
+
99
+ // Construct the Entry request to be sent by the client.
100
+ CreateEntryRequest entryRequest =
101
+ CreateEntryRequest .newBuilder ()
102
+ .setParent (EntryGroupName .of (projectId , location , entryGroupId ).toString ())
103
+ .setEntryId (entryId )
104
+ .setEntry (entry )
105
+ .build ();
106
+
107
+ // Use the client to send the API request.
108
+ Entry entryResponse = dataCatalogClient .createEntry (entryRequest );
109
+ System .out .printf ("\n Entry created with name: %s\n " , entryResponse .getName ());
110
+ } catch (AlreadyExistsException | IOException e ) {
111
+ // AlreadyExistsException's are thrown if EntryGroup or Entry already exists.
112
+ // IOException's are thrown when unable to create the DataCatalogClient,
113
+ // for example an invalid Service Account path.
114
+ System .out .println ("Error in create entry process:\n " + e .toString ());
115
+ }
116
+ }
117
+ }
118
+ // [END datacatalog_create_fileset_tag]
0 commit comments