Skip to content

Commit a5070a7

Browse files
samples: add generated samples (#83)
0 parents  commit a5070a7

File tree

4 files changed

+375
-0
lines changed

4 files changed

+375
-0
lines changed
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
/*
2+
* Copyright 2020 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+
* https://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+
// DO NOT EDIT! This is a generated sample ("Request", "datacatalog_get_entry")
17+
// sample-metadata:
18+
// title:
19+
// description: Get Entry
20+
// usage: gradle run -PmainClass=com.google.cloud.examples.datacatalog.v1beta1.DatacatalogGetEntry
21+
// [--args='[--project_id "[Google Cloud Project ID]"] [--location_id "[Google Cloud Location ID]"]
22+
// [--entry_group_id "[Entry Group ID]"] [--entry_id "[Entry ID]"]']
23+
24+
package com.google.cloud.examples.datacatalog.v1beta1;
25+
26+
import com.google.cloud.datacatalog.v1beta1.DataCatalogClient;
27+
import com.google.cloud.datacatalog.v1beta1.Entry;
28+
import com.google.cloud.datacatalog.v1beta1.GetEntryRequest;
29+
import org.apache.commons.cli.CommandLine;
30+
import org.apache.commons.cli.DefaultParser;
31+
import org.apache.commons.cli.Option;
32+
import org.apache.commons.cli.Options;
33+
34+
public class DatacatalogGetEntry {
35+
// [START datacatalog_get_entry]
36+
/*
37+
* Please include the following imports to run this sample.
38+
*
39+
* import com.google.cloud.datacatalog.v1beta1.DataCatalogClient;
40+
* import com.google.cloud.datacatalog.v1beta1.Entry;
41+
* import com.google.cloud.datacatalog.v1beta1.GetEntryRequest;
42+
*/
43+
44+
public static void sampleGetEntry() {
45+
// TODO(developer): Replace these variables before running the sample.
46+
String projectId = "[Google Cloud Project ID]";
47+
String locationId = "[Google Cloud Location ID]";
48+
String entryGroupId = "[Entry Group ID]";
49+
String entryId = "[Entry ID]";
50+
sampleGetEntry(projectId, locationId, entryGroupId, entryId);
51+
}
52+
53+
/**
54+
* Get Entry
55+
*
56+
* @param projectId Your Google Cloud project ID
57+
* @param locationId Google Cloud region, e.g. us-central1
58+
* @param entryGroupId ID of the Entry Group, e.g. {@literal @}bigquery, {@literal @}pubsub,
59+
* my_entry_group
60+
* @param entryId ID of the Entry
61+
*/
62+
public static void sampleGetEntry(
63+
String projectId, String locationId, String entryGroupId, String entryId) {
64+
try (DataCatalogClient dataCatalogClient = DataCatalogClient.create()) {
65+
String formattedName =
66+
DataCatalogClient.formatEntryName(projectId, locationId, entryGroupId, entryId);
67+
GetEntryRequest request = GetEntryRequest.newBuilder().setName(formattedName).build();
68+
Entry response = dataCatalogClient.getEntry(request);
69+
Entry entry = response;
70+
System.out.printf("Entry name: %s\n", entry.getName());
71+
System.out.printf("Entry type: %s\n", entry.getType());
72+
System.out.printf("Linked resource: %s\n", entry.getLinkedResource());
73+
} catch (Exception exception) {
74+
System.err.println("Failed to create the client due to: " + exception);
75+
}
76+
}
77+
// [END datacatalog_get_entry]
78+
79+
public static void main(String[] args) throws Exception {
80+
Options options = new Options();
81+
options.addOption(
82+
Option.builder("").required(false).hasArg(true).longOpt("project_id").build());
83+
options.addOption(
84+
Option.builder("").required(false).hasArg(true).longOpt("location_id").build());
85+
options.addOption(
86+
Option.builder("").required(false).hasArg(true).longOpt("entry_group_id").build());
87+
options.addOption(Option.builder("").required(false).hasArg(true).longOpt("entry_id").build());
88+
89+
CommandLine cl = (new DefaultParser()).parse(options, args);
90+
String projectId = cl.getOptionValue("project_id", "[Google Cloud Project ID]");
91+
String locationId = cl.getOptionValue("location_id", "[Google Cloud Location ID]");
92+
String entryGroupId = cl.getOptionValue("entry_group_id", "[Entry Group ID]");
93+
String entryId = cl.getOptionValue("entry_id", "[Entry ID]");
94+
95+
sampleGetEntry(projectId, locationId, entryGroupId, entryId);
96+
}
97+
}
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
/*
2+
* Copyright 2020 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+
* https://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+
// DO NOT EDIT! This is a generated sample ("Request", "datacatalog_lookup_entry")
17+
// sample-metadata:
18+
// title:
19+
// description: Lookup Entry
20+
// usage: gradle run
21+
// -PmainClass=com.google.cloud.examples.datacatalog.v1beta1.DatacatalogLookupEntry
22+
// [--args='[--resource_name "[Full Resource Name]"]']
23+
24+
package com.google.cloud.examples.datacatalog.v1beta1;
25+
26+
import com.google.cloud.datacatalog.v1beta1.DataCatalogClient;
27+
import com.google.cloud.datacatalog.v1beta1.Entry;
28+
import com.google.cloud.datacatalog.v1beta1.LookupEntryRequest;
29+
import org.apache.commons.cli.CommandLine;
30+
import org.apache.commons.cli.DefaultParser;
31+
import org.apache.commons.cli.Option;
32+
import org.apache.commons.cli.Options;
33+
34+
public class DatacatalogLookupEntry {
35+
// [START datacatalog_lookup_entry]
36+
/*
37+
* Please include the following imports to run this sample.
38+
*
39+
* import com.google.cloud.datacatalog.v1beta1.DataCatalogClient;
40+
* import com.google.cloud.datacatalog.v1beta1.Entry;
41+
* import com.google.cloud.datacatalog.v1beta1.LookupEntryRequest;
42+
*/
43+
44+
public static void sampleLookupEntry() {
45+
// TODO(developer): Replace these variables before running the sample.
46+
String resourceName = "[Full Resource Name]";
47+
sampleLookupEntry(resourceName);
48+
}
49+
50+
/**
51+
* Lookup Entry
52+
*
53+
* @param resourceName The full name of the Google Cloud Platform resource the Data Catalog entry
54+
* represents. See: https://cloud.google.com/apis/design/resource_names#full_resource_name
55+
* Examples:
56+
* //bigquery.googleapis.com/projects/bigquery-public-data/datasets/new_york_taxi_trips/tables/taxi_zone_geom
57+
* //pubsub.googleapis.com/projects/pubsub-public-data/topics/taxirides-realtime
58+
*/
59+
public static void sampleLookupEntry(String resourceName) {
60+
try (DataCatalogClient dataCatalogClient = DataCatalogClient.create()) {
61+
LookupEntryRequest request =
62+
LookupEntryRequest.newBuilder().setLinkedResource(resourceName).build();
63+
Entry response = dataCatalogClient.lookupEntry(request);
64+
Entry entry = response;
65+
System.out.printf("Entry name: %s\n", entry.getName());
66+
System.out.printf("Entry type: %s\n", entry.getType());
67+
System.out.printf("Linked resource: %s\n", entry.getLinkedResource());
68+
} catch (Exception exception) {
69+
System.err.println("Failed to create the client due to: " + exception);
70+
}
71+
}
72+
// [END datacatalog_lookup_entry]
73+
74+
public static void main(String[] args) throws Exception {
75+
Options options = new Options();
76+
options.addOption(
77+
Option.builder("").required(false).hasArg(true).longOpt("resource_name").build());
78+
79+
CommandLine cl = (new DefaultParser()).parse(options, args);
80+
String resourceName = cl.getOptionValue("resource_name", "[Full Resource Name]");
81+
82+
sampleLookupEntry(resourceName);
83+
}
84+
}
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
/*
2+
* Copyright 2020 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+
* https://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+
// DO NOT EDIT! This is a generated sample ("Request", "datacatalog_lookup_entry_sql_resource")
17+
// sample-metadata:
18+
// title:
19+
// description: Lookup Entry using SQL resource
20+
// usage: gradle run
21+
// -PmainClass=com.google.cloud.examples.datacatalog.v1beta1.DatacatalogLookupEntrySqlResource
22+
// [--args='[--sql_name "[SQL Resource Name]"]']
23+
24+
package com.google.cloud.examples.datacatalog.v1beta1;
25+
26+
import com.google.cloud.datacatalog.v1beta1.DataCatalogClient;
27+
import com.google.cloud.datacatalog.v1beta1.Entry;
28+
import com.google.cloud.datacatalog.v1beta1.LookupEntryRequest;
29+
import org.apache.commons.cli.CommandLine;
30+
import org.apache.commons.cli.DefaultParser;
31+
import org.apache.commons.cli.Option;
32+
import org.apache.commons.cli.Options;
33+
34+
public class DatacatalogLookupEntrySqlResource {
35+
// [START datacatalog_lookup_entry_sql_resource]
36+
/*
37+
* Please include the following imports to run this sample.
38+
*
39+
* import com.google.cloud.datacatalog.v1beta1.DataCatalogClient;
40+
* import com.google.cloud.datacatalog.v1beta1.Entry;
41+
* import com.google.cloud.datacatalog.v1beta1.LookupEntryRequest;
42+
*/
43+
44+
public static void sampleLookupEntry() {
45+
// TODO(developer): Replace these variables before running the sample.
46+
String sqlName = "[SQL Resource Name]";
47+
sampleLookupEntry(sqlName);
48+
}
49+
50+
/**
51+
* Lookup Entry using SQL resource
52+
*
53+
* @param sqlName The SQL name of the Google Cloud Platform resource the Data Catalog entry
54+
* represents. Examples:
55+
* bigquery.table.`bigquery-public-data`.new_york_taxi_trips.taxi_zone_geom
56+
* pubsub.topic.`pubsub-public-data`.`taxirides-realtime`
57+
*/
58+
public static void sampleLookupEntry(String sqlName) {
59+
try (DataCatalogClient dataCatalogClient = DataCatalogClient.create()) {
60+
LookupEntryRequest request = LookupEntryRequest.newBuilder().setSqlResource(sqlName).build();
61+
Entry response = dataCatalogClient.lookupEntry(request);
62+
Entry entry = response;
63+
System.out.printf("Entry name: %s\n", entry.getName());
64+
System.out.printf("Entry type: %s\n", entry.getType());
65+
System.out.printf("Linked resource: %s\n", entry.getLinkedResource());
66+
} catch (Exception exception) {
67+
System.err.println("Failed to create the client due to: " + exception);
68+
}
69+
}
70+
// [END datacatalog_lookup_entry_sql_resource]
71+
72+
public static void main(String[] args) throws Exception {
73+
Options options = new Options();
74+
options.addOption(Option.builder("").required(false).hasArg(true).longOpt("sql_name").build());
75+
76+
CommandLine cl = (new DefaultParser()).parse(options, args);
77+
String sqlName = cl.getOptionValue("sql_name", "[SQL Resource Name]");
78+
79+
sampleLookupEntry(sqlName);
80+
}
81+
}
Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
/*
2+
* Copyright 2020 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+
* https://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+
// DO NOT EDIT! This is a generated sample ("RequestPaged", "datacatalog_search")
17+
// sample-metadata:
18+
// title:
19+
// description: Search Catalog
20+
// usage: gradle run -PmainClass=com.google.cloud.examples.datacatalog.v1beta1.DatacatalogSearch
21+
// [--args='[--include_project_id "[Google Cloud Project ID]"] [--include_gcp_public_datasets false]
22+
// [--query "[String in search query syntax]"]']
23+
24+
package com.google.cloud.examples.datacatalog.v1beta1;
25+
26+
import com.google.cloud.datacatalog.v1beta1.DataCatalogClient;
27+
import com.google.cloud.datacatalog.v1beta1.SearchCatalogRequest;
28+
import com.google.cloud.datacatalog.v1beta1.SearchCatalogResult;
29+
import java.util.Arrays;
30+
import java.util.List;
31+
import org.apache.commons.cli.CommandLine;
32+
import org.apache.commons.cli.DefaultParser;
33+
import org.apache.commons.cli.Option;
34+
import org.apache.commons.cli.Options;
35+
36+
public class DatacatalogSearch {
37+
// [START datacatalog_search]
38+
/*
39+
* Please include the following imports to run this sample.
40+
*
41+
* import com.google.cloud.datacatalog.v1beta1.DataCatalogClient;
42+
* import com.google.cloud.datacatalog.v1beta1.SearchCatalogRequest;
43+
* import com.google.cloud.datacatalog.v1beta1.SearchCatalogRequest.Scope;
44+
* import com.google.cloud.datacatalog.v1beta1.SearchCatalogResult;
45+
* import java.util.Arrays;
46+
* import java.util.List;
47+
*/
48+
49+
public static void sampleSearchCatalog() {
50+
// TODO(developer): Replace these variables before running the sample.
51+
String includeProjectId = "[Google Cloud Project ID]";
52+
boolean includeGcpPublicDatasets = false;
53+
String query = "[String in search query syntax]";
54+
sampleSearchCatalog(includeProjectId, includeGcpPublicDatasets, query);
55+
}
56+
57+
/**
58+
* Search Catalog
59+
*
60+
* @param includeProjectId Your Google Cloud project ID.
61+
* @param includeGcpPublicDatasets If true, include Google Cloud Platform (GCP) public datasets in
62+
* the search results.
63+
* @param query Your query string. See:
64+
* https://cloud.google.com/data-catalog/docs/how-to/search-reference Example: system=bigquery
65+
* type=dataset
66+
*/
67+
public static void sampleSearchCatalog(
68+
String includeProjectId, boolean includeGcpPublicDatasets, String query) {
69+
try (DataCatalogClient dataCatalogClient = DataCatalogClient.create()) {
70+
List<String> includeProjectIds = Arrays.asList(includeProjectId);
71+
SearchCatalogRequest.Scope scope =
72+
SearchCatalogRequest.Scope.newBuilder()
73+
.addAllIncludeProjectIds(includeProjectIds)
74+
.setIncludeGcpPublicDatasets(includeGcpPublicDatasets)
75+
.build();
76+
SearchCatalogRequest request =
77+
SearchCatalogRequest.newBuilder().setScope(scope).setQuery(query).build();
78+
for (SearchCatalogResult responseItem :
79+
dataCatalogClient.searchCatalog(request).iterateAll()) {
80+
System.out.printf("Result type: %s\n", responseItem.getSearchResultType());
81+
System.out.printf("Result subtype: %s\n", responseItem.getSearchResultSubtype());
82+
System.out.printf("Relative resource name: %s\n", responseItem.getRelativeResourceName());
83+
System.out.printf("Linked resource: %s\n\n", responseItem.getLinkedResource());
84+
}
85+
} catch (Exception exception) {
86+
System.err.println("Failed to create the client due to: " + exception);
87+
}
88+
}
89+
// [END datacatalog_search]
90+
91+
public static void main(String[] args) throws Exception {
92+
Options options = new Options();
93+
options.addOption(
94+
Option.builder("").required(false).hasArg(true).longOpt("include_project_id").build());
95+
options.addOption(
96+
Option.builder("")
97+
.required(false)
98+
.hasArg(true)
99+
.longOpt("include_gcp_public_datasets")
100+
.build());
101+
options.addOption(Option.builder("").required(false).hasArg(true).longOpt("query").build());
102+
103+
CommandLine cl = (new DefaultParser()).parse(options, args);
104+
String includeProjectId = cl.getOptionValue("include_project_id", "[Google Cloud Project ID]");
105+
boolean includeGcpPublicDatasets =
106+
cl.getOptionValue("include_gcp_public_datasets") != null
107+
? Boolean.parseBoolean(cl.getOptionValue("include_gcp_public_datasets"))
108+
: false;
109+
String query = cl.getOptionValue("query", "[String in search query syntax]");
110+
111+
sampleSearchCatalog(includeProjectId, includeGcpPublicDatasets, query);
112+
}
113+
}

0 commit comments

Comments
 (0)