Query data with commit timestamp
Stay organized with collections
Save and categorize content based on your preferences.
Query data from a table containing a commit TIMESTAMP column.
Explore further
For detailed documentation that includes this code sample, see the following:
Code sample
C++
To learn how to install and use the client library for Spanner, see Spanner client libraries.
To authenticate to Spanner, set up Application Default Credentials. For more information, see Set up authentication for a local development environment.
voidQueryDataWithTimestamp(google::cloud::spanner::Clientclient){
namespacespanner=::google::cloud::spanner;
spanner::SqlStatementselect(
"SELECT SingerId, AlbumId, MarketingBudget, LastUpdateTime"
" FROM Albums"
" ORDER BY LastUpdateTime DESC");
usingRowType=
std::tuple<std::int64_t,std::int64_t,absl::optional<std::int64_t>,
absl::optional<spanner::Timestamp>>;
autorows=client.ExecuteQuery(std::move(select));
for(auto&row:spanner::StreamOf<RowType>(rows)){
if(!row)throwstd::move(row).status();
std::cout << std::get<0>(*row) << " " << std::get<1>(*row);
automarketing_budget=std::get<2>(*row);
if(!marketing_budget){
std::cout << " NULL";
}else{
std::cout << ' ' << *marketing_budget;
}
autolast_update_time=std::get<3>(*row);
if(!last_update_time){
std::cout << " NULL";
}else{
std::cout << ' ' << *last_update_time;
}
std::cout << "\n";
}
}
C#
To learn how to install and use the client library for Spanner, see Spanner client libraries.
To authenticate to Spanner, set up Application Default Credentials. For more information, see Set up authentication for a local development environment.
usingGoogle.Cloud.Spanner.Data ;
usingSystem;
usingSystem.Collections.Generic;
usingSystem.Threading.Tasks;
publicclassQueryDataWithTimestampColumnAsyncSample
{
publicclassAlbum
{
publicintSingerId{get;set;}
publicintAlbumId{get;set;}
publicDateTime?LastUpdateTime{get;set;}
publiclong?MarketingBudget{get;set;}
}
publicasyncTask<List<Album>>QueryDataWithTimestampColumnAsync(stringprojectId,stringinstanceId,stringdatabaseId)
{
stringconnectionString=$"Data Source=projects/{projectId}/instances/{instanceId}/databases/{databaseId}";
usingvarconnection=newSpannerConnection (connectionString);
usingvarcmd=connection.CreateSelectCommand ("SELECT SingerId, AlbumId, MarketingBudget, LastUpdateTime FROM Albums ORDER BY LastUpdateTime DESC");
varalbums=newList<Album>();
usingvarreader=awaitcmd.ExecuteReaderAsync ();
while(awaitreader.ReadAsync ())
{
albums.Add(newAlbum
{
SingerId=reader.GetFieldValue<int>("SingerId"),
AlbumId=reader.GetFieldValue<int>("AlbumId"),
LastUpdateTime=reader.IsDBNull (reader.GetOrdinal ("LastUpdateTime"))?(DateTime?)null:reader.GetFieldValue<DateTime>("LastUpdateTime"),
MarketingBudget=reader.IsDBNull (reader.GetOrdinal ("MarketingBudget"))?0:reader.GetFieldValue<long>("MarketingBudget")
});
}
returnalbums;
}
}
Go
To learn how to install and use the client library for Spanner, see Spanner client libraries.
To authenticate to Spanner, set up Application Default Credentials. For more information, see Set up authentication for a local development environment.
import(
"context"
"fmt"
"io"
"strconv"
"cloud.google.com/go/spanner"
"google.golang.org/api/iterator"
)
funcqueryWithTimestamp(wio.Writer,dbstring)error{
ctx:=context.Background()
client,err:=spanner.NewClient(ctx,db)
iferr!=nil{
returnerr
}
deferclient.Close()
stmt:=spanner.Statement {
SQL:`SELECT SingerId, AlbumId, MarketingBudget, LastUpdateTime
FROM Albums ORDER BY LastUpdateTime DESC`}
iter:=client.Single ().Query(ctx,stmt)
deferiter.Stop()
for{
row,err:=iter.Next()
iferr==iterator.Done{
returnnil
}
iferr!=nil{
returnerr
}
varsingerID,albumIDint64
varmarketingBudgetspanner.NullInt64
varlastUpdateTimespanner.NullTime
iferr:=row.ColumnByName ("SingerId",&singerID);err!=nil{
returnerr
}
iferr:=row.ColumnByName ("AlbumId",&albumID);err!=nil{
returnerr
}
iferr:=row.ColumnByName ("MarketingBudget",&marketingBudget);err!=nil{
returnerr
}
budget:="NULL"
ifmarketingBudget.Valid{
budget=strconv.FormatInt(marketingBudget.Int64 ,10)
}
iferr:=row.ColumnByName ("LastUpdateTime",&lastUpdateTime);err!=nil{
returnerr
}
timestamp:="NULL"
iflastUpdateTime.Valid{
timestamp=lastUpdateTime.String()
}
fmt.Fprintf(w,"%d %d %s %s\n",singerID,albumID,budget,timestamp)
}
}
Java
To learn how to install and use the client library for Spanner, see Spanner client libraries.
To authenticate to Spanner, set up Application Default Credentials. For more information, see Set up authentication for a local development environment.
staticvoidqueryMarketingBudgetWithTimestamp(DatabaseClientdbClient){
// Rows without an explicit value for MarketingBudget will have a MarketingBudget equal to
// null. A try-with-resource block is used to automatically release resources held by
// ResultSet.
try(ResultSetresultSet=
dbClient
.singleUse()
.executeQuery(
Statement.of(
"SELECT SingerId, AlbumId, MarketingBudget, LastUpdateTime FROM Albums"
+" ORDER BY LastUpdateTime DESC"))){
while(resultSet.next()){
System.out.printf(
"%d %d %s %s\n",
resultSet.getLong("SingerId"),
resultSet.getLong("AlbumId"),
// We check that the value is non null. ResultSet getters can only be used to retrieve
// non null values.
resultSet.isNull("MarketingBudget")?"NULL":resultSet.getLong("MarketingBudget"),
resultSet.isNull("LastUpdateTime")?"NULL":resultSet.getTimestamp("LastUpdateTime"));
}
}
}
Node.js
To learn how to install and use the client library for Spanner, see Spanner client libraries.
To authenticate to Spanner, set up Application Default Credentials. For more information, see Set up authentication for a local development environment.
// ...
// Imports the Google Cloud client library
const{Spanner}=require('@google-cloud/spanner');
/**
* TODO(developer): Uncomment the following lines before running the sample.
*/
// const projectId = 'my-project-id';
// const instanceId = 'my-instance';
// const databaseId = 'my-database';
// Creates a client
constspanner=newSpanner ({
projectId:projectId,
});
// Gets a reference to a Cloud Spanner instance and database
constinstance=spanner.instance(instanceId);
constdatabase=instance.database(databaseId);
constquery={
sql:`SELECT SingerId, AlbumId, MarketingBudget, LastUpdateTime
FROM Albums ORDER BY LastUpdateTime DESC`,
};
// Queries rows from the Albums table
try{
const[rows]=awaitdatabase.run(query);
rows.forEach(row=>{
constjson=row.toJSON();
console.log(
`SingerId: ${json.SingerId}, AlbumId: ${
json.AlbumId
}, MarketingBudget: ${
json.MarketingBudget?json.MarketingBudget:null
}, LastUpdateTime: ${json.LastUpdateTime}`,
);
});
}catch(err){
console.error('ERROR:',err);
}finally{
// Close the database when finished
database.close();
}
PHP
To learn how to install and use the client library for Spanner, see Spanner client libraries.
To authenticate to Spanner, set up Application Default Credentials. For more information, see Set up authentication for a local development environment.
use Google\Cloud\Spanner\SpannerClient;
/**
* Queries sample data from a database with a commit timestamp column.
*
* This sample uses the `MarketingBudget` column. You can add the column
* by running the `add_column` sample or by running this DDL statement against
* your database:
*
* ALTER TABLE Albums ADD COLUMN MarketingBudget INT64
*
* This sample also uses the 'LastUpdateTime' commit timestamp column. You can
* add the column by running the `add_timestamp_column` sample or by running
* this DDL statement against your database:
*
* ALTER TABLE Albums ADD COLUMN LastUpdateTime TIMESTAMP OPTIONS (allow_commit_timestamp=true)
*
* Example:
* ```
* query_data_with_timestamp_column($instanceId, $databaseId);
* ```
*
* @param string $instanceId The Spanner instance ID.
* @param string $databaseId The Spanner database ID.
*/
function query_data_with_timestamp_column(string $instanceId, string $databaseId): void
{
$spanner = new SpannerClient();
$instance = $spanner->instance($instanceId);
$database = $instance->database($databaseId);
$results = $database->execute(
'SELECT SingerId, AlbumId, MarketingBudget, LastUpdateTime ' .
' FROM Albums ORDER BY LastUpdateTime DESC'
);
foreach ($results as $row) {
if ($row['MarketingBudget'] == null) {
$row['MarketingBudget'] = 'NULL';
}
if ($row['LastUpdateTime'] == null) {
$row['LastUpdateTime'] = 'NULL';
}
printf('SingerId: %s, AlbumId: %s, MarketingBudget: %s, LastUpdateTime: %s' . PHP_EOL,
$row['SingerId'], $row['AlbumId'], $row['MarketingBudget'], $row['LastUpdateTime']);
}
}
Python
To learn how to install and use the client library for Spanner, see Spanner client libraries.
To authenticate to Spanner, set up Application Default Credentials. For more information, see Set up authentication for a local development environment.
defquery_data_with_timestamp(instance_id, database_id):
"""Queries sample data from the database using SQL.
This updates the `LastUpdateTime` column which must be created before
running this sample. You can add the column by running the
`add_timestamp_column` sample or by running this DDL statement
against your database:
ALTER TABLE Performances ADD COLUMN LastUpdateTime TIMESTAMP
OPTIONS (allow_commit_timestamp=true)
"""
spanner_client = spanner.Client()
instance = spanner_client.instance(instance_id)
database = instance.database(database_id)
with database.snapshot() as snapshot:
results = snapshot.execute_sql(
"SELECT SingerId, AlbumId, MarketingBudget FROM Albums "
"ORDER BY LastUpdateTime DESC"
)
for row in results:
print("SingerId: {}, AlbumId: {}, MarketingBudget: {}".format(*row))
Ruby
To learn how to install and use the client library for Spanner, see Spanner client libraries.
To authenticate to Spanner, set up Application Default Credentials. For more information, see Set up authentication for a local development environment.
# project_id = "Your Google Cloud project ID"
# instance_id = "Your Spanner instance ID"
# database_id = "Your Spanner database ID"
require"google/cloud/spanner"
spanner=Google::Cloud::Spanner .newproject:project_id
client=spanner.client instance_id,database_id
client.execute("SELECT SingerId, AlbumId, MarketingBudget, LastUpdateTime
FROM Albums ORDER BY LastUpdateTime DESC").rows.each do|row|
puts"#{row[:SingerId]}#{row[:AlbumId]}#{row[:MarketingBudget]}#{row[:LastUpdateTime]}"
end
What's next
To search and filter code samples for other Google Cloud products, see the Google Cloud sample browser.