-
Notifications
You must be signed in to change notification settings - Fork 2.9k
Add Data Catalog createEntry samples and tests. #1638
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
kurtisvg
merged 15 commits into
GoogleCloudPlatform:master
from
mesmacosta:task/add-create-fileset-entry
Oct 26, 2019
Merged
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
a9b5c7e
Add Data Catalog createEntry samples and tests
mesmacosta 3842dd6
Change second column name
mesmacosta 8b12149
Code review changes
mesmacosta 7ad0bc8
Merge branch 'master' into task/add-create-fileset-entry
mesmacosta daf17bd
Change region tag for the correct one
mesmacosta dc6daea
Merge branch 'task/add-create-fileset-entry' of https://github.com/me…
mesmacosta 6f7d709
Removed tag from region tag since this sample does not tag the entry
mesmacosta 804612d
ADD method overload to show parameter values examples
mesmacosta 6cb3466
Code review changes
mesmacosta 69fb80c
Merge branch 'master' into task/add-create-fileset-entry
mesmacosta 412ad13
Merge branch 'master' into task/add-create-fileset-entry
mesmacosta 1cb15ce
Code review changes
mesmacosta 672343b
Merge branch 'master' into task/add-create-fileset-entry
mesmacosta 8df1a03
Code review and checkstyle changes
mesmacosta 0b12da2
Clean up in a safer order
mesmacosta File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
72 changes: 72 additions & 0 deletions
72
datacatalog/cloud-client/src/main/java/com/example/datacatalog/CreateEntryGroup.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
/* | ||
* Copyright 2019 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 datacatalog_create_entry_group_tag] | ||
|
||
import com.google.api.gax.rpc.AlreadyExistsException; | ||
import com.google.cloud.datacatalog.CreateEntryGroupRequest; | ||
import com.google.cloud.datacatalog.EntryGroup; | ||
import com.google.cloud.datacatalog.LocationName; | ||
import com.google.cloud.datacatalog.v1beta1.DataCatalogClient; | ||
import java.io.IOException; | ||
|
||
public class CreateEntryGroup { | ||
|
||
public static void createEntryGroup() { | ||
// TODO(developer): Replace these variables before running the sample. | ||
String projectId = "my-project-id"; | ||
String entryGroupId = "fileset_entry_group"; | ||
createEntryGroup(projectId, 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"; | ||
|
||
// 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") | ||
.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()); | ||
} | ||
} | ||
} | ||
// [END datacatalog_create_entry_group_tag] |
118 changes: 118 additions & 0 deletions
118
datacatalog/cloud-client/src/main/java/com/example/datacatalog/CreateFilesetEntry.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
/* | ||
* Copyright 2019 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 datacatalog_create_fileset_tag] | ||
|
||
import com.google.api.gax.rpc.AlreadyExistsException; | ||
import com.google.cloud.datacatalog.ColumnSchema; | ||
import com.google.cloud.datacatalog.CreateEntryRequest; | ||
import com.google.cloud.datacatalog.Entry; | ||
import com.google.cloud.datacatalog.EntryGroupName; | ||
import com.google.cloud.datacatalog.EntryType; | ||
import com.google.cloud.datacatalog.GcsFilesetSpec; | ||
import com.google.cloud.datacatalog.Schema; | ||
import com.google.cloud.datacatalog.v1beta1.DataCatalogClient; | ||
import java.io.IOException; | ||
|
||
public class CreateFilesetEntry { | ||
|
||
public static void createEntry() { | ||
// TODO(developer): Replace these variables before running the sample. | ||
String projectId = "my-project-id"; | ||
String entryGroupId = "fileset_entry_group"; | ||
String entryId = "fileset_entry_id"; | ||
createEntry(projectId, entryGroupId, entryId); | ||
} | ||
|
||
// Create Fileset Entry. | ||
public static void createEntry(String projectId, String entryGroupId, String entryId) { | ||
// Currently, Data Catalog stores metadata in the us-central1 region. | ||
String location = "us-central1"; | ||
|
||
// 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 Entry for the Entry request. | ||
Entry entry = | ||
mesmacosta marked this conversation as resolved.
Show resolved
Hide resolved
|
||
Entry.newBuilder() | ||
.setDisplayName("My Fileset") | ||
.setDescription("This fileset consists of ....") | ||
.setGcsFilesetSpec( | ||
GcsFilesetSpec.newBuilder().addFilePatterns("gs://my_bucket/*").build()) | ||
.setSchema( | ||
Schema.newBuilder() | ||
.addColumns( | ||
ColumnSchema.newBuilder() | ||
.setColumn("first_name") | ||
.setDescription("First name") | ||
.setMode("REQUIRED") | ||
.setType("STRING") | ||
.build()) | ||
.addColumns( | ||
ColumnSchema.newBuilder() | ||
.setColumn("last_name") | ||
.setDescription("Last name") | ||
.setMode("REQUIRED") | ||
.setType("STRING") | ||
.build()) | ||
.addColumns( | ||
ColumnSchema.newBuilder() | ||
.setColumn("addresses") | ||
.setDescription("Addresses") | ||
.setMode("REPEATED") | ||
.setType("RECORD") | ||
.addSubcolumns( | ||
ColumnSchema.newBuilder() | ||
.setColumn("city") | ||
.setDescription("City") | ||
.setMode("NULLABLE") | ||
.setType("STRING") | ||
.build()) | ||
.addSubcolumns( | ||
ColumnSchema.newBuilder() | ||
.setColumn("state") | ||
.setDescription("State") | ||
.setMode("NULLABLE") | ||
.setType("STRING") | ||
.build()) | ||
.build()) | ||
.build()) | ||
.setType(EntryType.FILESET) | ||
.build(); | ||
|
||
// Construct the Entry request to be sent by the client. | ||
CreateEntryRequest entryRequest = | ||
CreateEntryRequest.newBuilder() | ||
.setParent(EntryGroupName.of(projectId, location, entryGroupId).toString()) | ||
.setEntryId(entryId) | ||
.setEntry(entry) | ||
.build(); | ||
|
||
// Use the client to send the API request. | ||
Entry entryResponse = dataCatalogClient.createEntry(entryRequest); | ||
System.out.printf("\nEntry created with name: %s\n", entryResponse.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()); | ||
} | ||
} | ||
} | ||
// [END datacatalog_create_fileset_tag] | ||
mesmacosta marked this conversation as resolved.
Show resolved
Hide resolved
|
106 changes: 106 additions & 0 deletions
106
datacatalog/cloud-client/src/test/java/com/example/datacatalog/CreateEntryTests.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
/* | ||
* Copyright 2019 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; | ||
|
||
import static org.junit.Assert.assertThat; | ||
|
||
import com.google.cloud.datacatalog.EntryGroupName; | ||
import com.google.cloud.datacatalog.EntryName; | ||
import com.google.cloud.datacatalog.v1beta1.DataCatalogClient; | ||
import java.io.ByteArrayOutputStream; | ||
import java.io.PrintStream; | ||
import java.util.UUID; | ||
import org.hamcrest.CoreMatchers; | ||
import org.junit.After; | ||
import org.junit.AfterClass; | ||
import org.junit.Before; | ||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
import org.junit.runners.JUnit4; | ||
|
||
/** Integration (system) tests for {@link CreateFilesetEntry} and {@link CreateEntryGroup}. */ | ||
@RunWith(JUnit4.class) | ||
public class CreateEntryTests { | ||
|
||
private ByteArrayOutputStream bout; | ||
|
||
private static String ENTRY_GROUP_ID_NO_CHILDREN = | ||
"entry_group_no_children_" + UUID.randomUUID().toString().substring(0, 8); | ||
private static String PARENT_ENTRY_GROUP_ID = | ||
"fileset_entry_group_parent_" + UUID.randomUUID().toString().substring(0, 8); | ||
private static String ENTRY_ID = | ||
"fileset_entry_id_" + UUID.randomUUID().toString().substring(0, 8); | ||
private static String LOCATION = "us-central1"; | ||
private static String PROJECT_ID = System.getenv().get("GOOGLE_CLOUD_PROJECT"); | ||
|
||
@Before | ||
public void setUp() { | ||
bout = new ByteArrayOutputStream(); | ||
System.setOut(new PrintStream(bout)); | ||
} | ||
|
||
@After | ||
public void tearDown() { | ||
System.setOut(null); | ||
bout.reset(); | ||
} | ||
|
||
@AfterClass | ||
public static void tearDownClass() { | ||
try (DataCatalogClient dataCatalogClient = DataCatalogClient.create()) { | ||
dataCatalogClient.deleteEntryGroup( | ||
EntryGroupName.of(PROJECT_ID, LOCATION, ENTRY_GROUP_ID_NO_CHILDREN).toString()); | ||
|
||
dataCatalogClient.deleteEntry( | ||
EntryName.of(PROJECT_ID, LOCATION, PARENT_ENTRY_GROUP_ID, ENTRY_ID).toString()); | ||
dataCatalogClient.deleteEntryGroup( | ||
EntryGroupName.of(PROJECT_ID, LOCATION, PARENT_ENTRY_GROUP_ID).toString()); | ||
} catch (Exception e) { | ||
System.out.println("Error in cleaning up test data:\n" + e.toString()); | ||
} | ||
} | ||
|
||
@Test | ||
public void testCreateFilesetEntry() { | ||
// Must create a Entry Group before creating the entry. | ||
CreateEntryGroup.createEntryGroup(PROJECT_ID, PARENT_ENTRY_GROUP_ID); | ||
CreateFilesetEntry.createEntry(PROJECT_ID, PARENT_ENTRY_GROUP_ID, ENTRY_ID); | ||
|
||
String output = bout.toString(); | ||
|
||
String entryTemplate = | ||
"Entry created with name: projects/%s/locations/us-central1/entryGroups/%s/entries/%s"; | ||
assertThat( | ||
output, | ||
CoreMatchers.containsString( | ||
String.format(entryTemplate, PROJECT_ID, PARENT_ENTRY_GROUP_ID, ENTRY_ID))); | ||
} | ||
|
||
@Test | ||
public void testCreateEntryGroup() { | ||
CreateEntryGroup.createEntryGroup(PROJECT_ID, ENTRY_GROUP_ID_NO_CHILDREN); | ||
|
||
String output = bout.toString(); | ||
|
||
String entryGroupTemplate = | ||
"Entry Group created with name: projects/%s/locations/us-central1/entryGroups/%s"; | ||
assertThat( | ||
output, | ||
CoreMatchers.containsString( | ||
String.format(entryGroupTemplate, PROJECT_ID, ENTRY_GROUP_ID_NO_CHILDREN))); | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.