Skip to content

Commit 286dd44

Browse files
jacspa96Jacek Spalinski
andauthored
feat(dataplex): adjust code snippets (#9608)
* feat(dataplex): adjust snippets * feat(dataplex): fix small typos in tests * feat(dataplex): small adjustment in tests --------- Co-authored-by: Jacek Spalinski <[email protected]>
1 parent 55d96d4 commit 286dd44

18 files changed

+128
-169
lines changed

dataplex/snippets/src/main/java/dataplex/CreateAspectType.java

Lines changed: 18 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
import com.google.cloud.dataplex.v1.LocationName;
2323
import java.util.List;
2424

25-
// Sample to create Aspect Type
2625
public class CreateAspectType {
2726

2827
public static void main(String[] args) throws Exception {
@@ -59,33 +58,32 @@ public static void main(String[] args) throws Exception {
5958
System.out.println("Successfully created aspect type: " + createdAspectType.getName());
6059
}
6160

61+
// Method to create Aspect Type located in projectId, location and with aspectTypeId and
62+
// aspectFields specifying schema of the Aspect Type
6263
public static AspectType createAspectType(
6364
String projectId,
6465
String location,
6566
String aspectTypeId,
6667
List<AspectType.MetadataTemplate> aspectFields)
6768
throws Exception {
68-
LocationName locationName = LocationName.of(projectId, location);
69-
AspectType aspectType =
70-
AspectType.newBuilder()
71-
.setDescription("description of the aspect type")
72-
.setMetadataTemplate(
73-
AspectType.MetadataTemplate.newBuilder()
74-
// The name must follow regex ^(([a-zA-Z]{1})([\\w\\-_]{0,62}))$
75-
// That means name must only contain alphanumeric character or dashes or
76-
// underscores, start with an alphabet, and must be less than 63 characters.
77-
.setName("name_of_the_template")
78-
.setType("record")
79-
// Aspect Type fields, that themselves are Metadata Templates
80-
.addAllRecordFields(aspectFields)
81-
.build())
82-
.build();
83-
8469
// Initialize client that will be used to send requests. This client only needs to be created
85-
// once, and can be reused for multiple requests. After completing all of your requests, call
86-
// the "close" method on the client to safely clean up any remaining background resources,
87-
// or use "try-with-close" statement to do this automatically.
70+
// once, and can be reused for multiple requests.
8871
try (CatalogServiceClient client = CatalogServiceClient.create()) {
72+
LocationName locationName = LocationName.of(projectId, location);
73+
AspectType aspectType =
74+
AspectType.newBuilder()
75+
.setDescription("description of the aspect type")
76+
.setMetadataTemplate(
77+
AspectType.MetadataTemplate.newBuilder()
78+
// The name must follow regex ^(([a-zA-Z]{1})([\\w\\-_]{0,62}))$
79+
// That means name must only contain alphanumeric character or dashes or
80+
// underscores, start with an alphabet, and must be less than 63 characters.
81+
.setName("name_of_the_template")
82+
.setType("record")
83+
// Aspect Type fields, that themselves are Metadata Templates
84+
.addAllRecordFields(aspectFields)
85+
.build())
86+
.build();
8987
return client.createAspectTypeAsync(locationName, aspectType, aspectTypeId).get();
9088
}
9189
}

dataplex/snippets/src/main/java/dataplex/CreateEntryGroup.java

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
import com.google.cloud.dataplex.v1.EntryGroup;
2222
import com.google.cloud.dataplex.v1.LocationName;
2323

24-
// Samples to create Entry Group
2524
public class CreateEntryGroup {
2625

2726
public static void main(String[] args) throws Exception {
@@ -35,17 +34,15 @@ public static void main(String[] args) throws Exception {
3534
System.out.println("Successfully created entry group: " + createdEntryGroup.getName());
3635
}
3736

37+
// Method to create Entry Group located in projectId, location and with entryGroupId
3838
public static EntryGroup createEntryGroup(String projectId, String location, String entryGroupId)
3939
throws Exception {
40-
LocationName locationName = LocationName.of(projectId, location);
41-
EntryGroup entryGroup =
42-
EntryGroup.newBuilder().setDescription("description of the entry group").build();
43-
4440
// Initialize client that will be used to send requests. This client only needs to be created
45-
// once, and can be reused for multiple requests. After completing all of your requests, call
46-
// the "close" method on the client to safely clean up any remaining background resources,
47-
// or use "try-with-close" statement to do this automatically.
41+
// once, and can be reused for multiple requests.
4842
try (CatalogServiceClient client = CatalogServiceClient.create()) {
43+
LocationName locationName = LocationName.of(projectId, location);
44+
EntryGroup entryGroup =
45+
EntryGroup.newBuilder().setDescription("description of the entry group").build();
4946
return client.createEntryGroupAsync(locationName, entryGroup, entryGroupId).get();
5047
}
5148
}

dataplex/snippets/src/main/java/dataplex/CreateEntryType.java

Lines changed: 16 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
import com.google.cloud.dataplex.v1.EntryType;
2222
import com.google.cloud.dataplex.v1.LocationName;
2323

24-
// Samples to create Entry Type
2524
public class CreateEntryType {
2625

2726
public static void main(String[] args) throws Exception {
@@ -35,27 +34,26 @@ public static void main(String[] args) throws Exception {
3534
System.out.println("Successfully created entry type: " + createdEntryType.getName());
3635
}
3736

37+
// Method to create Entry Type located in projectId, location and with entryTypeId
3838
public static EntryType createEntryType(String projectId, String location, String entryTypeId)
3939
throws Exception {
40-
LocationName locationName = LocationName.of(projectId, location);
41-
EntryType entryType =
42-
EntryType.newBuilder()
43-
.setDescription("description of the entry type")
44-
// Required aspects will need to be attached to every entry created for this entry type.
45-
// You cannot change required aspects for entry type once it is created.
46-
.addRequiredAspects(
47-
EntryType.AspectInfo.newBuilder()
48-
// Example of system aspect type.
49-
// It is also possible to specify custom aspect type.
50-
.setType("projects/dataplex-types/locations/global/aspectTypes/schema")
51-
.build())
52-
.build();
53-
5440
// Initialize client that will be used to send requests. This client only needs to be created
55-
// once, and can be reused for multiple requests. After completing all of your requests, call
56-
// the "close" method on the client to safely clean up any remaining background resources,
57-
// or use "try-with-close" statement to do this automatically.
41+
// once, and can be reused for multiple requests.
5842
try (CatalogServiceClient client = CatalogServiceClient.create()) {
43+
LocationName locationName = LocationName.of(projectId, location);
44+
EntryType entryType =
45+
EntryType.newBuilder()
46+
.setDescription("description of the entry type")
47+
// Required aspects will need to be attached to every entry created for this entry
48+
// type.
49+
// You cannot change required aspects for entry type once it is created.
50+
.addRequiredAspects(
51+
EntryType.AspectInfo.newBuilder()
52+
// Example of system aspect type.
53+
// It is also possible to specify custom aspect type.
54+
.setType("projects/dataplex-types/locations/global/aspectTypes/schema")
55+
.build())
56+
.build();
5957
return client.createEntryTypeAsync(locationName, entryType, entryTypeId).get();
6058
}
6159
}

dataplex/snippets/src/main/java/dataplex/DeleteAspectType.java

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
import com.google.cloud.dataplex.v1.AspectTypeName;
2121
import com.google.cloud.dataplex.v1.CatalogServiceClient;
2222

23-
// Sample to delete Aspect Type
2423
public class DeleteAspectType {
2524

2625
public static void main(String[] args) throws Exception {
@@ -34,15 +33,13 @@ public static void main(String[] args) throws Exception {
3433
System.out.println("Successfully deleted aspect type");
3534
}
3635

36+
// Method to delete Aspect Type located in projectId, location and with aspectTypeId
3737
public static void deleteAspectType(String projectId, String location, String aspectTypeId)
3838
throws Exception {
39-
AspectTypeName aspectTypeName = AspectTypeName.of(projectId, location, aspectTypeId);
40-
4139
// Initialize client that will be used to send requests. This client only needs to be created
42-
// once, and can be reused for multiple requests. After completing all of your requests, call
43-
// the "close" method on the client to safely clean up any remaining background resources,
44-
// or use "try-with-close" statement to do this automatically.
40+
// once, and can be reused for multiple requests.
4541
try (CatalogServiceClient client = CatalogServiceClient.create()) {
42+
AspectTypeName aspectTypeName = AspectTypeName.of(projectId, location, aspectTypeId);
4643
client.deleteAspectTypeAsync(aspectTypeName).get();
4744
}
4845
}

dataplex/snippets/src/main/java/dataplex/DeleteEntryGroup.java

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
import com.google.cloud.dataplex.v1.CatalogServiceClient;
2121
import com.google.cloud.dataplex.v1.EntryGroupName;
2222

23-
// Sample to delete Entry Group
2423
public class DeleteEntryGroup {
2524

2625
public static void main(String[] args) throws Exception {
@@ -34,15 +33,13 @@ public static void main(String[] args) throws Exception {
3433
System.out.println("Successfully deleted entry group");
3534
}
3635

36+
// Method to delete Entry Group located in projectId, location and with entryGroupId
3737
public static void deleteEntryGroup(String projectId, String location, String entryGroupId)
3838
throws Exception {
39-
EntryGroupName entryGroupName = EntryGroupName.of(projectId, location, entryGroupId);
40-
4139
// Initialize client that will be used to send requests. This client only needs to be created
42-
// once, and can be reused for multiple requests. After completing all of your requests, call
43-
// the "close" method on the client to safely clean up any remaining background resources,
44-
// or use "try-with-close" statement to do this automatically.
40+
// once, and can be reused for multiple requests.
4541
try (CatalogServiceClient client = CatalogServiceClient.create()) {
42+
EntryGroupName entryGroupName = EntryGroupName.of(projectId, location, entryGroupId);
4643
client.deleteEntryGroupAsync(entryGroupName).get();
4744
}
4845
}

dataplex/snippets/src/main/java/dataplex/DeleteEntryType.java

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
import com.google.cloud.dataplex.v1.CatalogServiceClient;
2121
import com.google.cloud.dataplex.v1.EntryTypeName;
2222

23-
// Sample to delete Entry Type
2423
public class DeleteEntryType {
2524

2625
public static void main(String[] args) throws Exception {
@@ -34,15 +33,13 @@ public static void main(String[] args) throws Exception {
3433
System.out.println("Successfully deleted entry type");
3534
}
3635

36+
// Method to delete Entry Type located in projectId, location and with entryTypeId
3737
public static void deleteEntryType(String projectId, String location, String entryTypeId)
3838
throws Exception {
39-
EntryTypeName entryTypeName = EntryTypeName.of(projectId, location, entryTypeId);
40-
4139
// Initialize client that will be used to send requests. This client only needs to be created
42-
// once, and can be reused for multiple requests. After completing all of your requests, call
43-
// the "close" method on the client to safely clean up any remaining background resources,
44-
// or use "try-with-close" statement to do this automatically.
40+
// once, and can be reused for multiple requests.
4541
try (CatalogServiceClient client = CatalogServiceClient.create()) {
42+
EntryTypeName entryTypeName = EntryTypeName.of(projectId, location, entryTypeId);
4643
client.deleteEntryTypeAsync(entryTypeName).get();
4744
}
4845
}

dataplex/snippets/src/main/java/dataplex/GetAspectType.java

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
import com.google.cloud.dataplex.v1.CatalogServiceClient;
2323
import java.io.IOException;
2424

25-
// Sample to get Aspect Type
2625
public class GetAspectType {
2726

2827
public static void main(String[] args) throws IOException {
@@ -36,15 +35,13 @@ public static void main(String[] args) throws IOException {
3635
System.out.println("Aspect type retrieved successfully: " + aspectType.getName());
3736
}
3837

38+
// Method to retrieve Aspect Type located in projectId, location and with aspectTypeId
3939
public static AspectType getAspectType(String projectId, String location, String aspectTypeId)
4040
throws IOException {
41-
AspectTypeName aspectTypeName = AspectTypeName.of(projectId, location, aspectTypeId);
42-
4341
// 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. After completing all of your requests, call
45-
// the "close" method on the client to safely clean up any remaining background resources,
46-
// or use "try-with-close" statement to do this automatically.
42+
// once, and can be reused for multiple requests.
4743
try (CatalogServiceClient client = CatalogServiceClient.create()) {
44+
AspectTypeName aspectTypeName = AspectTypeName.of(projectId, location, aspectTypeId);
4845
return client.getAspectType(aspectTypeName);
4946
}
5047
}

dataplex/snippets/src/main/java/dataplex/GetEntryGroup.java

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
import com.google.cloud.dataplex.v1.EntryGroupName;
2323
import java.io.IOException;
2424

25-
// Sample to get Entry Group
2625
public class GetEntryGroup {
2726

2827
public static void main(String[] args) throws IOException {
@@ -36,15 +35,13 @@ public static void main(String[] args) throws IOException {
3635
System.out.println("Entry group retrieved successfully: " + entryGroup.getName());
3736
}
3837

38+
// Method to retrieve Entry Group located in projectId, location and with entryGroupId
3939
public static EntryGroup getEntryGroup(String projectId, String location, String entryGroupId)
4040
throws IOException {
41-
EntryGroupName entryGroupName = EntryGroupName.of(projectId, location, entryGroupId);
42-
4341
// 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. After completing all of your requests, call
45-
// the "close" method on the client to safely clean up any remaining background resources,
46-
// or use "try-with-close" statement to do this automatically.
42+
// once, and can be reused for multiple requests.
4743
try (CatalogServiceClient client = CatalogServiceClient.create()) {
44+
EntryGroupName entryGroupName = EntryGroupName.of(projectId, location, entryGroupId);
4845
return client.getEntryGroup(entryGroupName);
4946
}
5047
}

dataplex/snippets/src/main/java/dataplex/GetEntryType.java

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
import com.google.cloud.dataplex.v1.EntryTypeName;
2323
import java.io.IOException;
2424

25-
// Sample to get Entry Type
2625
public class GetEntryType {
2726

2827
public static void main(String[] args) throws IOException {
@@ -36,15 +35,13 @@ public static void main(String[] args) throws IOException {
3635
System.out.println("Entry type retrieved successfully: " + entryType.getName());
3736
}
3837

38+
// Method to retrieve Entry Type located in projectId, location and with entryTypeId
3939
public static EntryType getEntryType(String projectId, String location, String entryTypeId)
4040
throws IOException {
41-
EntryTypeName entryTypeName = EntryTypeName.of(projectId, location, entryTypeId);
42-
4341
// 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. After completing all of your requests, call
45-
// the "close" method on the client to safely clean up any remaining background resources,
46-
// or use "try-with-close" statement to do this automatically.
42+
// once, and can be reused for multiple requests.
4743
try (CatalogServiceClient client = CatalogServiceClient.create()) {
44+
EntryTypeName entryTypeName = EntryTypeName.of(projectId, location, entryTypeId);
4845
return client.getEntryType(entryTypeName);
4946
}
5047
}

dataplex/snippets/src/main/java/dataplex/ListAspectTypes.java

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424
import java.io.IOException;
2525
import java.util.List;
2626

27-
// Sample to list Aspect Types
2827
public class ListAspectTypes {
2928

3029
public static void main(String[] args) throws IOException {
@@ -38,15 +37,13 @@ public static void main(String[] args) throws IOException {
3837
aspectType -> System.out.println("Aspect type name: " + aspectType.getName()));
3938
}
4039

40+
// Method to list Aspect Types located in projectId and location
4141
public static List<AspectType> listAspectTypes(String projectId, String location)
4242
throws IOException {
43-
LocationName locationName = LocationName.of(projectId, location);
44-
4543
// 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. After completing all of your requests, call
47-
// the "close" method on the client to safely clean up any remaining background resources,
48-
// or use "try-with-close" statement to do this automatically.
44+
// once, and can be reused for multiple requests.
4945
try (CatalogServiceClient client = CatalogServiceClient.create()) {
46+
LocationName locationName = LocationName.of(projectId, location);
5047
CatalogServiceClient.ListAspectTypesPagedResponse listAspectTypesResponse =
5148
client.listAspectTypes(locationName);
5249
// Paging is implicitly handled by .iterateAll(), all results will be returned

0 commit comments

Comments
 (0)