Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -5980,9 +5980,11 @@ public void testSnapshotTableCopyJob() throws InterruptedException {

@Test
public void testCopyJobWithLabelsAndExpTime() throws InterruptedException {
String destExpiryTime = "2025-12-31T23:59:59.999999999Z";
String sourceTableName = "test_copy_job_source_table_label";
String destinationTableName = "test_copy_job_destination_table_label";
String destExpiryTime = "2099-12-31T23:59:59.999999999Z";
String sourceTableName =
"test_copy_job_source_table_label" + UUID.randomUUID().toString().substring(0, 8);
String destinationTableName =
"test_copy_job_destination_table_label" + UUID.randomUUID().toString().substring(0, 8);
Map<String, String> labels = ImmutableMap.of("test_job_name", "test_copy_job");
TableId sourceTable = TableId.of(DATASET, sourceTableName);
StandardTableDefinition tableDefinition = StandardTableDefinition.of(TABLE_SCHEMA);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/*
* Copyright 2026 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_create_table_timestamp]
import com.google.cloud.bigquery.BigQuery;
import com.google.cloud.bigquery.BigQueryException;
import com.google.cloud.bigquery.BigQueryOptions;
import com.google.cloud.bigquery.Field;
import com.google.cloud.bigquery.Schema;
import com.google.cloud.bigquery.StandardSQLTypeName;
import com.google.cloud.bigquery.StandardTableDefinition;
import com.google.cloud.bigquery.TableDefinition;
import com.google.cloud.bigquery.TableId;
import com.google.cloud.bigquery.TableInfo;

public class CreateTableTimestamp {

public static void main(String[] args) {
// TODO(developer): Replace these variables before running the sample.
String datasetName = "MY_DATASET_NAME";
String tableName = "MY_TABLE_NAME";
Schema schema =
Schema.of(Field.newBuilder("timestampField", StandardSQLTypeName.TIMESTAMP).build());
createTable(datasetName, tableName, schema);
}

public static void createTable(String datasetName, String tableName, Schema schema) {
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.
BigQuery bigquery = BigQueryOptions.getDefaultInstance().getService();

TableId tableId = TableId.of(datasetName, tableName);
TableDefinition tableDefinition = StandardTableDefinition.of(schema);
TableInfo tableInfo = TableInfo.newBuilder(tableId, tableDefinition).build();

bigquery.create(tableInfo);
System.out.println("Table created successfully");
} catch (BigQueryException e) {
System.out.println("Table was not created. \n" + e);
}
}
}
// [END bigquery_create_table_timestamp]
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,36 @@
// Sample to running a query with timestamp query parameters.
public class QueryWithTimestampParameters {

public static void main(String[] args) {
queryWithTimestampParameters();
public static void queryFromTableTimestampParameters() {
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.
BigQuery bigquery = BigQueryOptions.getDefaultInstance().getService();

ZonedDateTime timestamp = LocalDateTime.of(2016, 12, 7, 8, 0, 0).atZone(ZoneOffset.UTC);
String query = "SELECT last_reported FROM "
+ "`bigquery-public-data`.new_york_citibike.citibike_stations"
+ " WHERE last_reported >= @ts_value LIMIT 5";
// Note: Standard SQL is required to use query parameters.
QueryJobConfiguration queryConfig =
QueryJobConfiguration.newBuilder(query)
.addNamedParameter(
"ts_value",
QueryParameterValue.timestamp(
// Timestamp takes microseconds since 1970-01-01T00:00:00 UTC
timestamp.toInstant().toEpochMilli() * 1000))
.build();

TableResult results = bigquery.query(queryConfig);

results
.iterateAll()
.forEach(row -> row.forEach(val -> System.out.printf("%s\n", val.toString())));

System.out.println("Query with timestamp parameter performed successfully.");
} catch (BigQueryException | InterruptedException e) {
System.out.println("Query not performed \n" + e);
}
}

public static void queryWithTimestampParameters() {
Expand Down Expand Up @@ -60,7 +88,7 @@ public static void queryWithTimestampParameters() {

System.out.println("Query with timestamp parameter performed successfully.");
} catch (BigQueryException | InterruptedException e) {
System.out.println("Query not performed \n" + e.toString());
System.out.println("Query not performed \n" + e);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,26 +37,25 @@ public class CreateTableIT {
private final Logger log = Logger.getLogger(this.getClass().getName());
private String tableName;
private ByteArrayOutputStream bout;
private PrintStream out;
private PrintStream originalPrintStream;

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

private static void requireEnvVar(String varName) {
private static void requireEnvVar() {
assertNotNull(
"Environment variable " + varName + " is required to perform these tests.",
System.getenv(varName));
"Environment variable BIGQUERY_DATASET_NAME is required to perform these tests.",
System.getenv("BIGQUERY_DATASET_NAME"));
}

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

@Before
public void setUp() {
bout = new ByteArrayOutputStream();
out = new PrintStream(bout);
PrintStream out = new PrintStream(bout);
originalPrintStream = System.out;
System.setOut(out);
tableName = "MY_TABLE_NAME_" + UUID.randomUUID().toString().replace("-", "_");
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
/*
* Copyright 2026 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 static org.junit.Assert.assertNotNull;

import com.google.cloud.bigquery.Field;
import com.google.cloud.bigquery.Schema;
import com.google.cloud.bigquery.StandardSQLTypeName;
import java.io.ByteArrayOutputStream;
import java.io.PrintStream;
import java.util.UUID;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.junit.After;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;

public class CreateTableTimestampIT {
private final Logger log = Logger.getLogger(this.getClass().getName());
private String tableName;
private ByteArrayOutputStream bout;
private PrintStream originalPrintStream;

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

private static void requireEnvVar() {
assertNotNull(
"Environment variable BIGQUERY_DATASET_NAME is required to perform these tests.",
System.getenv("BIGQUERY_DATASET_NAME"));
}

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

@Before
public void setUp() {
bout = new ByteArrayOutputStream();
PrintStream out = new PrintStream(bout);
originalPrintStream = System.out;
System.setOut(out);
tableName = "MY_TABLE_NAME_" + UUID.randomUUID().toString().replace("-", "_");
}

@After
public void tearDown() {
// Clean up
DeleteTable.deleteTable(BIGQUERY_DATASET_NAME, tableName);
// restores print statements in the original method
System.out.flush();
System.setOut(originalPrintStream);
log.log(Level.INFO, "\n" + bout.toString());
}

@Test
public void testCreateTable() {
Schema schema =
Schema.of(Field.of("timestampField", StandardSQLTypeName.TIMESTAMP));
CreateTableTimestamp.createTable(BIGQUERY_DATASET_NAME, tableName, schema);
assertThat(bout.toString()).contains("Table created successfully");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,12 @@ public class QueryWithTimestampParametersIT {

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);
PrintStream out = new PrintStream(bout);
originalPrintStream = System.out;
System.setOut(out);
}
Expand All @@ -54,4 +53,10 @@ public void testQueryWithTimestampParameters() {
QueryWithTimestampParameters.queryWithTimestampParameters();
assertThat(bout.toString()).contains("Query with timestamp parameter performed successfully.");
}

@Test
public void testQueryFromTableTimestampParameters() {
QueryWithTimestampParameters.queryFromTableTimestampParameters();
assertThat(bout.toString()).contains("Query with timestamp parameter performed successfully.");
}
}
Loading