Skip to content

Commit 5cba870

Browse files
Praful Makanistephaniewang526
andauthored
docs(samples): add Entry, EntryGroup and TagTemplate (#394)
* docs(samples): add Entry, EntryGroup and TagTemplate * fix IT * update sample * Update CreateEntryGroupIT.java * Update CreateEntryTests.java Co-authored-by: Stephanie Wang <stephaniewang526@users.noreply.github.com>
1 parent 93e60c8 commit 5cba870

31 files changed

+2179
-30
lines changed

datacatalog/snippets/pom.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,11 @@
4444
</dependency>
4545
<!-- [END datacatalog_install_with_bom] -->
4646

47+
<dependency>
48+
<groupId>com.google.protobuf</groupId>
49+
<artifactId>protobuf-java-util</artifactId>
50+
<version>3.14.0</version>
51+
</dependency>
4752
<dependency>
4853
<groupId>junit</groupId>
4954
<artifactId>junit</artifactId>
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
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 com.example.datacatalog;
18+
19+
// [START data_catalog_create_entry]
20+
import com.google.cloud.datacatalog.v1.CreateEntryRequest;
21+
import com.google.cloud.datacatalog.v1.DataCatalogClient;
22+
import com.google.cloud.datacatalog.v1.Entry;
23+
import com.google.cloud.datacatalog.v1.EntryGroupName;
24+
import java.io.IOException;
25+
26+
// Sample to create an entry
27+
public class CreateEntry {
28+
29+
public static void main(String[] args) throws IOException {
30+
// TODO(developer): Replace these variables before running the sample.
31+
String projectId = "MY_PROJECT_ID";
32+
String location = "MY_LOCATION";
33+
String entryGroupId = "MY_ENTRY_GROUP_ID";
34+
String entryId = "MY_ENTRY_ID";
35+
EntryGroupName entryGroupName = EntryGroupName.of(projectId, location, entryGroupId);
36+
Entry entry = Entry.newBuilder().build();
37+
createEntry(entryGroupName, entryId, entry);
38+
}
39+
40+
public static void createEntry(EntryGroupName entryGroupName, String entryId, Entry entry)
41+
throws IOException {
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 client = DataCatalogClient.create()) {
46+
CreateEntryRequest request =
47+
CreateEntryRequest.newBuilder()
48+
.setParent(entryGroupName.toString())
49+
.setEntryId(entryId)
50+
.setEntry(entry)
51+
.build();
52+
client.createEntry(request);
53+
System.out.println("Entry created successfully");
54+
}
55+
}
56+
}
57+
// [END data_catalog_create_entry]
Lines changed: 14 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2019 Google Inc.
2+
* Copyright 2020 Google Inc.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -16,57 +16,47 @@
1616

1717
package com.example.datacatalog;
1818

19-
// [START datacatalog_create_entry_group_tag]
20-
21-
import com.google.api.gax.rpc.AlreadyExistsException;
19+
// [START data_catalog_create_entry_group]
2220
import com.google.cloud.datacatalog.v1.CreateEntryGroupRequest;
2321
import com.google.cloud.datacatalog.v1.DataCatalogClient;
2422
import com.google.cloud.datacatalog.v1.EntryGroup;
2523
import com.google.cloud.datacatalog.v1.LocationName;
2624
import java.io.IOException;
2725

26+
// Sample to create an entry group
2827
public class CreateEntryGroup {
2928

30-
public static void createEntryGroup() {
29+
public static void main(String[] args) throws IOException {
3130
// 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);
31+
String projectId = "MY_PROJECT_ID";
32+
String location = "us-central1";
33+
String entryGroupId = "MY_ENTRY_GROUP_ID";
34+
createEntryGroup(projectId, location, entryGroupId);
3535
}
3636

3737
// 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-
38+
public static void createEntryGroup(String projectId, String location, String entryGroupId)
39+
throws IOException {
4240
// Initialize client that will be used to send requests. This client only needs to be created
4341
// once, and can be reused for multiple requests. After completing all of your requests, call
4442
// the "close" method on the client to safely clean up any remaining background resources.
4543
try (DataCatalogClient dataCatalogClient = DataCatalogClient.create()) {
46-
// Construct the EntryGroup for the EntryGroup request.
4744
EntryGroup entryGroup =
4845
EntryGroup.newBuilder()
49-
.setDisplayName("My Fileset Entry Group")
46+
.setDisplayName("MY Entry Group")
5047
.setDescription("This Entry Group consists of ....")
5148
.build();
5249

53-
// Construct the EntryGroup request to be sent by the client.
5450
CreateEntryGroupRequest entryGroupRequest =
5551
CreateEntryGroupRequest.newBuilder()
5652
.setParent(LocationName.of(projectId, location).toString())
5753
.setEntryGroupId(entryGroupId)
5854
.setEntryGroup(entryGroup)
5955
.build();
6056

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());
57+
dataCatalogClient.createEntryGroup(entryGroupRequest);
58+
System.out.println("Entry Group created successfully");
6959
}
7060
}
7161
}
72-
// [END datacatalog_create_entry_group_tag]
62+
// [END data_catalog_create_entry_group]

datacatalog/snippets/src/main/java/com/example/datacatalog/CreateFilesetEntry.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,4 +115,4 @@ public static void createEntry(String projectId, String entryGroupId, String ent
115115
}
116116
}
117117
}
118-
// [END datacatalog_create_fileset_tag]
118+
// [END datacatalog_create_fileset_tag]
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
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 com.example.datacatalog;
18+
19+
// [START data_catalog_create_tag_template]
20+
import com.google.cloud.datacatalog.v1.CreateTagTemplateRequest;
21+
import com.google.cloud.datacatalog.v1.DataCatalogClient;
22+
import com.google.cloud.datacatalog.v1.FieldType;
23+
import com.google.cloud.datacatalog.v1.LocationName;
24+
import com.google.cloud.datacatalog.v1.TagTemplate;
25+
import com.google.cloud.datacatalog.v1.TagTemplateField;
26+
import java.io.IOException;
27+
28+
// Sample to create tag template
29+
public class CreateTagTemplate {
30+
31+
public static void main(String[] args) throws IOException {
32+
// TODO(developer): Replace these variables before running the sample.
33+
String projectId = "MY_PROJECT_ID";
34+
String location = "MY_LOCATION";
35+
LocationName locationName = LocationName.of(projectId, location);
36+
String tagTemplateId = "MY_TAG_TEMPLATE_ID";
37+
TagTemplateField sourceField =
38+
TagTemplateField.newBuilder()
39+
.setDisplayName("Your display name")
40+
.setType(
41+
FieldType.newBuilder().setPrimitiveType(FieldType.PrimitiveType.STRING).build())
42+
.build();
43+
TagTemplate tagTemplate =
44+
TagTemplate.newBuilder()
45+
.setDisplayName("Your display name")
46+
.putFields("sourceField", sourceField)
47+
.build();
48+
createTagTemplate(locationName, tagTemplateId, tagTemplate);
49+
}
50+
51+
public static void createTagTemplate(
52+
LocationName name, String tagTemplateId, TagTemplate template) throws IOException {
53+
// Initialize client that will be used to send requests. This client only needs to be created
54+
// once, and can be reused for multiple requests. After completing all of your requests, call
55+
// the "close" method on the client to safely clean up any remaining background resources.
56+
try (DataCatalogClient client = DataCatalogClient.create()) {
57+
CreateTagTemplateRequest request =
58+
CreateTagTemplateRequest.newBuilder()
59+
.setParent(name.toString())
60+
.setTagTemplateId(tagTemplateId)
61+
.setTagTemplate(template)
62+
.build();
63+
client.createTagTemplate(request);
64+
System.out.println("Tag template created successfully");
65+
}
66+
}
67+
}
68+
// [END data_catalog_create_tag_template]
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
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 com.example.datacatalog;
18+
19+
// [START data_catalog_delete_entry]
20+
import com.google.cloud.datacatalog.v1.DataCatalogClient;
21+
import com.google.cloud.datacatalog.v1.DeleteEntryRequest;
22+
import com.google.cloud.datacatalog.v1.EntryName;
23+
import java.io.IOException;
24+
25+
// Sample to delete a entry
26+
public class DeleteEntry {
27+
28+
public static void main(String[] args) throws IOException {
29+
// TODO(developer): Replace these variables before running the sample.
30+
String projectId = "MY_PROJECT_ID";
31+
String location = "MY_LOCATION";
32+
String entryGroupId = "MY_ENTRY_GROUP_ID";
33+
String entryId = "MY_ENTRY_ID";
34+
EntryName entryName = EntryName.of(projectId, location, entryGroupId, entryId);
35+
deleteEntry(entryName);
36+
}
37+
38+
public static void deleteEntry(EntryName entryName) throws IOException {
39+
// Initialize client that will be used to send requests. This client only needs to be created
40+
// once, and can be reused for multiple requests. After completing all of your requests, call
41+
// the "close" method on the client to safely clean up any remaining background resources.
42+
try (DataCatalogClient client = DataCatalogClient.create()) {
43+
DeleteEntryRequest request =
44+
DeleteEntryRequest.newBuilder().setName(entryName.toString()).build();
45+
client.deleteEntry(request);
46+
System.out.println("Entry deleted successfully");
47+
}
48+
}
49+
}
50+
// [END data_catalog_delete_entry]
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
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_delete_entry_group]
20+
import com.google.cloud.datacatalog.v1.DataCatalogClient;
21+
import com.google.cloud.datacatalog.v1.DeleteEntryGroupRequest;
22+
import com.google.cloud.datacatalog.v1.EntryGroupName;
23+
import java.io.IOException;
24+
25+
// Sample to delete a entry group
26+
public class DeleteEntryGroup {
27+
28+
public static void main(String[] args) throws IOException {
29+
// TODO(developer): Replace these variables before running the sample.
30+
String projectId = "MY_PROJECT_ID";
31+
String location = "MY_LOCATION";
32+
String entryGroupId = "MY_ENTRY_GROUP_ID";
33+
EntryGroupName entryGroupName = EntryGroupName.of(projectId, location, entryGroupId);
34+
deleteEntryGroup(entryGroupName);
35+
}
36+
37+
public static void deleteEntryGroup(EntryGroupName entryGroupName) throws IOException {
38+
// Initialize client that will be used to send requests. This client only needs to be created
39+
// once, and can be reused for multiple requests. After completing all of your requests, call
40+
// the "close" method on the client to safely clean up any remaining background resources.
41+
try (DataCatalogClient client = DataCatalogClient.create()) {
42+
DeleteEntryGroupRequest request =
43+
DeleteEntryGroupRequest.newBuilder().setName(entryGroupName.toString()).build();
44+
client.deleteEntryGroup(request);
45+
System.out.println("Entry group deleted successfully");
46+
}
47+
}
48+
}
49+
// [END data_catalog_delete_entry_group]
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
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 com.example.datacatalog;
18+
19+
// [START data_catalog_delete_tag_template]
20+
import com.google.cloud.datacatalog.v1.DataCatalogClient;
21+
import com.google.cloud.datacatalog.v1.DeleteTagTemplateRequest;
22+
import com.google.cloud.datacatalog.v1.TagTemplateName;
23+
import java.io.IOException;
24+
25+
// Sample to delete tag template
26+
public class DeleteTagTemplate {
27+
28+
public static void main(String[] args) throws IOException {
29+
// TODO(developer): Replace these variables before running the sample.
30+
String projectId = "MY_PROJECT_ID";
31+
String location = "MY_LOCATION";
32+
String tagTemplateId = "MY_TAG_TEMPLATE_ID";
33+
TagTemplateName tagTemplate = TagTemplateName.of(projectId, location, tagTemplateId);
34+
deleteTagTemplate(tagTemplate);
35+
}
36+
37+
public static void deleteTagTemplate(TagTemplateName template) throws IOException {
38+
// Initialize client that will be used to send requests. This client only needs to be created
39+
// once, and can be reused for multiple requests. After completing all of your requests, call
40+
// the "close" method on the client to safely clean up any remaining background resources.
41+
try (DataCatalogClient client = DataCatalogClient.create()) {
42+
DeleteTagTemplateRequest request =
43+
DeleteTagTemplateRequest.newBuilder().setName(template.toString()).setForce(true).build();
44+
client.deleteTagTemplate(request);
45+
System.out.println("Tag template deleted successfully");
46+
}
47+
}
48+
}
49+
// [END data_catalog_delete_tag_template]

0 commit comments

Comments
 (0)