diff --git a/samples/snippets/pom.xml b/samples/snippets/pom.xml index d6cbcca5d1..9238160a41 100644 --- a/samples/snippets/pom.xml +++ b/samples/snippets/pom.xml @@ -47,7 +47,7 @@ com.google.cloud libraries-bom - 26.64.0 + 24.0.0 pom import @@ -65,6 +65,7 @@ com.google.cloud google-cloud-bigquery + 2.42.0 io.opentelemetry diff --git a/samples/snippets/src/main/java/com/example/bigquery/EnableOpenTelemetryTracing.java b/samples/snippets/src/main/java/com/example/bigquery/EnableOpenTelemetryTracing.java deleted file mode 100644 index 57ec7eb71d..0000000000 --- a/samples/snippets/src/main/java/com/example/bigquery/EnableOpenTelemetryTracing.java +++ /dev/null @@ -1,85 +0,0 @@ -/* - * Copyright 2025 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 - * - * 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.bigquery; - -// [START bigquery_enable_otel_tracing] -import com.google.cloud.bigquery.BigQuery; -import com.google.cloud.bigquery.BigQueryOptions; -import com.google.cloud.bigquery.Dataset; -import com.google.cloud.bigquery.DatasetInfo; -import io.opentelemetry.api.OpenTelemetry; -import io.opentelemetry.api.trace.Tracer; -import io.opentelemetry.exporter.logging.LoggingSpanExporter; -import io.opentelemetry.sdk.OpenTelemetrySdk; -import io.opentelemetry.sdk.trace.SdkTracerProvider; -import io.opentelemetry.sdk.trace.export.SimpleSpanProcessor; -import io.opentelemetry.sdk.trace.samplers.Sampler; -import java.util.logging.ConsoleHandler; -import java.util.logging.Logger; - -public class EnableOpenTelemetryTracing { - private static final Logger log = Logger.getLogger(EnableOpenTelemetryTracing.class.getName()); - - public static void main(String[] args) { - // Set logging to System.err. - ConsoleHandler ch = new ConsoleHandler(); - log.addHandler(ch); - - // TODO(developer): Replace values before running the sample. - final String tracerName = "Sample Tracer"; - final String datasetId = "sampleDatasetId"; - - // Create TracerProvider that exports to a logger. - SdkTracerProvider tracerProvider = - SdkTracerProvider.builder() - .addSpanProcessor(SimpleSpanProcessor.builder(LoggingSpanExporter.create()).build()) - .setSampler(Sampler.alwaysOn()) - .build(); - - // Create global OpenTelemetry instance using the TracerProvider. - OpenTelemetry otel = OpenTelemetrySdk.builder().setTracerProvider(tracerProvider).build(); - - // Create Tracer instance from the OpenTelemetry object. Tracers are used to create - // Spans. There can be multiple Tracers in an OpenTelemetry instance. - Tracer tracer = otel.getTracer(tracerName); - - enableOpenTelemetry(tracer, datasetId); - } - - public static void enableOpenTelemetry(Tracer tracer, String datasetId) { - // Create BigQuery client to trace. EnableOpenTelemetryTracing and OpenTelemetryTracer must - // be set to enable tracing. - BigQueryOptions otelOptions = - BigQueryOptions.newBuilder() - .setEnableOpenTelemetryTracing(true) - .setOpenTelemetryTracer(tracer) - .build(); - BigQuery bigquery = otelOptions.getService(); - - try { - // Create dataset. - DatasetInfo info = DatasetInfo.newBuilder(datasetId).build(); - Dataset dataset = bigquery.create(info); - } catch (Exception e) { - System.out.println( - String.format("Failed to create dataset: %s: %s", e.toString(), e.getMessage())); - } finally { - bigquery.delete(datasetId); - } - } -} -// [END bigquery_enable_otel_tracing] diff --git a/samples/snippets/src/main/java/com/example/bigquery/EnableOpenTelemetryTracingWithParentSpan.java b/samples/snippets/src/main/java/com/example/bigquery/EnableOpenTelemetryTracingWithParentSpan.java deleted file mode 100644 index af69df10ba..0000000000 --- a/samples/snippets/src/main/java/com/example/bigquery/EnableOpenTelemetryTracingWithParentSpan.java +++ /dev/null @@ -1,105 +0,0 @@ -/* - * Copyright 2025 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 - * - * 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.bigquery; - -// [START bigquery_enable_otel_tracing_with_parent_span] -import com.google.cloud.bigquery.BigQuery; -import com.google.cloud.bigquery.BigQueryOptions; -import com.google.cloud.bigquery.Dataset; -import com.google.cloud.bigquery.DatasetInfo; -import io.opentelemetry.api.OpenTelemetry; -import io.opentelemetry.api.trace.Span; -import io.opentelemetry.api.trace.Tracer; -import io.opentelemetry.context.Scope; -import io.opentelemetry.exporter.logging.LoggingSpanExporter; -import io.opentelemetry.sdk.OpenTelemetrySdk; -import io.opentelemetry.sdk.trace.SdkTracerProvider; -import io.opentelemetry.sdk.trace.export.SimpleSpanProcessor; -import io.opentelemetry.sdk.trace.samplers.Sampler; -import java.time.LocalDate; -import java.util.logging.ConsoleHandler; -import java.util.logging.Logger; - -public class EnableOpenTelemetryTracingWithParentSpan { - private static final Logger log = - Logger.getLogger(EnableOpenTelemetryTracingWithParentSpan.class.getName()); - - public static void main(String[] args) { - // Set logging to System.err. - ConsoleHandler ch = new ConsoleHandler(); - log.addHandler(ch); - - // TODO(developer): Replace values before running the sample. - final String tracerName = "Sample Tracer"; - final String parentSpanName = "Sample Parent Span"; - final String datasetId = "sampleDatasetId"; - - // Create TracerProvider that exports to a logger. - SdkTracerProvider tracerProvider = - SdkTracerProvider.builder() - .addSpanProcessor(SimpleSpanProcessor.builder(LoggingSpanExporter.create()).build()) - .setSampler(Sampler.alwaysOn()) - .build(); - - // Create OpenTelemetry instance using the TracerProvider. - OpenTelemetry otel = OpenTelemetrySdk.builder().setTracerProvider(tracerProvider).build(); - - // Create Tracer instance from the global OpenTelemetry object. Tracers are used to create - // Spans. There can be multiple Tracers in a global OpenTelemetry instance. - final Tracer tracer = otel.getTracer(tracerName); - enableOpenTelemetryWithParentSpan(tracer, parentSpanName, datasetId); - } - - public static void enableOpenTelemetryWithParentSpan( - Tracer tracer, String parentSpanName, String datasetId) { - // Create BigQuery client to trace. EnableOpenTelemetryTracing and OpenTelemetryTracer must - // be set to enable tracing. - BigQueryOptions otelOptions = - BigQueryOptions.newBuilder() - .setEnableOpenTelemetryTracing(true) - .setOpenTelemetryTracer(tracer) - .build(); - BigQuery bigquery = otelOptions.getService(); - - LocalDate currentDate = LocalDate.now(); - - // Create the root parent Span. setNoParent() ensures that it is a parent Span with a Span ID - // of 0. - Span parentSpan = - tracer - .spanBuilder(parentSpanName) - .setNoParent() - .setAttribute("current_date", currentDate.toString()) - .startSpan(); - - // The Span Context is automatically passed on to any functions called within the scope of the - // try block. parentSpan.makeCurrent() sets parentSpan to be the parent of any Spans created in - // this scope, or the scope of any functions called within this scope. - try (Scope parentScope = parentSpan.makeCurrent()) { - DatasetInfo info = DatasetInfo.newBuilder(datasetId).build(); - Dataset dataset = bigquery.create(info); - } catch (Exception e) { - System.out.println( - String.format("Failed to create dataset: %s: %s", e.toString(), e.getMessage())); - } finally { - // finally block ensures that Spans are cleaned up properly. - parentSpan.end(); - bigquery.delete(datasetId); - } - } -} -// [END bigquery_enable_otel_tracing_with_parent_span] diff --git a/samples/snippets/src/main/java/com/example/bigquery/QueryJobOptional.java b/samples/snippets/src/main/java/com/example/bigquery/QueryJobOptional.java deleted file mode 100644 index 1abf580671..0000000000 --- a/samples/snippets/src/main/java/com/example/bigquery/QueryJobOptional.java +++ /dev/null @@ -1,78 +0,0 @@ -/* - * Copyright 2024 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 - * - * 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.bigquery; - -// [START bigquery_query_job_optional] -import com.google.cloud.bigquery.BigQuery; -import com.google.cloud.bigquery.BigQueryException; -import com.google.cloud.bigquery.BigQueryOptions; -import com.google.cloud.bigquery.JobId; -import com.google.cloud.bigquery.QueryJobConfiguration; -import com.google.cloud.bigquery.QueryJobConfiguration.JobCreationMode; -import com.google.cloud.bigquery.TableResult; - -// Sample demonstrating short mode query execution. -// -// This feature is controlled by setting the defaultJobCreationMode -// field in the BigQueryOptions used for the client. JOB_CREATION_OPTIONAL -// allows for the execution of queries without creating a job. -public class QueryJobOptional { - - public static void main(String[] args) { - String query = - "SELECT name, gender, SUM(number) AS total FROM " - + "bigquery-public-data.usa_names.usa_1910_2013 GROUP BY " - + "name, gender ORDER BY total DESC LIMIT 10"; - queryJobOptional(query); - } - - public static void queryJobOptional(String query) { - try { - // Initialize client that will be used to send requests. This client only needs - // to be created once, and can be reused for multiple requests. - BigQueryOptions options = BigQueryOptions.getDefaultInstance(); - options.setDefaultJobCreationMode(JobCreationMode.JOB_CREATION_OPTIONAL); - BigQuery bigquery = options.getService(); - - // Execute the query. The returned TableResult provides access information - // about the query execution as well as query results. - TableResult results = bigquery.query(QueryJobConfiguration.of(query)); - - JobId jobId = results.getJobId(); - if (jobId != null) { - System.out.println("Query was run with job state. Job ID: " + jobId.toString()); - } else { - System.out.println("Query was run in short mode. Query ID: " + results.getQueryId()); - } - - // Print the results. - results - .iterateAll() - .forEach( - row -> { - System.out.print("name:" + row.get("name").getStringValue()); - System.out.print(", gender: " + row.get("gender").getStringValue()); - System.out.print(", total: " + row.get("total").getLongValue()); - System.out.println(); - }); - - } catch (BigQueryException | InterruptedException e) { - System.out.println("Query not performed \n" + e.toString()); - } - } -} -// [END bigquery_query_job_optional] diff --git a/samples/snippets/src/test/java/com/example/bigquery/EnableOpenTelemetryTracingIT.java b/samples/snippets/src/test/java/com/example/bigquery/EnableOpenTelemetryTracingIT.java deleted file mode 100644 index 0ad5651018..0000000000 --- a/samples/snippets/src/test/java/com/example/bigquery/EnableOpenTelemetryTracingIT.java +++ /dev/null @@ -1,105 +0,0 @@ -/* - * Copyright 2025 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 - * - * 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.bigquery; - -import static com.google.common.truth.Truth.assertThat; - -import com.google.cloud.bigquery.testing.RemoteBigQueryHelper; -import io.opentelemetry.api.OpenTelemetry; -import io.opentelemetry.api.trace.Tracer; -import io.opentelemetry.sdk.OpenTelemetrySdk; -import io.opentelemetry.sdk.common.CompletableResultCode; -import io.opentelemetry.sdk.trace.SdkTracerProvider; -import io.opentelemetry.sdk.trace.data.SpanData; -import io.opentelemetry.sdk.trace.export.SimpleSpanProcessor; -import io.opentelemetry.sdk.trace.samplers.Sampler; -import java.io.ByteArrayOutputStream; -import java.io.PrintStream; -import java.util.Collection; -import java.util.logging.Level; -import java.util.logging.Logger; -import org.junit.After; -import org.junit.Before; -import org.junit.Test; - -public class EnableOpenTelemetryTracingIT { - private final Logger log = Logger.getLogger(this.getClass().getName()); - private ByteArrayOutputStream bout; - private PrintStream out; - private PrintStream originalPrintStream; - - private static class ConsoleSpanExporter - implements io.opentelemetry.sdk.trace.export.SpanExporter { - @Override - public CompletableResultCode export(Collection collection) { - if (collection.isEmpty()) { - return CompletableResultCode.ofFailure(); - } - for (SpanData data : collection) { - System.out.println(data); - } - return CompletableResultCode.ofSuccess(); - } - - @Override - public CompletableResultCode flush() { - return CompletableResultCode.ofSuccess(); - } - - @Override - public CompletableResultCode shutdown() { - return CompletableResultCode.ofSuccess(); - } - } - - @Before - public void setUp() { - bout = new ByteArrayOutputStream(); - out = new PrintStream(bout); - originalPrintStream = System.out; - System.setOut(out); - } - - @After - public void tearDown() { - // restores print statements in the original method - System.out.flush(); - System.setOut(originalPrintStream); - log.log(Level.INFO, "\n" + bout.toString()); - } - - @Test - public void testEnableOpenTelemetryTracing() { - final String tracerName = "testSampleTracer"; - final String datasetId = RemoteBigQueryHelper.generateDatasetName(); - - SdkTracerProvider tracerProvider = - SdkTracerProvider.builder() - .addSpanProcessor(SimpleSpanProcessor.builder(new ConsoleSpanExporter()).build()) - .setSampler(Sampler.alwaysOn()) - .build(); - - OpenTelemetry otel = OpenTelemetrySdk.builder().setTracerProvider(tracerProvider).build(); - - final Tracer tracer = otel.getTracer(tracerName); - - EnableOpenTelemetryTracing.enableOpenTelemetry(tracer, datasetId); - - assertThat(bout.toString()).contains("com.google.cloud.bigquery.BigQuery.createDataset"); - assertThat(bout.toString()).contains("com.google.cloud.bigquery.BigQuery.deleteDataset"); - } -} diff --git a/samples/snippets/src/test/java/com/example/bigquery/EnableOpenTelemetryTracingWithParentSpanIT.java b/samples/snippets/src/test/java/com/example/bigquery/EnableOpenTelemetryTracingWithParentSpanIT.java deleted file mode 100644 index 482915008b..0000000000 --- a/samples/snippets/src/test/java/com/example/bigquery/EnableOpenTelemetryTracingWithParentSpanIT.java +++ /dev/null @@ -1,110 +0,0 @@ -/* - * Copyright 2025 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 - * - * 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.bigquery; - -import static com.google.common.truth.Truth.assertThat; - -import com.google.cloud.bigquery.testing.RemoteBigQueryHelper; -import io.opentelemetry.api.OpenTelemetry; -import io.opentelemetry.api.trace.Tracer; -import io.opentelemetry.sdk.OpenTelemetrySdk; -import io.opentelemetry.sdk.common.CompletableResultCode; -import io.opentelemetry.sdk.trace.SdkTracerProvider; -import io.opentelemetry.sdk.trace.data.SpanData; -import io.opentelemetry.sdk.trace.export.SimpleSpanProcessor; -import io.opentelemetry.sdk.trace.samplers.Sampler; -import java.io.ByteArrayOutputStream; -import java.io.PrintStream; -import java.time.LocalDate; -import java.util.Collection; -import java.util.logging.Level; -import java.util.logging.Logger; -import org.junit.After; -import org.junit.Before; -import org.junit.Test; - -public class EnableOpenTelemetryTracingWithParentSpanIT { - private final Logger log = Logger.getLogger(this.getClass().getName()); - private ByteArrayOutputStream bout; - private PrintStream out; - private PrintStream originalPrintStream; - - private static class ConsoleSpanExporter - implements io.opentelemetry.sdk.trace.export.SpanExporter { - @Override - public CompletableResultCode export(Collection collection) { - if (collection.isEmpty()) { - return CompletableResultCode.ofFailure(); - } - for (SpanData data : collection) { - System.out.println(data); - } - return CompletableResultCode.ofSuccess(); - } - - @Override - public CompletableResultCode flush() { - return CompletableResultCode.ofSuccess(); - } - - @Override - public CompletableResultCode shutdown() { - return CompletableResultCode.ofSuccess(); - } - } - - @Before - public void setUp() { - bout = new ByteArrayOutputStream(); - out = new PrintStream(bout); - originalPrintStream = System.out; - System.setOut(out); - } - - @After - public void tearDown() { - // restores print statements in the original method - System.out.flush(); - System.setOut(originalPrintStream); - log.log(Level.INFO, "\n" + bout.toString()); - } - - @Test - public void testEnableOpenTelemetryWithParentSpan() { - final String tracerName = "testSampleTracer"; - final String parentSpanName = "testSampleParentSpan"; - final String datasetId = RemoteBigQueryHelper.generateDatasetName(); - final LocalDate currentDate = LocalDate.now(); - - SdkTracerProvider tracerProvider = - SdkTracerProvider.builder() - .addSpanProcessor(SimpleSpanProcessor.builder(new ConsoleSpanExporter()).build()) - .setSampler(Sampler.alwaysOn()) - .build(); - - OpenTelemetry otel = OpenTelemetrySdk.builder().setTracerProvider(tracerProvider).build(); - - final Tracer tracer = otel.getTracer(tracerName); - - EnableOpenTelemetryTracingWithParentSpan.enableOpenTelemetryWithParentSpan( - tracer, parentSpanName, datasetId); - - assertThat(bout.toString()).contains(parentSpanName); - assertThat(bout.toString()) - .contains(String.format("AttributesMap{data={current_date=%s}", currentDate.toString())); - } -} diff --git a/samples/snippets/src/test/java/com/example/bigquery/QueryJobOptionalIT.java b/samples/snippets/src/test/java/com/example/bigquery/QueryJobOptionalIT.java deleted file mode 100644 index 8e3e979098..0000000000 --- a/samples/snippets/src/test/java/com/example/bigquery/QueryJobOptionalIT.java +++ /dev/null @@ -1,62 +0,0 @@ -/* - * 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 - * - * 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.bigquery; - -import static com.google.common.truth.Truth.assertThat; - -import java.io.ByteArrayOutputStream; -import java.io.PrintStream; -import java.util.logging.Level; -import java.util.logging.Logger; -import org.junit.After; -import org.junit.Before; -import org.junit.Test; - -public class QueryJobOptionalIT { - - private final Logger log = Logger.getLogger(this.getClass().getName()); - private ByteArrayOutputStream bout; - private PrintStream out; - private PrintStream originalPrintStream; - - @Before - public void setUp() { - bout = new ByteArrayOutputStream(); - out = new PrintStream(bout); - originalPrintStream = System.out; - System.setOut(out); - } - - @After - public void tearDown() { - // restores print statements in the original method - System.out.flush(); - System.setOut(originalPrintStream); - log.log(Level.INFO, "\n" + bout.toString()); - } - - @Test - public void testQueryBatch() { - String query = - "SELECT name, gender, SUM(number) AS total FROM " - + "bigquery-public-data.usa_names.usa_1910_2013 GROUP BY " - + "name, gender ORDER BY total DESC LIMIT 10"; - - QueryJobOptional.queryJobOptional(query); - assertThat(bout.toString()).contains("Query was run"); - } -} diff --git a/samples/snippets/src/test/java/com/example/bigquery/QueryLargeResultsIT.java b/samples/snippets/src/test/java/com/example/bigquery/QueryLargeResultsIT.java index 11efa613bb..486af9365d 100644 --- a/samples/snippets/src/test/java/com/example/bigquery/QueryLargeResultsIT.java +++ b/samples/snippets/src/test/java/com/example/bigquery/QueryLargeResultsIT.java @@ -73,6 +73,7 @@ public void tearDown() { @Test public void testQueryLargeResults() { + // TODO: Do not merge. String query = "SELECT corpus FROM [bigquery-public-data:samples.shakespeare] GROUP BY corpus;"; QueryLargeResults.queryLargeResults(BIGQUERY_DATASET_NAME, tableName, query); assertThat(bout.toString()).contains("Query large results performed successfully.");