Skip to content
This repository was archived by the owner on Mar 23, 2026. It is now read-only.

Commit 04b60e8

Browse files
committed
docs: Add specific samples for creating and query timestamps
1 parent 2578dcb commit 04b60e8

5 files changed

Lines changed: 181 additions & 11 deletions

File tree

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: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,34 @@
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 bigquery-public-data.new_york_citibike.citibike_stations WHERE last_reported >= @ts_value LIMIT 5";
41+
// Note: Standard SQL is required to use query parameters.
42+
QueryJobConfiguration queryConfig =
43+
QueryJobConfiguration.newBuilder(query)
44+
.addNamedParameter(
45+
"ts_value",
46+
QueryParameterValue.timestamp(
47+
// Timestamp takes microseconds since 1970-01-01T00:00:00 UTC
48+
timestamp.toInstant().toEpochMilli() * 1000))
49+
.build();
50+
51+
TableResult results = bigquery.query(queryConfig);
52+
53+
results
54+
.iterateAll()
55+
.forEach(row -> row.forEach(val -> System.out.printf("%s\n", val.toString())));
56+
57+
System.out.println("Query with timestamp parameter performed successfully.");
58+
} catch (BigQueryException | InterruptedException e) {
59+
System.out.println("Query not performed \n" + e);
60+
}
3561
}
3662

3763
public static void queryWithTimestampParameters() {
@@ -60,7 +86,7 @@ public static void queryWithTimestampParameters() {
6086

6187
System.out.println("Query with timestamp parameter performed successfully.");
6288
} catch (BigQueryException | InterruptedException e) {
63-
System.out.println("Query not performed \n" + e.toString());
89+
System.out.println("Query not performed \n" + e);
6490
}
6591
}
6692
}

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

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)