Skip to content

Commit 010eb37

Browse files
jacspa96Jacek Spalinski
and
Jacek Spalinski
authored
feat(dataplex): add code samples for Entry (GoogleCloudPlatform#9587)
* feat(dataplex): add sample for list Entries * feat(dataplex): add sample for get Entry * feat(dataplex): add sample for lookup Entry * feat(dataplex): add sample for create Entry * feat(dataplex): add sample for update Entry * feat(dataplex): add sample for delete Entry * feat(dataplex): add integration tests for Entry * feat(dataplex): add filter to list Entries * feat(dataplex): remove redundant comment * feat(dataplex): change path filter to remove ambiguity with generic * feat(dataplex): adjust comments * feat(dataplex): move all code in methods into try scope * feat(dataplex): add link to documentation about EntryView * feat(dataplex): add link to documentation about filters * feat(dataplex): add link to documentation about field mask * feat(dataplex): adjust comments * feat(dataplex): adjust comments * feat(dataplex): adjust methods' comments --------- Co-authored-by: Jacek Spalinski <jspa@google.com>
1 parent f4a9938 commit 010eb37

File tree

7 files changed

+546
-0
lines changed

7 files changed

+546
-0
lines changed
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
/*
2+
* Copyright 2024 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 dataplex;
18+
19+
// [START dataplex_create_entry]
20+
import com.google.cloud.dataplex.v1.Aspect;
21+
import com.google.cloud.dataplex.v1.CatalogServiceClient;
22+
import com.google.cloud.dataplex.v1.Entry;
23+
import com.google.cloud.dataplex.v1.EntryGroupName;
24+
import com.google.cloud.dataplex.v1.EntrySource;
25+
import com.google.protobuf.Struct;
26+
import com.google.protobuf.Value;
27+
import java.util.Map;
28+
29+
public class CreateEntry {
30+
31+
public static void main(String[] args) throws Exception {
32+
// TODO(developer): Replace these variables before running the sample.
33+
String projectId = "MY_PROJECT_ID";
34+
String location = "MY_LOCATION";
35+
String entryGroupId = "MY_ENTRY_GROUP_ID";
36+
String entryId = "MY_ENTRY_ID";
37+
38+
Entry createdEntry = createEntry(projectId, location, entryGroupId, entryId);
39+
System.out.println("Successfully created entry: " + createdEntry.getName());
40+
}
41+
42+
// Method to create Entry located in projectId, location, entryGroupId and with entryId
43+
public static Entry createEntry(
44+
String projectId, String location, String entryGroupId, String entryId) throws Exception {
45+
// Initialize client that will be used to send requests. This client only needs to be created
46+
// once, and can be reused for multiple requests.
47+
try (CatalogServiceClient client = CatalogServiceClient.create()) {
48+
EntryGroupName entryGroupName = EntryGroupName.of(projectId, location, entryGroupId);
49+
Entry entry =
50+
Entry.newBuilder()
51+
// Example of system Entry Type.
52+
// It is also possible to specify custom Entry Type.
53+
.setEntryType("projects/dataplex-types/locations/global/entryTypes/generic")
54+
.setEntrySource(
55+
EntrySource.newBuilder().setDescription("description of the entry").build())
56+
.putAllAspects(
57+
Map.of(
58+
"dataplex-types.global.generic",
59+
Aspect.newBuilder()
60+
// This is required Aspect Type for "generic" Entry Type.
61+
// For custom Aspect Type required Entry Type would be different.
62+
.setAspectType(
63+
"projects/dataplex-types/locations/global/aspectTypes/generic")
64+
.setData(
65+
Struct.newBuilder()
66+
// "Generic" Aspect Type have fields called "type" and "system.
67+
// The values below are a sample of possible options.
68+
.putFields(
69+
"type",
70+
Value.newBuilder().setStringValue("example value").build())
71+
.putFields(
72+
"system",
73+
Value.newBuilder().setStringValue("example system").build())
74+
.build())
75+
.build()))
76+
.build();
77+
return client.createEntry(entryGroupName, entry, entryId);
78+
}
79+
}
80+
}
81+
// [END dataplex_create_entry]
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/*
2+
* Copyright 2024 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 dataplex;
18+
19+
// [START dataplex_delete_entry]
20+
import com.google.cloud.dataplex.v1.CatalogServiceClient;
21+
import com.google.cloud.dataplex.v1.EntryName;
22+
23+
public class DeleteEntry {
24+
25+
public static void main(String[] args) throws Exception {
26+
// TODO(developer): Replace these variables before running the sample.
27+
String projectId = "MY_PROJECT_ID";
28+
String location = "MY_LOCATION";
29+
String entryGroupId = "MY_ENTRY_GROUP_ID";
30+
String entryId = "MY_ENTRY_ID";
31+
32+
deleteEntry(projectId, location, entryGroupId, entryId);
33+
System.out.println("Successfully deleted entry");
34+
}
35+
36+
// Method to delete Entry located in projectId, location, entryGroupId and with entryId
37+
public static void deleteEntry(
38+
String projectId, String location, String entryGroupId, String entryId) throws Exception {
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.
41+
try (CatalogServiceClient client = CatalogServiceClient.create()) {
42+
EntryName entryName = EntryName.of(projectId, location, entryGroupId, entryId);
43+
client.deleteEntry(entryName);
44+
}
45+
}
46+
}
47+
// [END dataplex_delete_entry]
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
/*
2+
* Copyright 2024 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 dataplex;
18+
19+
// [START dataplex_get_entry]
20+
import com.google.cloud.dataplex.v1.CatalogServiceClient;
21+
import com.google.cloud.dataplex.v1.Entry;
22+
import com.google.cloud.dataplex.v1.EntryName;
23+
import com.google.cloud.dataplex.v1.EntryView;
24+
import com.google.cloud.dataplex.v1.GetEntryRequest;
25+
import java.io.IOException;
26+
27+
public class GetEntry {
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+
36+
Entry entry = getEntry(projectId, location, entryGroupId, entryId);
37+
System.out.println("Entry retrieved successfully: " + entry.getName());
38+
entry
39+
.getAspectsMap()
40+
.keySet()
41+
.forEach(aspectKey -> System.out.println("Retrieved aspect for entry: " + aspectKey));
42+
}
43+
44+
// Method to retrieve Entry located in projectId, location, entryGroupId and with entryId
45+
// When Entry is created in Dataplex for example for BigQuery table,
46+
// access permissions might differ between Dataplex and source system.
47+
// "Get" method checks permissions in Dataplex.
48+
// Please also refer how to lookup an Entry, which checks permissions in source system.
49+
public static Entry getEntry(
50+
String projectId, String location, String entryGroupId, String entryId) throws IOException {
51+
// Initialize client that will be used to send requests. This client only needs to be created
52+
// once, and can be reused for multiple requests.
53+
try (CatalogServiceClient client = CatalogServiceClient.create()) {
54+
GetEntryRequest getEntryRequest =
55+
GetEntryRequest.newBuilder()
56+
.setName(EntryName.of(projectId, location, entryGroupId, entryId).toString())
57+
// View determines which Aspects are returned with the Entry.
58+
// For all available options, see:
59+
// https://cloud.google.com/sdk/gcloud/reference/dataplex/entries/lookup#--view
60+
.setView(EntryView.FULL)
61+
// Following 2 lines will be ignored, because "View" is set to FULL.
62+
// Their purpose is to demonstrate how to filter the Aspects returned for Entry
63+
// when "View" is set to CUSTOM.
64+
.addAspectTypes("projects/dataplex-types/locations/global/aspectTypes/generic")
65+
.addPaths("my_path")
66+
.build();
67+
return client.getEntry(getEntryRequest);
68+
}
69+
}
70+
}
71+
// [END dataplex_get_entry]
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
/*
2+
* Copyright 2024 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 dataplex;
18+
19+
// [START dataplex_list_entries]
20+
import com.google.cloud.dataplex.v1.CatalogServiceClient;
21+
import com.google.cloud.dataplex.v1.Entry;
22+
import com.google.cloud.dataplex.v1.EntryGroupName;
23+
import com.google.cloud.dataplex.v1.ListEntriesRequest;
24+
import com.google.common.collect.ImmutableList;
25+
import java.io.IOException;
26+
import java.util.List;
27+
28+
public class ListEntries {
29+
30+
public static void main(String[] args) throws IOException {
31+
// TODO(developer): Replace these variables before running the sample.
32+
String projectId = "MY_PROJECT_ID";
33+
String location = "MY_LOCATION";
34+
String entryGroupId = "MY_ENTRY_GROUP_ID";
35+
36+
List<Entry> entries = listEntries(projectId, location, entryGroupId);
37+
entries.forEach(aspectType -> System.out.println("Entry name: " + aspectType.getName()));
38+
}
39+
40+
// Method to list Entries located in projectId, location and entryGroupId
41+
public static List<Entry> listEntries(String projectId, String location, String entryGroupId)
42+
throws IOException {
43+
// Initialize client that will be used to send requests. This client only needs to be created
44+
// once, and can be reused for multiple requests.
45+
try (CatalogServiceClient client = CatalogServiceClient.create()) {
46+
ListEntriesRequest listEntriesRequest =
47+
ListEntriesRequest.newBuilder()
48+
.setParent(EntryGroupName.of(projectId, location, entryGroupId).toString())
49+
// A filter on the entries to return. Filters are case-sensitive.
50+
// You can filter the request by the following fields:
51+
// * entry_type
52+
// * entry_source.display_name
53+
// To learn more about filters in general, see:
54+
// https://cloud.google.com/sdk/gcloud/reference/topic/filters
55+
.setFilter("entry_type=projects/dataplex-types/locations/global/entryTypes/generic")
56+
.build();
57+
CatalogServiceClient.ListEntriesPagedResponse listEntriesResponse =
58+
client.listEntries(listEntriesRequest);
59+
// Paging is implicitly handled by .iterateAll(), all results will be returned
60+
return ImmutableList.copyOf(listEntriesResponse.iterateAll());
61+
}
62+
}
63+
}
64+
// [END dataplex_list_entries]
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
/*
2+
* Copyright 2024 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 dataplex;
18+
19+
// [START dataplex_lookup_entry]
20+
import com.google.cloud.dataplex.v1.CatalogServiceClient;
21+
import com.google.cloud.dataplex.v1.Entry;
22+
import com.google.cloud.dataplex.v1.EntryName;
23+
import com.google.cloud.dataplex.v1.EntryView;
24+
import com.google.cloud.dataplex.v1.LookupEntryRequest;
25+
import java.io.IOException;
26+
27+
public class LookupEntry {
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+
36+
Entry entry = lookupEntry(projectId, location, entryGroupId, entryId);
37+
System.out.println("Entry retrieved successfully: " + entry.getName());
38+
entry
39+
.getAspectsMap()
40+
.keySet()
41+
.forEach(aspectKey -> System.out.println("Retrieved aspect for entry: " + aspectKey));
42+
}
43+
44+
// Method to retrieve Entry located in projectId, location, entryGroupId and with entryId
45+
// When Entry is created in Dataplex for example for BigQuery table,
46+
// access permissions might differ between Dataplex and source system.
47+
// "Lookup" method checks permissions in source system.
48+
// Please also refer how to get an Entry, which checks permissions in Dataplex.
49+
public static Entry lookupEntry(
50+
String projectId, String location, String entryGroupId, String entryId) throws IOException {
51+
// Initialize client that will be used to send requests. This client only needs to be created
52+
// once, and can be reused for multiple requests.
53+
try (CatalogServiceClient client = CatalogServiceClient.create()) {
54+
String projectLocation = String.format("projects/%s/locations/%s", projectId, location);
55+
LookupEntryRequest lookupEntryRequest =
56+
LookupEntryRequest.newBuilder()
57+
// The project to which the request should be attributed
58+
.setName(projectLocation)
59+
// The resource name of the Entry
60+
.setEntry(EntryName.of(projectId, location, entryGroupId, entryId).toString())
61+
// View determines which Aspects are returned with the Entry.
62+
// For all available options, see:
63+
// https://cloud.google.com/sdk/gcloud/reference/dataplex/entries/lookup#--view
64+
.setView(EntryView.FULL)
65+
// Following 2 lines will be ignored, because "View" is set to FULL.
66+
// Their purpose is to demonstrate how to filter the Aspects returned for Entry
67+
// when "View" is set to CUSTOM.
68+
.addAspectTypes("projects/dataplex-types/locations/global/aspectTypes/generic")
69+
.addPaths("my_path")
70+
.build();
71+
return client.lookupEntry(lookupEntryRequest);
72+
}
73+
}
74+
}
75+
// [END dataplex_lookup_entry]

0 commit comments

Comments
 (0)