Skip to content

Commit

Permalink
samples: export data to BigQuery (#90)
Browse files Browse the repository at this point in the history
  • Loading branch information
TrucHLe authored and Shabirmean committed Nov 11, 2022
1 parent fa05458 commit e41d47a
Show file tree
Hide file tree
Showing 3 changed files with 207 additions and 0 deletions.
6 changes: 6 additions & 0 deletions contact-center-insights/snippets/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,12 @@
<version>1.1.3</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.google.cloud</groupId>
<artifactId>google-cloud-bigquery</artifactId>
<version>2.1.7</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.google.cloud</groupId>
<artifactId>google-cloud-pubsub</artifactId>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
/*
* Copyright 2021 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.contactcenterinsights;

// [START contactcenterinsights_export_to_bigquery]

import com.google.api.gax.longrunning.OperationTimedPollAlgorithm;
import com.google.api.gax.retrying.RetrySettings;
import com.google.cloud.contactcenterinsights.v1.ContactCenterInsightsClient;
import com.google.cloud.contactcenterinsights.v1.ContactCenterInsightsSettings;
import com.google.cloud.contactcenterinsights.v1.ExportInsightsDataRequest;
import com.google.cloud.contactcenterinsights.v1.ExportInsightsDataResponse;
import com.google.cloud.contactcenterinsights.v1.LocationName;
import java.io.IOException;
import org.threeten.bp.Duration;

public class ExportToBigquery {

public static void main(String[] args) throws Exception, IOException {
// TODO(developer): Replace these variables before running the sample.
String projectId = "my_project_id";
String bigqueryProjectId = "my_bigquery_project_id";
String bigqueryDataset = "my_bigquery_dataset";
String bigqueryTable = "my_bigquery_table";

exportToBigquery(projectId, bigqueryProjectId, bigqueryDataset, bigqueryTable);
}

public static void exportToBigquery(
String projectId, String bigqueryProjectId, String bigqueryDataset, String bigqueryTable)
throws Exception, IOException {
// Set the operation total polling timeout to 24 hours instead of the 5-minute default.
// Other values are copied from the default values of {@link ContactCenterInsightsStubSettings}.
ContactCenterInsightsSettings.Builder clientSettings =
ContactCenterInsightsSettings.newBuilder();
clientSettings
.exportInsightsDataOperationSettings()
.setPollingAlgorithm(
OperationTimedPollAlgorithm.create(
RetrySettings.newBuilder()
.setInitialRetryDelay(Duration.ofMillis(5000L))
.setRetryDelayMultiplier(1.5)
.setMaxRetryDelay(Duration.ofMillis(45000L))
.setInitialRpcTimeout(Duration.ZERO)
.setRpcTimeoutMultiplier(1.0)
.setMaxRpcTimeout(Duration.ZERO)
.setTotalTimeout(Duration.ofHours(24L))
.build()));

// 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 (ContactCenterInsightsClient client =
ContactCenterInsightsClient.create(clientSettings.build())) {
// Construct an export request.
LocationName parent = LocationName.of(projectId, "us-central1");
ExportInsightsDataRequest request =
ExportInsightsDataRequest.newBuilder()
.setParent(parent.toString())
.setBigQueryDestination(
ExportInsightsDataRequest.BigQueryDestination.newBuilder()
.setProjectId(bigqueryProjectId)
.setDataset(bigqueryDataset)
.setTable(bigqueryTable)
.build())
.setFilter("agent_id=\"007\"")
.build();

// Call the Insights client to export data to BigQuery.
ExportInsightsDataResponse response = client.exportInsightsDataAsync(request).get();
System.out.printf("Exported data to BigQuery");
}
}
}

// [END contactcenterinsights_export_to_bigquery]
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
/*
* Copyright 2021 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.contactcenterinsights;

import static com.google.common.truth.Truth.assertThat;
import static junit.framework.TestCase.assertNotNull;

import com.google.cloud.bigquery.BigQuery;
import com.google.cloud.bigquery.BigQueryException;
import com.google.cloud.bigquery.BigQueryOptions;
import com.google.cloud.bigquery.Dataset;
import com.google.cloud.bigquery.DatasetId;
import com.google.cloud.bigquery.DatasetInfo;
import com.google.cloud.bigquery.Schema;
import com.google.cloud.bigquery.StandardTableDefinition;
import com.google.cloud.bigquery.Table;
import com.google.cloud.bigquery.TableDefinition;
import com.google.cloud.bigquery.TableId;
import com.google.cloud.bigquery.TableInfo;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.PrintStream;
import java.util.ArrayList;
import java.util.UUID;
import org.junit.After;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;

@RunWith(JUnit4.class)
public class ExportToBigqueryIT {

private static final String PROJECT_ID = System.getenv("GOOGLE_CLOUD_PROJECT");
private static final String BIGQUERY_PROJECT_ID = System.getenv("GOOGLE_CLOUD_PROJECT");
private static final String GCLOUD_TESTS_PREFIX = "java_samples_tests";
private ByteArrayOutputStream bout;
private PrintStream out;
private String bigqueryDatasetId;
private String bigqueryTableId;

private static void requireEnvVar(String varName) {
assertNotNull(String.format(varName), String.format(varName));
}

@BeforeClass
public static void checkRequirements() {
requireEnvVar("GOOGLE_APPLICATION_CREDENTIALS");
requireEnvVar("GOOGLE_CLOUD_PROJECT");
}

@Before
public void setUp() throws BigQueryException {
bout = new ByteArrayOutputStream();
out = new PrintStream(bout);
System.setOut(out);

// Generate BigQuery table and dataset IDs.
bigqueryDatasetId =
String.format("%s_%s", GCLOUD_TESTS_PREFIX, UUID.randomUUID().toString().replace("-", "_"));
bigqueryTableId =
String.format("%s_%s", GCLOUD_TESTS_PREFIX, UUID.randomUUID().toString().replace("-", "_"));

// Create a BigQuery dataset.
BigQuery bigquery = BigQueryOptions.getDefaultInstance().getService();
DatasetInfo datasetInfo =
DatasetInfo.newBuilder(DatasetId.of(BIGQUERY_PROJECT_ID, bigqueryDatasetId)).build();
Dataset dataset = bigquery.create(datasetInfo);

// Create a BigQuery table under the created dataset.
Schema schema = Schema.of(new ArrayList<>());
TableDefinition tableDefinition = StandardTableDefinition.of(schema);
TableInfo tableInfo =
TableInfo.newBuilder(TableId.of(bigqueryDatasetId, bigqueryTableId), tableDefinition)
.build();
Table table = bigquery.create(tableInfo);
}

@After
public void tearDown() throws BigQueryException {
// Delete the BigQuery dataset and table.
BigQuery bigquery = BigQueryOptions.getDefaultInstance().getService();
boolean success =
bigquery.delete(
DatasetId.of(PROJECT_ID, bigqueryDatasetId),
BigQuery.DatasetDeleteOption.deleteContents());
System.setOut(null);
}

@Test
public void testExportToBigquery() throws Exception, IOException {
ExportToBigquery.exportToBigquery(
PROJECT_ID, BIGQUERY_PROJECT_ID, bigqueryDatasetId, bigqueryTableId);
assertThat(bout.toString()).contains("Exported data to BigQuery");
}
}

0 comments on commit e41d47a

Please sign in to comment.