Skip to content

Commit f14db0a

Browse files
committed
Merge branch 'main' into PR #3867 to update
2 parents f6399a4 + fac16a8 commit f14db0a

File tree

6 files changed

+187
-14
lines changed

6 files changed

+187
-14
lines changed

google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/it/ITBigQueryTest.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5980,9 +5980,11 @@ public void testSnapshotTableCopyJob() throws InterruptedException {
59805980

59815981
@Test
59825982
public void testCopyJobWithLabelsAndExpTime() throws InterruptedException {
5983-
String destExpiryTime = "2025-12-31T23:59:59.999999999Z";
5984-
String sourceTableName = "test_copy_job_source_table_label";
5985-
String destinationTableName = "test_copy_job_destination_table_label";
5983+
String destExpiryTime = "2099-12-31T23:59:59.999999999Z";
5984+
String sourceTableName =
5985+
"test_copy_job_source_table_label" + UUID.randomUUID().toString().substring(0, 8);
5986+
String destinationTableName =
5987+
"test_copy_job_destination_table_label" + UUID.randomUUID().toString().substring(0, 8);
59865988
Map<String, String> labels = ImmutableMap.of("test_job_name", "test_copy_job");
59875989
TableId sourceTable = TableId.of(DATASET, sourceTableName);
59885990
StandardTableDefinition tableDefinition = StandardTableDefinition.of(TABLE_SCHEMA);
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
/*
2+
* Copyright 2026 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+
* 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.bigquery;
18+
19+
// [START bigquery_create_table_timestamp]
20+
import com.google.cloud.bigquery.BigQuery;
21+
import com.google.cloud.bigquery.BigQueryException;
22+
import com.google.cloud.bigquery.BigQueryOptions;
23+
import com.google.cloud.bigquery.Field;
24+
import com.google.cloud.bigquery.Schema;
25+
import com.google.cloud.bigquery.StandardSQLTypeName;
26+
import com.google.cloud.bigquery.StandardTableDefinition;
27+
import com.google.cloud.bigquery.TableDefinition;
28+
import com.google.cloud.bigquery.TableId;
29+
import com.google.cloud.bigquery.TableInfo;
30+
31+
public class CreateTableTimestamp {
32+
33+
public static void main(String[] args) {
34+
// TODO(developer): Replace these variables before running the sample.
35+
String datasetName = "MY_DATASET_NAME";
36+
String tableName = "MY_TABLE_NAME";
37+
Schema schema =
38+
Schema.of(Field.newBuilder("timestampField", StandardSQLTypeName.TIMESTAMP).build());
39+
createTable(datasetName, tableName, schema);
40+
}
41+
42+
public static void createTable(String datasetName, String tableName, Schema schema) {
43+
try {
44+
// Initialize client that will be used to send requests. This client only needs to be created
45+
// once, and can be reused for multiple requests.
46+
BigQuery bigquery = BigQueryOptions.getDefaultInstance().getService();
47+
48+
TableId tableId = TableId.of(datasetName, tableName);
49+
TableDefinition tableDefinition = StandardTableDefinition.of(schema);
50+
TableInfo tableInfo = TableInfo.newBuilder(tableId, tableDefinition).build();
51+
52+
bigquery.create(tableInfo);
53+
System.out.println("Table created successfully");
54+
} catch (BigQueryException e) {
55+
System.out.println("Table was not created. \n" + e);
56+
}
57+
}
58+
}
59+
// [END bigquery_create_table_timestamp]

samples/snippets/src/main/java/com/example/bigquery/QueryWithTimestampParameters.java

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,36 @@
3030
// Sample to running a query with timestamp query parameters.
3131
public class QueryWithTimestampParameters {
3232

33-
public static void main(String[] args) {
34-
queryWithTimestampParameters();
33+
public static void queryFromTableTimestampParameters() {
34+
try {
35+
// Initialize client that will be used to send requests. This client only needs to be created
36+
// once, and can be reused for multiple requests.
37+
BigQuery bigquery = BigQueryOptions.getDefaultInstance().getService();
38+
39+
ZonedDateTime timestamp = LocalDateTime.of(2016, 12, 7, 8, 0, 0).atZone(ZoneOffset.UTC);
40+
String query = "SELECT last_reported FROM "
41+
+ "`bigquery-public-data`.new_york_citibike.citibike_stations"
42+
+ " WHERE last_reported >= @ts_value LIMIT 5";
43+
// Note: Standard SQL is required to use query parameters.
44+
QueryJobConfiguration queryConfig =
45+
QueryJobConfiguration.newBuilder(query)
46+
.addNamedParameter(
47+
"ts_value",
48+
QueryParameterValue.timestamp(
49+
// Timestamp takes microseconds since 1970-01-01T00:00:00 UTC
50+
timestamp.toInstant().toEpochMilli() * 1000))
51+
.build();
52+
53+
TableResult results = bigquery.query(queryConfig);
54+
55+
results
56+
.iterateAll()
57+
.forEach(row -> row.forEach(val -> System.out.printf("%s\n", val.toString())));
58+
59+
System.out.println("Query with timestamp parameter performed successfully.");
60+
} catch (BigQueryException | InterruptedException e) {
61+
System.out.println("Query not performed \n" + e);
62+
}
3563
}
3664

3765
public static void queryWithTimestampParameters() {
@@ -60,7 +88,7 @@ public static void queryWithTimestampParameters() {
6088

6189
System.out.println("Query with timestamp parameter performed successfully.");
6290
} catch (BigQueryException | InterruptedException e) {
63-
System.out.println("Query not performed \n" + e.toString());
91+
System.out.println("Query not performed \n" + e);
6492
}
6593
}
6694
}

samples/snippets/src/test/java/com/example/bigquery/CreateTableIT.java

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -37,26 +37,25 @@ public class CreateTableIT {
3737
private final Logger log = Logger.getLogger(this.getClass().getName());
3838
private String tableName;
3939
private ByteArrayOutputStream bout;
40-
private PrintStream out;
4140
private PrintStream originalPrintStream;
4241

4342
private static final String BIGQUERY_DATASET_NAME = System.getenv("BIGQUERY_DATASET_NAME");
4443

45-
private static void requireEnvVar(String varName) {
44+
private static void requireEnvVar() {
4645
assertNotNull(
47-
"Environment variable " + varName + " is required to perform these tests.",
48-
System.getenv(varName));
46+
"Environment variable BIGQUERY_DATASET_NAME is required to perform these tests.",
47+
System.getenv("BIGQUERY_DATASET_NAME"));
4948
}
5049

5150
@BeforeClass
5251
public static void checkRequirements() {
53-
requireEnvVar("BIGQUERY_DATASET_NAME");
52+
requireEnvVar();
5453
}
5554

5655
@Before
5756
public void setUp() {
5857
bout = new ByteArrayOutputStream();
59-
out = new PrintStream(bout);
58+
PrintStream out = new PrintStream(bout);
6059
originalPrintStream = System.out;
6160
System.setOut(out);
6261
tableName = "MY_TABLE_NAME_" + UUID.randomUUID().toString().replace("-", "_");
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
/*
2+
* Copyright 2026 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+
* 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.bigquery;
18+
19+
import static com.google.common.truth.Truth.assertThat;
20+
import static org.junit.Assert.assertNotNull;
21+
22+
import com.google.cloud.bigquery.Field;
23+
import com.google.cloud.bigquery.Schema;
24+
import com.google.cloud.bigquery.StandardSQLTypeName;
25+
import java.io.ByteArrayOutputStream;
26+
import java.io.PrintStream;
27+
import java.util.UUID;
28+
import java.util.logging.Level;
29+
import java.util.logging.Logger;
30+
import org.junit.After;
31+
import org.junit.Before;
32+
import org.junit.BeforeClass;
33+
import org.junit.Test;
34+
35+
public class CreateTableTimestampIT {
36+
private final Logger log = Logger.getLogger(this.getClass().getName());
37+
private String tableName;
38+
private ByteArrayOutputStream bout;
39+
private PrintStream originalPrintStream;
40+
41+
private static final String BIGQUERY_DATASET_NAME = System.getenv("BIGQUERY_DATASET_NAME");
42+
43+
private static void requireEnvVar() {
44+
assertNotNull(
45+
"Environment variable BIGQUERY_DATASET_NAME is required to perform these tests.",
46+
System.getenv("BIGQUERY_DATASET_NAME"));
47+
}
48+
49+
@BeforeClass
50+
public static void checkRequirements() {
51+
requireEnvVar();
52+
}
53+
54+
@Before
55+
public void setUp() {
56+
bout = new ByteArrayOutputStream();
57+
PrintStream out = new PrintStream(bout);
58+
originalPrintStream = System.out;
59+
System.setOut(out);
60+
tableName = "MY_TABLE_NAME_" + UUID.randomUUID().toString().replace("-", "_");
61+
}
62+
63+
@After
64+
public void tearDown() {
65+
// Clean up
66+
DeleteTable.deleteTable(BIGQUERY_DATASET_NAME, tableName);
67+
// restores print statements in the original method
68+
System.out.flush();
69+
System.setOut(originalPrintStream);
70+
log.log(Level.INFO, "\n" + bout.toString());
71+
}
72+
73+
@Test
74+
public void testCreateTable() {
75+
Schema schema =
76+
Schema.of(Field.of("timestampField", StandardSQLTypeName.TIMESTAMP));
77+
CreateTableTimestamp.createTable(BIGQUERY_DATASET_NAME, tableName, schema);
78+
assertThat(bout.toString()).contains("Table created successfully");
79+
}
80+
}

samples/snippets/src/test/java/com/example/bigquery/QueryWithTimestampParametersIT.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,12 @@ public class QueryWithTimestampParametersIT {
3030

3131
private final Logger log = Logger.getLogger(this.getClass().getName());
3232
private ByteArrayOutputStream bout;
33-
private PrintStream out;
3433
private PrintStream originalPrintStream;
3534

3635
@Before
3736
public void setUp() {
3837
bout = new ByteArrayOutputStream();
39-
out = new PrintStream(bout);
38+
PrintStream out = new PrintStream(bout);
4039
originalPrintStream = System.out;
4140
System.setOut(out);
4241
}
@@ -54,4 +53,10 @@ public void testQueryWithTimestampParameters() {
5453
QueryWithTimestampParameters.queryWithTimestampParameters();
5554
assertThat(bout.toString()).contains("Query with timestamp parameter performed successfully.");
5655
}
56+
57+
@Test
58+
public void testQueryFromTableTimestampParameters() {
59+
QueryWithTimestampParameters.queryFromTableTimestampParameters();
60+
assertThat(bout.toString()).contains("Query with timestamp parameter performed successfully.");
61+
}
5762
}

0 commit comments

Comments
 (0)