Skip to content

Commit 4ac2639

Browse files
mesmacostachingor13
authored andcommitted
samples: Add Data Catalog createEntry samples and tests. (#1638)
* Add Data Catalog createEntry samples and tests * Change second column name * Code review changes * Change region tag for the correct one * Removed tag from region tag since this sample does not tag the entry * ADD method overload to show parameter values examples * Code review changes * Code review changes * Code review and checkstyle changes * Clean up in a safer order
1 parent e487b1f commit 4ac2639

File tree

3 files changed

+296
-0
lines changed

3 files changed

+296
-0
lines changed
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
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_entry_group_tag]
20+
21+
import com.google.api.gax.rpc.AlreadyExistsException;
22+
import com.google.cloud.datacatalog.CreateEntryGroupRequest;
23+
import com.google.cloud.datacatalog.EntryGroup;
24+
import com.google.cloud.datacatalog.LocationName;
25+
import com.google.cloud.datacatalog.v1beta1.DataCatalogClient;
26+
import java.io.IOException;
27+
28+
public class CreateEntryGroup {
29+
30+
public static void createEntryGroup() {
31+
// TODO(developer): Replace these variables before running the sample.
32+
String projectId = "my-project-id";
33+
String entryGroupId = "fileset_entry_group";
34+
createEntryGroup(projectId, entryGroupId);
35+
}
36+
37+
// Create Entry Group.
38+
public static void createEntryGroup(String projectId, String entryGroupId) {
39+
// Currently, Data Catalog stores metadata in the us-central1 region.
40+
String location = "us-central1";
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 (DataCatalogClient dataCatalogClient = DataCatalogClient.create()) {
46+
// Construct the EntryGroup for the EntryGroup request.
47+
EntryGroup entryGroup =
48+
EntryGroup.newBuilder()
49+
.setDisplayName("My Fileset Entry Group")
50+
.setDescription("This Entry Group consists of ....")
51+
.build();
52+
53+
// Construct the EntryGroup request to be sent by the client.
54+
CreateEntryGroupRequest entryGroupRequest =
55+
CreateEntryGroupRequest.newBuilder()
56+
.setParent(LocationName.of(projectId, location).toString())
57+
.setEntryGroupId(entryGroupId)
58+
.setEntryGroup(entryGroup)
59+
.build();
60+
61+
// Use the client to send the API request.
62+
EntryGroup entryGroupResponse = dataCatalogClient.createEntryGroup(entryGroupRequest);
63+
System.out.printf("\nEntry Group created with name: %s\n", entryGroupResponse.getName());
64+
} catch (AlreadyExistsException | IOException e) {
65+
// AlreadyExistsException's are thrown if EntryGroup or Entry already exists.
66+
// IOException's are thrown when unable to create the DataCatalogClient,
67+
// for example an invalid Service Account path.
68+
System.out.println("Error in create entry process:\n" + e.toString());
69+
}
70+
}
71+
}
72+
// [END datacatalog_create_entry_group_tag]
Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
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("\nEntry 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]
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
/*
2+
* Copyright 2019 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 com.example.datacatalog;
18+
19+
import static org.junit.Assert.assertThat;
20+
21+
import com.google.cloud.datacatalog.EntryGroupName;
22+
import com.google.cloud.datacatalog.EntryName;
23+
import com.google.cloud.datacatalog.v1beta1.DataCatalogClient;
24+
import java.io.ByteArrayOutputStream;
25+
import java.io.PrintStream;
26+
import java.util.UUID;
27+
import org.hamcrest.CoreMatchers;
28+
import org.junit.After;
29+
import org.junit.AfterClass;
30+
import org.junit.Before;
31+
import org.junit.Test;
32+
import org.junit.runner.RunWith;
33+
import org.junit.runners.JUnit4;
34+
35+
/** Integration (system) tests for {@link CreateFilesetEntry} and {@link CreateEntryGroup}. */
36+
@RunWith(JUnit4.class)
37+
public class CreateEntryTests {
38+
39+
private ByteArrayOutputStream bout;
40+
41+
private static String ENTRY_GROUP_ID_NO_CHILDREN =
42+
"entry_group_no_children_" + UUID.randomUUID().toString().substring(0, 8);
43+
private static String PARENT_ENTRY_GROUP_ID =
44+
"fileset_entry_group_parent_" + UUID.randomUUID().toString().substring(0, 8);
45+
private static String ENTRY_ID =
46+
"fileset_entry_id_" + UUID.randomUUID().toString().substring(0, 8);
47+
private static String LOCATION = "us-central1";
48+
private static String PROJECT_ID = System.getenv().get("GOOGLE_CLOUD_PROJECT");
49+
50+
@Before
51+
public void setUp() {
52+
bout = new ByteArrayOutputStream();
53+
System.setOut(new PrintStream(bout));
54+
}
55+
56+
@After
57+
public void tearDown() {
58+
System.setOut(null);
59+
bout.reset();
60+
}
61+
62+
@AfterClass
63+
public static void tearDownClass() {
64+
try (DataCatalogClient dataCatalogClient = DataCatalogClient.create()) {
65+
dataCatalogClient.deleteEntryGroup(
66+
EntryGroupName.of(PROJECT_ID, LOCATION, ENTRY_GROUP_ID_NO_CHILDREN).toString());
67+
68+
dataCatalogClient.deleteEntry(
69+
EntryName.of(PROJECT_ID, LOCATION, PARENT_ENTRY_GROUP_ID, ENTRY_ID).toString());
70+
dataCatalogClient.deleteEntryGroup(
71+
EntryGroupName.of(PROJECT_ID, LOCATION, PARENT_ENTRY_GROUP_ID).toString());
72+
} catch (Exception e) {
73+
System.out.println("Error in cleaning up test data:\n" + e.toString());
74+
}
75+
}
76+
77+
@Test
78+
public void testCreateFilesetEntry() {
79+
// Must create a Entry Group before creating the entry.
80+
CreateEntryGroup.createEntryGroup(PROJECT_ID, PARENT_ENTRY_GROUP_ID);
81+
CreateFilesetEntry.createEntry(PROJECT_ID, PARENT_ENTRY_GROUP_ID, ENTRY_ID);
82+
83+
String output = bout.toString();
84+
85+
String entryTemplate =
86+
"Entry created with name: projects/%s/locations/us-central1/entryGroups/%s/entries/%s";
87+
assertThat(
88+
output,
89+
CoreMatchers.containsString(
90+
String.format(entryTemplate, PROJECT_ID, PARENT_ENTRY_GROUP_ID, ENTRY_ID)));
91+
}
92+
93+
@Test
94+
public void testCreateEntryGroup() {
95+
CreateEntryGroup.createEntryGroup(PROJECT_ID, ENTRY_GROUP_ID_NO_CHILDREN);
96+
97+
String output = bout.toString();
98+
99+
String entryGroupTemplate =
100+
"Entry Group created with name: projects/%s/locations/us-central1/entryGroups/%s";
101+
assertThat(
102+
output,
103+
CoreMatchers.containsString(
104+
String.format(entryGroupTemplate, PROJECT_ID, ENTRY_GROUP_ID_NO_CHILDREN)));
105+
}
106+
}

0 commit comments

Comments
 (0)