diff --git a/README.md b/README.md index b2f1b3fe..b00b523b 100644 --- a/README.md +++ b/README.md @@ -83,6 +83,7 @@ Samples are in the [`samples/`](https://github.com/googleapis/java-contact-cente | Create Phrase Matcher All Of | [source code](https://github.com/googleapis/java-contact-center-insights/blob/main/samples/snippets/src/main/java/com/example/contactcenterinsights/CreatePhraseMatcherAllOf.java) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/java-contact-center-insights&page=editor&open_in_editor=samples/snippets/src/main/java/com/example/contactcenterinsights/CreatePhraseMatcherAllOf.java) | | Create Phrase Matcher Any Of | [source code](https://github.com/googleapis/java-contact-center-insights/blob/main/samples/snippets/src/main/java/com/example/contactcenterinsights/CreatePhraseMatcherAnyOf.java) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/java-contact-center-insights&page=editor&open_in_editor=samples/snippets/src/main/java/com/example/contactcenterinsights/CreatePhraseMatcherAnyOf.java) | | Enable Pub Sub Notifications | [source code](https://github.com/googleapis/java-contact-center-insights/blob/main/samples/snippets/src/main/java/com/example/contactcenterinsights/EnablePubSubNotifications.java) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/java-contact-center-insights&page=editor&open_in_editor=samples/snippets/src/main/java/com/example/contactcenterinsights/EnablePubSubNotifications.java) | +| Export To Bigquery | [source code](https://github.com/googleapis/java-contact-center-insights/blob/main/samples/snippets/src/main/java/com/example/contactcenterinsights/ExportToBigquery.java) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/java-contact-center-insights&page=editor&open_in_editor=samples/snippets/src/main/java/com/example/contactcenterinsights/ExportToBigquery.java) | | Get Operation | [source code](https://github.com/googleapis/java-contact-center-insights/blob/main/samples/snippets/src/main/java/com/example/contactcenterinsights/GetOperation.java) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/java-contact-center-insights&page=editor&open_in_editor=samples/snippets/src/main/java/com/example/contactcenterinsights/GetOperation.java) | | Set Project Ttl | [source code](https://github.com/googleapis/java-contact-center-insights/blob/main/samples/snippets/src/main/java/com/example/contactcenterinsights/SetProjectTtl.java) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/java-contact-center-insights&page=editor&open_in_editor=samples/snippets/src/main/java/com/example/contactcenterinsights/SetProjectTtl.java) | diff --git a/samples/install-without-bom/pom.xml b/samples/install-without-bom/pom.xml index 0db68162..b32777b6 100644 --- a/samples/install-without-bom/pom.xml +++ b/samples/install-without-bom/pom.xml @@ -45,6 +45,12 @@ 1.1.3 test + + com.google.cloud + google-cloud-bigquery + 2.1.7 + test + com.google.cloud google-cloud-pubsub diff --git a/samples/snapshot/pom.xml b/samples/snapshot/pom.xml index 3d07654a..d3cb6beb 100644 --- a/samples/snapshot/pom.xml +++ b/samples/snapshot/pom.xml @@ -44,6 +44,12 @@ 1.1.3 test + + com.google.cloud + google-cloud-bigquery + 2.1.7 + test + com.google.cloud google-cloud-pubsub diff --git a/samples/snippets/pom.xml b/samples/snippets/pom.xml index bbb083d4..13fddb8a 100644 --- a/samples/snippets/pom.xml +++ b/samples/snippets/pom.xml @@ -43,6 +43,12 @@ 1.1.3 test + + com.google.cloud + google-cloud-bigquery + 2.1.7 + test + com.google.cloud google-cloud-pubsub diff --git a/samples/snippets/src/main/java/com/example/contactcenterinsights/ExportToBigquery.java b/samples/snippets/src/main/java/com/example/contactcenterinsights/ExportToBigquery.java new file mode 100644 index 00000000..c5e8de14 --- /dev/null +++ b/samples/snippets/src/main/java/com/example/contactcenterinsights/ExportToBigquery.java @@ -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] diff --git a/samples/snippets/src/test/java/com/example/contactcenterinsights/ExportToBigqueryIT.java b/samples/snippets/src/test/java/com/example/contactcenterinsights/ExportToBigqueryIT.java new file mode 100644 index 00000000..a7b68acf --- /dev/null +++ b/samples/snippets/src/test/java/com/example/contactcenterinsights/ExportToBigqueryIT.java @@ -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"); + } +}