Skip to content

Commit

Permalink
samples: add generated samples (#83)
Browse files Browse the repository at this point in the history
  • Loading branch information
yoshi-automation authored Feb 5, 2020
0 parents commit a5070a7
Show file tree
Hide file tree
Showing 4 changed files with 375 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
/*
* 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
*
* https://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.
*/
// DO NOT EDIT! This is a generated sample ("Request", "datacatalog_get_entry")
// sample-metadata:
// title:
// description: Get Entry
// usage: gradle run -PmainClass=com.google.cloud.examples.datacatalog.v1beta1.DatacatalogGetEntry
// [--args='[--project_id "[Google Cloud Project ID]"] [--location_id "[Google Cloud Location ID]"]
// [--entry_group_id "[Entry Group ID]"] [--entry_id "[Entry ID]"]']

package com.google.cloud.examples.datacatalog.v1beta1;

import com.google.cloud.datacatalog.v1beta1.DataCatalogClient;
import com.google.cloud.datacatalog.v1beta1.Entry;
import com.google.cloud.datacatalog.v1beta1.GetEntryRequest;
import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.DefaultParser;
import org.apache.commons.cli.Option;
import org.apache.commons.cli.Options;

public class DatacatalogGetEntry {
// [START datacatalog_get_entry]
/*
* Please include the following imports to run this sample.
*
* import com.google.cloud.datacatalog.v1beta1.DataCatalogClient;
* import com.google.cloud.datacatalog.v1beta1.Entry;
* import com.google.cloud.datacatalog.v1beta1.GetEntryRequest;
*/

public static void sampleGetEntry() {
// TODO(developer): Replace these variables before running the sample.
String projectId = "[Google Cloud Project ID]";
String locationId = "[Google Cloud Location ID]";
String entryGroupId = "[Entry Group ID]";
String entryId = "[Entry ID]";
sampleGetEntry(projectId, locationId, entryGroupId, entryId);
}

/**
* Get Entry
*
* @param projectId Your Google Cloud project ID
* @param locationId Google Cloud region, e.g. us-central1
* @param entryGroupId ID of the Entry Group, e.g. {@literal @}bigquery, {@literal @}pubsub,
* my_entry_group
* @param entryId ID of the Entry
*/
public static void sampleGetEntry(
String projectId, String locationId, String entryGroupId, String entryId) {
try (DataCatalogClient dataCatalogClient = DataCatalogClient.create()) {
String formattedName =
DataCatalogClient.formatEntryName(projectId, locationId, entryGroupId, entryId);
GetEntryRequest request = GetEntryRequest.newBuilder().setName(formattedName).build();
Entry response = dataCatalogClient.getEntry(request);
Entry entry = response;
System.out.printf("Entry name: %s\n", entry.getName());
System.out.printf("Entry type: %s\n", entry.getType());
System.out.printf("Linked resource: %s\n", entry.getLinkedResource());
} catch (Exception exception) {
System.err.println("Failed to create the client due to: " + exception);
}
}
// [END datacatalog_get_entry]

public static void main(String[] args) throws Exception {
Options options = new Options();
options.addOption(
Option.builder("").required(false).hasArg(true).longOpt("project_id").build());
options.addOption(
Option.builder("").required(false).hasArg(true).longOpt("location_id").build());
options.addOption(
Option.builder("").required(false).hasArg(true).longOpt("entry_group_id").build());
options.addOption(Option.builder("").required(false).hasArg(true).longOpt("entry_id").build());

CommandLine cl = (new DefaultParser()).parse(options, args);
String projectId = cl.getOptionValue("project_id", "[Google Cloud Project ID]");
String locationId = cl.getOptionValue("location_id", "[Google Cloud Location ID]");
String entryGroupId = cl.getOptionValue("entry_group_id", "[Entry Group ID]");
String entryId = cl.getOptionValue("entry_id", "[Entry ID]");

sampleGetEntry(projectId, locationId, entryGroupId, entryId);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
/*
* 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
*
* https://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.
*/
// DO NOT EDIT! This is a generated sample ("Request", "datacatalog_lookup_entry")
// sample-metadata:
// title:
// description: Lookup Entry
// usage: gradle run
// -PmainClass=com.google.cloud.examples.datacatalog.v1beta1.DatacatalogLookupEntry
// [--args='[--resource_name "[Full Resource Name]"]']

package com.google.cloud.examples.datacatalog.v1beta1;

import com.google.cloud.datacatalog.v1beta1.DataCatalogClient;
import com.google.cloud.datacatalog.v1beta1.Entry;
import com.google.cloud.datacatalog.v1beta1.LookupEntryRequest;
import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.DefaultParser;
import org.apache.commons.cli.Option;
import org.apache.commons.cli.Options;

public class DatacatalogLookupEntry {
// [START datacatalog_lookup_entry]
/*
* Please include the following imports to run this sample.
*
* import com.google.cloud.datacatalog.v1beta1.DataCatalogClient;
* import com.google.cloud.datacatalog.v1beta1.Entry;
* import com.google.cloud.datacatalog.v1beta1.LookupEntryRequest;
*/

public static void sampleLookupEntry() {
// TODO(developer): Replace these variables before running the sample.
String resourceName = "[Full Resource Name]";
sampleLookupEntry(resourceName);
}

/**
* Lookup Entry
*
* @param resourceName The full name of the Google Cloud Platform resource the Data Catalog entry
* represents. See: https://cloud.google.com/apis/design/resource_names#full_resource_name
* Examples:
* //bigquery.googleapis.com/projects/bigquery-public-data/datasets/new_york_taxi_trips/tables/taxi_zone_geom
* //pubsub.googleapis.com/projects/pubsub-public-data/topics/taxirides-realtime
*/
public static void sampleLookupEntry(String resourceName) {
try (DataCatalogClient dataCatalogClient = DataCatalogClient.create()) {
LookupEntryRequest request =
LookupEntryRequest.newBuilder().setLinkedResource(resourceName).build();
Entry response = dataCatalogClient.lookupEntry(request);
Entry entry = response;
System.out.printf("Entry name: %s\n", entry.getName());
System.out.printf("Entry type: %s\n", entry.getType());
System.out.printf("Linked resource: %s\n", entry.getLinkedResource());
} catch (Exception exception) {
System.err.println("Failed to create the client due to: " + exception);
}
}
// [END datacatalog_lookup_entry]

public static void main(String[] args) throws Exception {
Options options = new Options();
options.addOption(
Option.builder("").required(false).hasArg(true).longOpt("resource_name").build());

CommandLine cl = (new DefaultParser()).parse(options, args);
String resourceName = cl.getOptionValue("resource_name", "[Full Resource Name]");

sampleLookupEntry(resourceName);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
/*
* 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
*
* https://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.
*/
// DO NOT EDIT! This is a generated sample ("Request", "datacatalog_lookup_entry_sql_resource")
// sample-metadata:
// title:
// description: Lookup Entry using SQL resource
// usage: gradle run
// -PmainClass=com.google.cloud.examples.datacatalog.v1beta1.DatacatalogLookupEntrySqlResource
// [--args='[--sql_name "[SQL Resource Name]"]']

package com.google.cloud.examples.datacatalog.v1beta1;

import com.google.cloud.datacatalog.v1beta1.DataCatalogClient;
import com.google.cloud.datacatalog.v1beta1.Entry;
import com.google.cloud.datacatalog.v1beta1.LookupEntryRequest;
import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.DefaultParser;
import org.apache.commons.cli.Option;
import org.apache.commons.cli.Options;

public class DatacatalogLookupEntrySqlResource {
// [START datacatalog_lookup_entry_sql_resource]
/*
* Please include the following imports to run this sample.
*
* import com.google.cloud.datacatalog.v1beta1.DataCatalogClient;
* import com.google.cloud.datacatalog.v1beta1.Entry;
* import com.google.cloud.datacatalog.v1beta1.LookupEntryRequest;
*/

public static void sampleLookupEntry() {
// TODO(developer): Replace these variables before running the sample.
String sqlName = "[SQL Resource Name]";
sampleLookupEntry(sqlName);
}

/**
* Lookup Entry using SQL resource
*
* @param sqlName The SQL name of the Google Cloud Platform resource the Data Catalog entry
* represents. Examples:
* bigquery.table.`bigquery-public-data`.new_york_taxi_trips.taxi_zone_geom
* pubsub.topic.`pubsub-public-data`.`taxirides-realtime`
*/
public static void sampleLookupEntry(String sqlName) {
try (DataCatalogClient dataCatalogClient = DataCatalogClient.create()) {
LookupEntryRequest request = LookupEntryRequest.newBuilder().setSqlResource(sqlName).build();
Entry response = dataCatalogClient.lookupEntry(request);
Entry entry = response;
System.out.printf("Entry name: %s\n", entry.getName());
System.out.printf("Entry type: %s\n", entry.getType());
System.out.printf("Linked resource: %s\n", entry.getLinkedResource());
} catch (Exception exception) {
System.err.println("Failed to create the client due to: " + exception);
}
}
// [END datacatalog_lookup_entry_sql_resource]

public static void main(String[] args) throws Exception {
Options options = new Options();
options.addOption(Option.builder("").required(false).hasArg(true).longOpt("sql_name").build());

CommandLine cl = (new DefaultParser()).parse(options, args);
String sqlName = cl.getOptionValue("sql_name", "[SQL Resource Name]");

sampleLookupEntry(sqlName);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
/*
* 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
*
* https://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.
*/
// DO NOT EDIT! This is a generated sample ("RequestPaged", "datacatalog_search")
// sample-metadata:
// title:
// description: Search Catalog
// usage: gradle run -PmainClass=com.google.cloud.examples.datacatalog.v1beta1.DatacatalogSearch
// [--args='[--include_project_id "[Google Cloud Project ID]"] [--include_gcp_public_datasets false]
// [--query "[String in search query syntax]"]']

package com.google.cloud.examples.datacatalog.v1beta1;

import com.google.cloud.datacatalog.v1beta1.DataCatalogClient;
import com.google.cloud.datacatalog.v1beta1.SearchCatalogRequest;
import com.google.cloud.datacatalog.v1beta1.SearchCatalogResult;
import java.util.Arrays;
import java.util.List;
import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.DefaultParser;
import org.apache.commons.cli.Option;
import org.apache.commons.cli.Options;

public class DatacatalogSearch {
// [START datacatalog_search]
/*
* Please include the following imports to run this sample.
*
* import com.google.cloud.datacatalog.v1beta1.DataCatalogClient;
* import com.google.cloud.datacatalog.v1beta1.SearchCatalogRequest;
* import com.google.cloud.datacatalog.v1beta1.SearchCatalogRequest.Scope;
* import com.google.cloud.datacatalog.v1beta1.SearchCatalogResult;
* import java.util.Arrays;
* import java.util.List;
*/

public static void sampleSearchCatalog() {
// TODO(developer): Replace these variables before running the sample.
String includeProjectId = "[Google Cloud Project ID]";
boolean includeGcpPublicDatasets = false;
String query = "[String in search query syntax]";
sampleSearchCatalog(includeProjectId, includeGcpPublicDatasets, query);
}

/**
* Search Catalog
*
* @param includeProjectId Your Google Cloud project ID.
* @param includeGcpPublicDatasets If true, include Google Cloud Platform (GCP) public datasets in
* the search results.
* @param query Your query string. See:
* https://cloud.google.com/data-catalog/docs/how-to/search-reference Example: system=bigquery
* type=dataset
*/
public static void sampleSearchCatalog(
String includeProjectId, boolean includeGcpPublicDatasets, String query) {
try (DataCatalogClient dataCatalogClient = DataCatalogClient.create()) {
List<String> includeProjectIds = Arrays.asList(includeProjectId);
SearchCatalogRequest.Scope scope =
SearchCatalogRequest.Scope.newBuilder()
.addAllIncludeProjectIds(includeProjectIds)
.setIncludeGcpPublicDatasets(includeGcpPublicDatasets)
.build();
SearchCatalogRequest request =
SearchCatalogRequest.newBuilder().setScope(scope).setQuery(query).build();
for (SearchCatalogResult responseItem :
dataCatalogClient.searchCatalog(request).iterateAll()) {
System.out.printf("Result type: %s\n", responseItem.getSearchResultType());
System.out.printf("Result subtype: %s\n", responseItem.getSearchResultSubtype());
System.out.printf("Relative resource name: %s\n", responseItem.getRelativeResourceName());
System.out.printf("Linked resource: %s\n\n", responseItem.getLinkedResource());
}
} catch (Exception exception) {
System.err.println("Failed to create the client due to: " + exception);
}
}
// [END datacatalog_search]

public static void main(String[] args) throws Exception {
Options options = new Options();
options.addOption(
Option.builder("").required(false).hasArg(true).longOpt("include_project_id").build());
options.addOption(
Option.builder("")
.required(false)
.hasArg(true)
.longOpt("include_gcp_public_datasets")
.build());
options.addOption(Option.builder("").required(false).hasArg(true).longOpt("query").build());

CommandLine cl = (new DefaultParser()).parse(options, args);
String includeProjectId = cl.getOptionValue("include_project_id", "[Google Cloud Project ID]");
boolean includeGcpPublicDatasets =
cl.getOptionValue("include_gcp_public_datasets") != null
? Boolean.parseBoolean(cl.getOptionValue("include_gcp_public_datasets"))
: false;
String query = cl.getOptionValue("query", "[String in search query syntax]");

sampleSearchCatalog(includeProjectId, includeGcpPublicDatasets, query);
}
}

0 comments on commit a5070a7

Please sign in to comment.