Skip to content

Commit b718949

Browse files
jacspa96Jacek Spalinski
andauthored
Dataplex Aspect Type snippets (#9548)
* feat(dataplex): add pom.xml file for dataplex snippets * feat(dataplex): add sample for list Aspect Types * feat(dataplex): add sample for get Aspect Type * feat(dataplex): add sample for delete Aspect Type * feat(dataplex): add sample for create Aspect Type * feat(dataplex): add sample for update Aspect Type * feat(dataplex): add integration test for list Aspect Types * feat(dataplex): add integration test for get Aspect Type * feat(dataplex): add integration test for delete Aspect Type * feat(dataplex): add integration test for create Aspect Type * feat(dataplex): add integration test for update Aspect Type * feat(dataplex): adjust classes to pass lint check * feat(dataplex): add dataplex folder to CODEOWNERS * feat(dataplex): adjust license headers * feat(dataplex): adjust comment for update mask * feat(dataplex): adjust comment in tests * feat(dataplex): move all tests for Aspect Types to single class to reduce number of RPC calls * feat(dataplex): remove region tag from pom.xml * feat(dataplex): make aspectFields function argument and adjust comments * feat(dataplex): adjust tests after changes in code samples * feat(dataplex): add link to documentation for available locations * feat(dataplex): make snippets return values * feat(dataplex): adjust tests to check return values * feat(dataplex): add comments explaining junit annotations * feat(dataplex): adjust test methods names to adhere to naming convention --------- Co-authored-by: Jacek Spalinski <[email protected]>
1 parent ed95aeb commit b718949

File tree

8 files changed

+520
-0
lines changed

8 files changed

+520
-0
lines changed

.github/CODEOWNERS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@
8888
# ---* Shared with DEE Teams
8989
/content-warehouse @GoogleCloudPlatform/java-samples-reviewers @yoshi-approver @GoogleCloudPlatform/cloud-samples-reviewers @GoogleCloudPlatform/googleapis-contentwarehouse
9090
/datacatalog @GoogleCloudPlatform/java-samples-reviewers @yoshi-approver @GoogleCloudPlatform/cloud-samples-reviewers @GoogleCloudPlatform/googleapi-dataplex
91+
/dataplex @GoogleCloudPlatform/java-samples-reviewers @yoshi-approver @GoogleCloudPlatform/cloud-samples-reviewers @GoogleCloudPlatform/googleapi-dataplex
9192
/functions @GoogleCloudPlatform/java-samples-reviewers @yoshi-approver @GoogleCloudPlatform/cloud-samples-reviewers
9293
# ---* Fully Eng Owned
9394

dataplex/snippets/pom.xml

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
7+
<groupId>dataplex</groupId>
8+
<artifactId>dataplex-snippets</artifactId>
9+
<packaging>jar</packaging>
10+
<name>Google Dataplex Snippets</name>
11+
<url>https://github.com/GoogleCloudPlatform/java-docs-samples/tree/main/dataplex</url>
12+
13+
<!--
14+
The parent pom defines common style checks and testing strategies for our samples.
15+
Removing or replacing it should not affect the execution of the samples in any way.
16+
-->
17+
<parent>
18+
<groupId>com.google.cloud.samples</groupId>
19+
<artifactId>shared-configuration</artifactId>
20+
<version>1.2.2</version>
21+
</parent>
22+
23+
<properties>
24+
<maven.compiler.source>11</maven.compiler.source>
25+
<maven.compiler.target>11</maven.compiler.target>
26+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
27+
</properties>
28+
29+
<dependencyManagement>
30+
<dependencies>
31+
<dependency>
32+
<groupId>com.google.cloud</groupId>
33+
<artifactId>libraries-bom</artifactId>
34+
<version>26.47.0</version>
35+
<type>pom</type>
36+
<scope>import</scope>
37+
</dependency>
38+
</dependencies>
39+
</dependencyManagement>
40+
41+
<dependencies>
42+
<dependency>
43+
<groupId>com.google.cloud</groupId>
44+
<artifactId>google-cloud-dataplex</artifactId>
45+
</dependency>
46+
<dependency>
47+
<groupId>junit</groupId>
48+
<artifactId>junit</artifactId>
49+
<version>4.13.2</version>
50+
<scope>test</scope>
51+
</dependency>
52+
<dependency>
53+
<groupId>com.google.truth</groupId>
54+
<artifactId>truth</artifactId>
55+
<version>1.4.4</version>
56+
<scope>test</scope>
57+
</dependency>
58+
</dependencies>
59+
</project>
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
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_aspect_type]
20+
import com.google.cloud.dataplex.v1.AspectType;
21+
import com.google.cloud.dataplex.v1.CatalogServiceClient;
22+
import com.google.cloud.dataplex.v1.LocationName;
23+
import java.util.List;
24+
25+
// Sample to create Aspect Type
26+
public class CreateAspectType {
27+
28+
public static void main(String[] args) throws Exception {
29+
// TODO(developer): Replace these variables before running the sample.
30+
String projectId = "MY_PROJECT_ID";
31+
// Available locations: https://cloud.google.com/dataplex/docs/locations
32+
String location = "MY_LOCATION";
33+
String aspectTypeId = "MY_ASPECT_TYPE_ID";
34+
35+
LocationName locationName = LocationName.of(projectId, location);
36+
AspectType.MetadataTemplate aspectField =
37+
AspectType.MetadataTemplate.newBuilder()
38+
// The name must follow regex ^(([a-zA-Z]{1})([\\w\\-_]{0,62}))$
39+
// That means name must only contain alphanumeric character or dashes or underscores,
40+
// start with an alphabet, and must be less than 63 characters.
41+
.setName("name_of_the_field")
42+
// Metadata Template is recursive structure,
43+
// primitive types such as "string" or "integer" indicate leaf node,
44+
// complex types such as "record" or "array" would require nested Metadata Template
45+
.setType("string")
46+
.setIndex(1)
47+
.setAnnotations(
48+
AspectType.MetadataTemplate.Annotations.newBuilder()
49+
.setDescription("description of the field")
50+
.build())
51+
.setConstraints(
52+
AspectType.MetadataTemplate.Constraints.newBuilder()
53+
// Specifies if field will be required in Aspect Type.
54+
.setRequired(true)
55+
.build())
56+
.build();
57+
List<AspectType.MetadataTemplate> aspectFields = List.of(aspectField);
58+
AspectType createdAspectType = createAspectType(locationName, aspectTypeId, aspectFields);
59+
System.out.println("Successfully created aspect type: " + createdAspectType.getName());
60+
}
61+
62+
public static AspectType createAspectType(
63+
LocationName locationName,
64+
String aspectTypeId,
65+
List<AspectType.MetadataTemplate> aspectFields)
66+
throws Exception {
67+
AspectType aspectType =
68+
AspectType.newBuilder()
69+
.setDescription("description of the aspect type")
70+
.setMetadataTemplate(
71+
AspectType.MetadataTemplate.newBuilder()
72+
// The name must follow regex ^(([a-zA-Z]{1})([\\w\\-_]{0,62}))$
73+
// That means name must only contain alphanumeric character or dashes or
74+
// underscores, start with an alphabet, and must be less than 63 characters.
75+
.setName("name_of_the_template")
76+
.setType("record")
77+
// Aspect Type fields, that themselves are Metadata Templates
78+
.addAllRecordFields(aspectFields)
79+
.build())
80+
.build();
81+
82+
// Initialize client that will be used to send requests. This client only needs to be created
83+
// once, and can be reused for multiple requests. After completing all of your requests, call
84+
// the "close" method on the client to safely clean up any remaining background resources,
85+
// or use "try-with-close" statement to do this automatically.
86+
try (CatalogServiceClient client = CatalogServiceClient.create()) {
87+
return client.createAspectTypeAsync(locationName, aspectType, aspectTypeId).get();
88+
}
89+
}
90+
}
91+
// [END dataplex_create_aspect_type]
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
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_aspect_type]
20+
import com.google.cloud.dataplex.v1.AspectTypeName;
21+
import com.google.cloud.dataplex.v1.CatalogServiceClient;
22+
23+
// Sample to delete Aspect Type
24+
public class DeleteAspectType {
25+
26+
public static void main(String[] args) throws Exception {
27+
// TODO(developer): Replace these variables before running the sample.
28+
String projectId = "MY_PROJECT_ID";
29+
// Available locations: https://cloud.google.com/dataplex/docs/locations
30+
String location = "MY_LOCATION";
31+
String aspectTypeId = "MY_ASPECT_TYPE_ID";
32+
33+
AspectTypeName aspectTypeName = AspectTypeName.of(projectId, location, aspectTypeId);
34+
deleteAspectType(aspectTypeName);
35+
System.out.println("Successfully deleted aspect type");
36+
}
37+
38+
public static void deleteAspectType(AspectTypeName aspectTypeName) 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. After completing all of your requests, call
41+
// the "close" method on the client to safely clean up any remaining background resources,
42+
// or use "try-with-close" statement to do this automatically.
43+
try (CatalogServiceClient client = CatalogServiceClient.create()) {
44+
client.deleteAspectTypeAsync(aspectTypeName).get();
45+
}
46+
}
47+
}
48+
// [END dataplex_delete_aspect_type]
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
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_aspect_type]
20+
import com.google.cloud.dataplex.v1.AspectType;
21+
import com.google.cloud.dataplex.v1.AspectTypeName;
22+
import com.google.cloud.dataplex.v1.CatalogServiceClient;
23+
import java.io.IOException;
24+
25+
// Sample to get Aspect Type
26+
public class GetAspectType {
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+
// Available locations: https://cloud.google.com/dataplex/docs/locations
32+
String location = "MY_LOCATION";
33+
String aspectTypeId = "MY_ASPECT_TYPE_ID";
34+
35+
AspectTypeName aspectTypeName = AspectTypeName.of(projectId, location, aspectTypeId);
36+
AspectType aspectType = getAspectType(aspectTypeName);
37+
System.out.println("Aspect type retrieved successfully: " + aspectType.getName());
38+
}
39+
40+
public static AspectType getAspectType(AspectTypeName aspectTypeName) throws IOException {
41+
// 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.
45+
try (CatalogServiceClient client = CatalogServiceClient.create()) {
46+
return client.getAspectType(aspectTypeName);
47+
}
48+
}
49+
}
50+
// [END dataplex_get_aspect_type]
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
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_aspect_types]
20+
import com.google.cloud.dataplex.v1.AspectType;
21+
import com.google.cloud.dataplex.v1.CatalogServiceClient;
22+
import com.google.cloud.dataplex.v1.LocationName;
23+
import com.google.common.collect.ImmutableList;
24+
import java.io.IOException;
25+
import java.util.List;
26+
27+
// Sample to list Aspect Types
28+
public class ListAspectTypes {
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+
// Available locations: https://cloud.google.com/dataplex/docs/locations
34+
String location = "MY_LOCATION";
35+
36+
LocationName locationName = LocationName.of(projectId, location);
37+
List<AspectType> aspectTypes = listAspectTypes(locationName);
38+
aspectTypes.forEach(
39+
aspectType -> System.out.println("Aspect type name: " + aspectType.getName()));
40+
}
41+
42+
public static List<AspectType> listAspectTypes(LocationName locationName) 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. 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.
47+
try (CatalogServiceClient client = CatalogServiceClient.create()) {
48+
CatalogServiceClient.ListAspectTypesPagedResponse listAspectTypesResponse =
49+
client.listAspectTypes(locationName);
50+
return ImmutableList.copyOf(listAspectTypesResponse.iterateAll());
51+
}
52+
}
53+
}
54+
// [END dataplex_list_aspect_types]

0 commit comments

Comments
 (0)