Skip to content

Commit e41d47a

Browse files
TrucHLeShabirmean
authored andcommitted
samples: export data to BigQuery (#90)
1 parent fa05458 commit e41d47a

File tree

3 files changed

+207
-0
lines changed

3 files changed

+207
-0
lines changed

contact-center-insights/snippets/pom.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,12 @@
4343
<version>1.1.3</version>
4444
<scope>test</scope>
4545
</dependency>
46+
<dependency>
47+
<groupId>com.google.cloud</groupId>
48+
<artifactId>google-cloud-bigquery</artifactId>
49+
<version>2.1.7</version>
50+
<scope>test</scope>
51+
</dependency>
4652
<dependency>
4753
<groupId>com.google.cloud</groupId>
4854
<artifactId>google-cloud-pubsub</artifactId>
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
/*
2+
* Copyright 2021 Google Inc.
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 com.example.contactcenterinsights;
18+
19+
// [START contactcenterinsights_export_to_bigquery]
20+
21+
import com.google.api.gax.longrunning.OperationTimedPollAlgorithm;
22+
import com.google.api.gax.retrying.RetrySettings;
23+
import com.google.cloud.contactcenterinsights.v1.ContactCenterInsightsClient;
24+
import com.google.cloud.contactcenterinsights.v1.ContactCenterInsightsSettings;
25+
import com.google.cloud.contactcenterinsights.v1.ExportInsightsDataRequest;
26+
import com.google.cloud.contactcenterinsights.v1.ExportInsightsDataResponse;
27+
import com.google.cloud.contactcenterinsights.v1.LocationName;
28+
import java.io.IOException;
29+
import org.threeten.bp.Duration;
30+
31+
public class ExportToBigquery {
32+
33+
public static void main(String[] args) throws Exception, IOException {
34+
// TODO(developer): Replace these variables before running the sample.
35+
String projectId = "my_project_id";
36+
String bigqueryProjectId = "my_bigquery_project_id";
37+
String bigqueryDataset = "my_bigquery_dataset";
38+
String bigqueryTable = "my_bigquery_table";
39+
40+
exportToBigquery(projectId, bigqueryProjectId, bigqueryDataset, bigqueryTable);
41+
}
42+
43+
public static void exportToBigquery(
44+
String projectId, String bigqueryProjectId, String bigqueryDataset, String bigqueryTable)
45+
throws Exception, IOException {
46+
// Set the operation total polling timeout to 24 hours instead of the 5-minute default.
47+
// Other values are copied from the default values of {@link ContactCenterInsightsStubSettings}.
48+
ContactCenterInsightsSettings.Builder clientSettings =
49+
ContactCenterInsightsSettings.newBuilder();
50+
clientSettings
51+
.exportInsightsDataOperationSettings()
52+
.setPollingAlgorithm(
53+
OperationTimedPollAlgorithm.create(
54+
RetrySettings.newBuilder()
55+
.setInitialRetryDelay(Duration.ofMillis(5000L))
56+
.setRetryDelayMultiplier(1.5)
57+
.setMaxRetryDelay(Duration.ofMillis(45000L))
58+
.setInitialRpcTimeout(Duration.ZERO)
59+
.setRpcTimeoutMultiplier(1.0)
60+
.setMaxRpcTimeout(Duration.ZERO)
61+
.setTotalTimeout(Duration.ofHours(24L))
62+
.build()));
63+
64+
// Initialize client that will be used to send requests. This client only needs to be created
65+
// once, and can be reused for multiple requests. After completing all of your requests, call
66+
// the "close" method on the client to safely clean up any remaining background resources.
67+
try (ContactCenterInsightsClient client =
68+
ContactCenterInsightsClient.create(clientSettings.build())) {
69+
// Construct an export request.
70+
LocationName parent = LocationName.of(projectId, "us-central1");
71+
ExportInsightsDataRequest request =
72+
ExportInsightsDataRequest.newBuilder()
73+
.setParent(parent.toString())
74+
.setBigQueryDestination(
75+
ExportInsightsDataRequest.BigQueryDestination.newBuilder()
76+
.setProjectId(bigqueryProjectId)
77+
.setDataset(bigqueryDataset)
78+
.setTable(bigqueryTable)
79+
.build())
80+
.setFilter("agent_id=\"007\"")
81+
.build();
82+
83+
// Call the Insights client to export data to BigQuery.
84+
ExportInsightsDataResponse response = client.exportInsightsDataAsync(request).get();
85+
System.out.printf("Exported data to BigQuery");
86+
}
87+
}
88+
}
89+
90+
// [END contactcenterinsights_export_to_bigquery]
Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
/*
2+
* Copyright 2021 Google Inc.
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 com.example.contactcenterinsights;
18+
19+
import static com.google.common.truth.Truth.assertThat;
20+
import static junit.framework.TestCase.assertNotNull;
21+
22+
import com.google.cloud.bigquery.BigQuery;
23+
import com.google.cloud.bigquery.BigQueryException;
24+
import com.google.cloud.bigquery.BigQueryOptions;
25+
import com.google.cloud.bigquery.Dataset;
26+
import com.google.cloud.bigquery.DatasetId;
27+
import com.google.cloud.bigquery.DatasetInfo;
28+
import com.google.cloud.bigquery.Schema;
29+
import com.google.cloud.bigquery.StandardTableDefinition;
30+
import com.google.cloud.bigquery.Table;
31+
import com.google.cloud.bigquery.TableDefinition;
32+
import com.google.cloud.bigquery.TableId;
33+
import com.google.cloud.bigquery.TableInfo;
34+
import java.io.ByteArrayOutputStream;
35+
import java.io.IOException;
36+
import java.io.PrintStream;
37+
import java.util.ArrayList;
38+
import java.util.UUID;
39+
import org.junit.After;
40+
import org.junit.Before;
41+
import org.junit.BeforeClass;
42+
import org.junit.Test;
43+
import org.junit.runner.RunWith;
44+
import org.junit.runners.JUnit4;
45+
46+
@RunWith(JUnit4.class)
47+
public class ExportToBigqueryIT {
48+
49+
private static final String PROJECT_ID = System.getenv("GOOGLE_CLOUD_PROJECT");
50+
private static final String BIGQUERY_PROJECT_ID = System.getenv("GOOGLE_CLOUD_PROJECT");
51+
private static final String GCLOUD_TESTS_PREFIX = "java_samples_tests";
52+
private ByteArrayOutputStream bout;
53+
private PrintStream out;
54+
private String bigqueryDatasetId;
55+
private String bigqueryTableId;
56+
57+
private static void requireEnvVar(String varName) {
58+
assertNotNull(String.format(varName), String.format(varName));
59+
}
60+
61+
@BeforeClass
62+
public static void checkRequirements() {
63+
requireEnvVar("GOOGLE_APPLICATION_CREDENTIALS");
64+
requireEnvVar("GOOGLE_CLOUD_PROJECT");
65+
}
66+
67+
@Before
68+
public void setUp() throws BigQueryException {
69+
bout = new ByteArrayOutputStream();
70+
out = new PrintStream(bout);
71+
System.setOut(out);
72+
73+
// Generate BigQuery table and dataset IDs.
74+
bigqueryDatasetId =
75+
String.format("%s_%s", GCLOUD_TESTS_PREFIX, UUID.randomUUID().toString().replace("-", "_"));
76+
bigqueryTableId =
77+
String.format("%s_%s", GCLOUD_TESTS_PREFIX, UUID.randomUUID().toString().replace("-", "_"));
78+
79+
// Create a BigQuery dataset.
80+
BigQuery bigquery = BigQueryOptions.getDefaultInstance().getService();
81+
DatasetInfo datasetInfo =
82+
DatasetInfo.newBuilder(DatasetId.of(BIGQUERY_PROJECT_ID, bigqueryDatasetId)).build();
83+
Dataset dataset = bigquery.create(datasetInfo);
84+
85+
// Create a BigQuery table under the created dataset.
86+
Schema schema = Schema.of(new ArrayList<>());
87+
TableDefinition tableDefinition = StandardTableDefinition.of(schema);
88+
TableInfo tableInfo =
89+
TableInfo.newBuilder(TableId.of(bigqueryDatasetId, bigqueryTableId), tableDefinition)
90+
.build();
91+
Table table = bigquery.create(tableInfo);
92+
}
93+
94+
@After
95+
public void tearDown() throws BigQueryException {
96+
// Delete the BigQuery dataset and table.
97+
BigQuery bigquery = BigQueryOptions.getDefaultInstance().getService();
98+
boolean success =
99+
bigquery.delete(
100+
DatasetId.of(PROJECT_ID, bigqueryDatasetId),
101+
BigQuery.DatasetDeleteOption.deleteContents());
102+
System.setOut(null);
103+
}
104+
105+
@Test
106+
public void testExportToBigquery() throws Exception, IOException {
107+
ExportToBigquery.exportToBigquery(
108+
PROJECT_ID, BIGQUERY_PROJECT_ID, bigqueryDatasetId, bigqueryTableId);
109+
assertThat(bout.toString()).contains("Exported data to BigQuery");
110+
}
111+
}

0 commit comments

Comments
 (0)