Skip to content
This repository has been archived by the owner on Sep 16, 2023. It is now read-only.

docs(samples): add Entry, EntryGroup and TagTemplate #394

Merged
merged 5 commits into from Jun 30, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 5 additions & 0 deletions samples/install-without-bom/pom.xml
Expand Up @@ -33,6 +33,11 @@
</dependency>
<!-- [END datacatalog_install_without_bom] -->

<dependency>
<groupId>com.google.protobuf</groupId>
<artifactId>protobuf-java-util</artifactId>
<version>3.14.0</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
Expand Down
5 changes: 5 additions & 0 deletions samples/snapshot/pom.xml
Expand Up @@ -31,6 +31,11 @@
<version>1.0.10</version>
</dependency>

<dependency>
<groupId>com.google.protobuf</groupId>
<artifactId>protobuf-java-util</artifactId>
<version>3.14.0</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
Expand Down
5 changes: 5 additions & 0 deletions samples/snippets/pom.xml
Expand Up @@ -44,6 +44,11 @@
</dependency>
<!-- [END datacatalog_install_with_bom] -->

<dependency>
<groupId>com.google.protobuf</groupId>
<artifactId>protobuf-java-util</artifactId>
<version>3.14.0</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
Expand Down
@@ -0,0 +1,57 @@
/*
* Copyright 2020 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.example.datacatalog;

// [START data_catalog_create_entry]
import com.google.cloud.datacatalog.v1.CreateEntryRequest;
import com.google.cloud.datacatalog.v1.DataCatalogClient;
import com.google.cloud.datacatalog.v1.Entry;
import com.google.cloud.datacatalog.v1.EntryGroupName;
import java.io.IOException;

// Sample to create an entry
public class CreateEntry {

public static void main(String[] args) throws IOException {
// TODO(developer): Replace these variables before running the sample.
String projectId = "MY_PROJECT_ID";
String location = "MY_LOCATION";
String entryGroupId = "MY_ENTRY_GROUP_ID";
String entryId = "MY_ENTRY_ID";
EntryGroupName entryGroupName = EntryGroupName.of(projectId, location, entryGroupId);
Entry entry = Entry.newBuilder().build();
createEntry(entryGroupName, entryId, entry);
}

public static void createEntry(EntryGroupName entryGroupName, String entryId, Entry entry)
throws IOException {
// Initialize client that will be used to send requests. This client only needs to be created
// once, and can be reused for multiple requests. After completing all of your requests, call
// the "close" method on the client to safely clean up any remaining background resources.
try (DataCatalogClient client = DataCatalogClient.create()) {
CreateEntryRequest request =
CreateEntryRequest.newBuilder()
.setParent(entryGroupName.toString())
.setEntryId(entryId)
.setEntry(entry)
.build();
client.createEntry(request);
System.out.println("Entry created successfully");
}
}
}
// [END data_catalog_create_entry]
@@ -1,5 +1,5 @@
/*
* Copyright 2019 Google Inc.
* Copyright 2020 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -16,57 +16,47 @@

package com.example.datacatalog;

// [START datacatalog_create_entry_group_tag]

import com.google.api.gax.rpc.AlreadyExistsException;
// [START data_catalog_create_entry_group]
import com.google.cloud.datacatalog.v1.CreateEntryGroupRequest;
import com.google.cloud.datacatalog.v1.DataCatalogClient;
import com.google.cloud.datacatalog.v1.EntryGroup;
import com.google.cloud.datacatalog.v1.LocationName;
import java.io.IOException;

// Sample to create an entry group
public class CreateEntryGroup {

public static void createEntryGroup() {
public static void main(String[] args) throws IOException {
// TODO(developer): Replace these variables before running the sample.
String projectId = "my-project-id";
String entryGroupId = "fileset_entry_group";
createEntryGroup(projectId, entryGroupId);
String projectId = "MY_PROJECT_ID";
String location = "us-central1";
String entryGroupId = "MY_ENTRY_GROUP_ID";
createEntryGroup(projectId, location, entryGroupId);
}

// Create Entry Group.
public static void createEntryGroup(String projectId, String entryGroupId) {
// Currently, Data Catalog stores metadata in the us-central1 region.
String location = "us-central1";

public static void createEntryGroup(String projectId, String location, String entryGroupId)
throws IOException {
// Initialize client that will be used to send requests. This client only needs to be created
// once, and can be reused for multiple requests. After completing all of your requests, call
// the "close" method on the client to safely clean up any remaining background resources.
try (DataCatalogClient dataCatalogClient = DataCatalogClient.create()) {
// Construct the EntryGroup for the EntryGroup request.
EntryGroup entryGroup =
EntryGroup.newBuilder()
.setDisplayName("My Fileset Entry Group")
.setDisplayName("MY Entry Group")
.setDescription("This Entry Group consists of ....")
.build();

// Construct the EntryGroup request to be sent by the client.
CreateEntryGroupRequest entryGroupRequest =
CreateEntryGroupRequest.newBuilder()
.setParent(LocationName.of(projectId, location).toString())
.setEntryGroupId(entryGroupId)
.setEntryGroup(entryGroup)
.build();

// Use the client to send the API request.
EntryGroup entryGroupResponse = dataCatalogClient.createEntryGroup(entryGroupRequest);
System.out.printf("\nEntry Group created with name: %s\n", entryGroupResponse.getName());
} catch (AlreadyExistsException | IOException e) {
// AlreadyExistsException's are thrown if EntryGroup or Entry already exists.
// IOException's are thrown when unable to create the DataCatalogClient,
// for example an invalid Service Account path.
System.out.println("Error in create entry process:\n" + e.toString());
dataCatalogClient.createEntryGroup(entryGroupRequest);
System.out.println("Entry Group created successfully");
}
}
}
// [END datacatalog_create_entry_group_tag]
// [END data_catalog_create_entry_group]
Expand Up @@ -115,4 +115,4 @@ public static void createEntry(String projectId, String entryGroupId, String ent
}
}
}
// [END datacatalog_create_fileset_tag]
// [END datacatalog_create_fileset_tag]
@@ -0,0 +1,68 @@
/*
* Copyright 2020 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.example.datacatalog;

// [START data_catalog_create_tag_template]
import com.google.cloud.datacatalog.v1.CreateTagTemplateRequest;
import com.google.cloud.datacatalog.v1.DataCatalogClient;
import com.google.cloud.datacatalog.v1.FieldType;
import com.google.cloud.datacatalog.v1.LocationName;
import com.google.cloud.datacatalog.v1.TagTemplate;
import com.google.cloud.datacatalog.v1.TagTemplateField;
import java.io.IOException;

// Sample to create tag template
public class CreateTagTemplate {

public static void main(String[] args) throws IOException {
// TODO(developer): Replace these variables before running the sample.
String projectId = "MY_PROJECT_ID";
String location = "MY_LOCATION";
LocationName locationName = LocationName.of(projectId, location);
String tagTemplateId = "MY_TAG_TEMPLATE_ID";
TagTemplateField sourceField =
TagTemplateField.newBuilder()
.setDisplayName("Your display name")
.setType(
FieldType.newBuilder().setPrimitiveType(FieldType.PrimitiveType.STRING).build())
.build();
TagTemplate tagTemplate =
TagTemplate.newBuilder()
.setDisplayName("Your display name")
.putFields("sourceField", sourceField)
.build();
createTagTemplate(locationName, tagTemplateId, tagTemplate);
}

public static void createTagTemplate(
LocationName name, String tagTemplateId, TagTemplate template) throws IOException {
// Initialize client that will be used to send requests. This client only needs to be created
// once, and can be reused for multiple requests. After completing all of your requests, call
// the "close" method on the client to safely clean up any remaining background resources.
try (DataCatalogClient client = DataCatalogClient.create()) {
CreateTagTemplateRequest request =
CreateTagTemplateRequest.newBuilder()
.setParent(name.toString())
.setTagTemplateId(tagTemplateId)
.setTagTemplate(template)
.build();
client.createTagTemplate(request);
System.out.println("Tag template created successfully");
}
}
}
// [END data_catalog_create_tag_template]
@@ -0,0 +1,50 @@
/*
* Copyright 2020 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.example.datacatalog;

// [START data_catalog_delete_entry]
import com.google.cloud.datacatalog.v1.DataCatalogClient;
import com.google.cloud.datacatalog.v1.DeleteEntryRequest;
import com.google.cloud.datacatalog.v1.EntryName;
import java.io.IOException;

// Sample to delete a entry
public class DeleteEntry {

public static void main(String[] args) throws IOException {
// TODO(developer): Replace these variables before running the sample.
String projectId = "MY_PROJECT_ID";
String location = "MY_LOCATION";
String entryGroupId = "MY_ENTRY_GROUP_ID";
String entryId = "MY_ENTRY_ID";
EntryName entryName = EntryName.of(projectId, location, entryGroupId, entryId);
deleteEntry(entryName);
}

public static void deleteEntry(EntryName entryName) throws IOException {
// Initialize client that will be used to send requests. This client only needs to be created
// once, and can be reused for multiple requests. After completing all of your requests, call
// the "close" method on the client to safely clean up any remaining background resources.
try (DataCatalogClient client = DataCatalogClient.create()) {
DeleteEntryRequest request =
DeleteEntryRequest.newBuilder().setName(entryName.toString()).build();
client.deleteEntry(request);
System.out.println("Entry deleted successfully");
}
}
}
// [END data_catalog_delete_entry]
@@ -0,0 +1,49 @@
/*
* Copyright 2020 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.example.datacatalog;

// [START data_catalog_delete_entry_group]
import com.google.cloud.datacatalog.v1.DataCatalogClient;
import com.google.cloud.datacatalog.v1.DeleteEntryGroupRequest;
import com.google.cloud.datacatalog.v1.EntryGroupName;
import java.io.IOException;

// Sample to delete a entry group
public class DeleteEntryGroup {

public static void main(String[] args) throws IOException {
// TODO(developer): Replace these variables before running the sample.
String projectId = "MY_PROJECT_ID";
String location = "MY_LOCATION";
String entryGroupId = "MY_ENTRY_GROUP_ID";
EntryGroupName entryGroupName = EntryGroupName.of(projectId, location, entryGroupId);
deleteEntryGroup(entryGroupName);
}

public static void deleteEntryGroup(EntryGroupName entryGroupName) throws IOException {
// Initialize client that will be used to send requests. This client only needs to be created
// once, and can be reused for multiple requests. After completing all of your requests, call
// the "close" method on the client to safely clean up any remaining background resources.
try (DataCatalogClient client = DataCatalogClient.create()) {
DeleteEntryGroupRequest request =
DeleteEntryGroupRequest.newBuilder().setName(entryGroupName.toString()).build();
client.deleteEntryGroup(request);
System.out.println("Entry group deleted successfully");
}
}
}
// [END data_catalog_delete_entry_group]
@@ -0,0 +1,49 @@
/*
* Copyright 2020 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.example.datacatalog;

// [START data_catalog_delete_tag_template]
import com.google.cloud.datacatalog.v1.DataCatalogClient;
import com.google.cloud.datacatalog.v1.DeleteTagTemplateRequest;
import com.google.cloud.datacatalog.v1.TagTemplateName;
import java.io.IOException;

// Sample to delete tag template
public class DeleteTagTemplate {

public static void main(String[] args) throws IOException {
// TODO(developer): Replace these variables before running the sample.
String projectId = "MY_PROJECT_ID";
String location = "MY_LOCATION";
String tagTemplateId = "MY_TAG_TEMPLATE_ID";
TagTemplateName tagTemplate = TagTemplateName.of(projectId, location, tagTemplateId);
deleteTagTemplate(tagTemplate);
}

public static void deleteTagTemplate(TagTemplateName template) throws IOException {
// Initialize client that will be used to send requests. This client only needs to be created
// once, and can be reused for multiple requests. After completing all of your requests, call
// the "close" method on the client to safely clean up any remaining background resources.
try (DataCatalogClient client = DataCatalogClient.create()) {
DeleteTagTemplateRequest request =
DeleteTagTemplateRequest.newBuilder().setName(template.toString()).setForce(true).build();
client.deleteTagTemplate(request);
System.out.println("Tag template deleted successfully");
}
}
}
// [END data_catalog_delete_tag_template]