Mutations write data with TIMESTAMP column

Write data into a table containing a TIMESTAMP column by using mutations.

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.

voidInsertDataWithTimestamp(google::cloud::spanner::Clientclient){
namespacespanner=::google::cloud::spanner;
autocommit_result=client.Commit(spanner::Mutations{
spanner::InsertOrUpdateMutationBuilder(
"Performances",
{"SingerId","VenueId","EventDate","Revenue","LastUpdateTime"})
.EmplaceRow(1,4,absl::CivilDay(2017,10,5),11000,
spanner::CommitTimestamp{})
.EmplaceRow(1,19,absl::CivilDay(2017,11,2),15000,
spanner::CommitTimestamp{})
.EmplaceRow(2,42,absl::CivilDay(2017,12,23),7000,
spanner::CommitTimestamp{})
.Build()});
if(!commit_result)throwstd::move(commit_result).status();
std::cout
 << "Update was successful [spanner_insert_data_with_timestamp_column]\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.Linq;
usingSystem.Threading.Tasks;
publicclassWriteDataWithTimestampAsyncSample
{
publicclassPerformance
{
publicintSingerId{get;set;}
publicintVenueId{get;set;}
publicDateTimeEventDate{get;set;}
publiclongRevenue{get;set;}
}
publicasyncTask<int>WriteDataWithTimestampAsync(stringprojectId,stringinstanceId,stringdatabaseId)
{
stringconnectionString=$"Data Source=projects/{projectId}/instances/{instanceId}/databases/{databaseId}";
List<Performance>performances=newList<Performance>
{
newPerformance{SingerId=1,VenueId=4,EventDate=DateTime.Parse("2017-10-05"),Revenue=11000},
newPerformance{SingerId=1,VenueId=19,EventDate=DateTime.Parse("2017-11-02"),Revenue=15000},
newPerformance{SingerId=2,VenueId=42,EventDate=DateTime.Parse("2017-12-23"),Revenue=7000},
};
// Create connection to Cloud Spanner.
usingvarconnection=newSpannerConnection (connectionString);
awaitconnection.OpenAsync ();
// Insert rows into the Performances table.
varrowCountAarray=awaitTask.WhenAll(performances.Select (performance=>
{
varcmd=connection.CreateInsertCommand ("Performances",newSpannerParameterCollection
{
{"SingerId",SpannerDbType .Int64 ,performance.SingerId},
{"VenueId",SpannerDbType .Int64 ,performance.VenueId},
{"EventDate",SpannerDbType .Date ,performance.EventDate},
{"Revenue",SpannerDbType .Int64 ,performance.Revenue},
{"LastUpdateTime",SpannerDbType .Timestamp ,SpannerParameter .CommitTimestamp },
});
returncmd.ExecuteNonQueryAsync();
}));
returnrowCountAarray.Sum();
}
}

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"
"cloud.google.com/go/spanner"
)
funcwriteWithTimestamp(dbstring)error{
ctx:=context.Background()
client,err:=spanner.NewClient(ctx,db)
iferr!=nil{
returnerr
}
deferclient.Close()
performanceColumns:=[]string{"SingerId","VenueId","EventDate","Revenue","LastUpdateTime"}
m:=[]*spanner.Mutation{
spanner.InsertOrUpdate ("Performances",performanceColumns,[]interface{}{1,4,"2017-10-05",11000,spanner.CommitTimestamp }),
spanner.InsertOrUpdate ("Performances",performanceColumns,[]interface{}{1,19,"2017-11-02",15000,spanner.CommitTimestamp }),
spanner.InsertOrUpdate ("Performances",performanceColumns,[]interface{}{2,42,"2017-12-23",7000,spanner.CommitTimestamp }),
}
_,err=client.Apply (ctx,m)
returnerr
}

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.

staticfinalList<Performance>PERFORMANCES=
Arrays.asList(
newPerformance(1,4,"2017-10-05",11000),
newPerformance(1,19,"2017-11-02",15000),
newPerformance(2,42,"2017-12-23",7000));
staticvoidwriteExampleDataWithTimestamp(DatabaseClientdbClient){
List<Mutation>mutations=newArrayList<>();
for(Performanceperformance:PERFORMANCES){
mutations.add(
Mutation.newInsertBuilder("Performances")
.set("SingerId")
.to(performance.singerId)
.set("VenueId")
.to(performance.venueId)
.set("EventDate")
.to(performance.eventDate)
.set("Revenue")
.to(performance.revenue)
.set("LastUpdateTime")
.to(Value.COMMIT_TIMESTAMP)
.build());
}
dbClient.write(mutations);
}

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);
// Instantiate Spanner table objects
constperformancesTable=database.table('Performances');
constdata=[
{
SingerId:'1',
VenueId:'4',
EventDate:'2017-10-05',
Revenue:'11000',
LastUpdateTime:'spanner.commit_timestamp()',
},
{
SingerId:'1',
VenueId:'19',
EventDate:'2017-11-02',
Revenue:'15000',
LastUpdateTime:'spanner.commit_timestamp()',
},
{
SingerId:'2',
VenueId:'42',
EventDate:'2017-12-23',
Revenue:'7000',
LastUpdateTime:'spanner.commit_timestamp()',
},
];
// Inserts rows into the Singers table
// Note: Cloud Spanner interprets Node.js numbers as FLOAT64s, so
// they must be converted to strings before being inserted as INT64s
try{
awaitperformancesTable.insert(data);
console.log('Inserted data.');
}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;
/**
 * Inserts sample data into a table with a commit timestamp column.
 *
 * The database and table must already exist and can be created using
 * `create_table_with_timestamp_column`.
 * Example:
 * ```
 * insert_data_with_timestamp_column($instanceId, $databaseId);
 * ```
 *
 * @param string $instanceId The Spanner instance ID.
 * @param string $databaseId The Spanner database ID.
 */
function insert_data_with_timestamp_column(string $instanceId, string $databaseId): void
{
 $spanner = new SpannerClient();
 $instance = $spanner->instance($instanceId);
 $database = $instance->database($databaseId);
 $operation = $database->transaction(['singleUse' => true])
 ->insertBatch('Performances', [
 ['SingerId' => 1, 'VenueId' => 4, 'EventDate' => '2017-10-05', 'Revenue' => 11000, 'LastUpdateTime' => $spanner->commitTimestamp()],
 ['SingerId' => 1, 'VenueId' => 19, 'EventDate' => '2017-11-02', 'Revenue' => 15000, 'LastUpdateTime' => $spanner->commitTimestamp()],
 ['SingerId' => 2, 'VenueId' => 42, 'EventDate' => '2017-12-23', 'Revenue' => 7000, 'LastUpdateTime' => $spanner->commitTimestamp()],
 ])
 ->commit();
 print('Inserted data.' . PHP_EOL);
}

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.

definsert_data_with_timestamp(instance_id, database_id):
"""Inserts data with a COMMIT_TIMESTAMP field into a table."""
 spanner_client = spanner.Client()
 instance = spanner_client.instance(instance_id)
 database = instance.database(database_id)
 with database.batch() as batch:
 batch.insert(
 table="Performances",
 columns=("SingerId", "VenueId", "EventDate", "Revenue", "LastUpdateTime"),
 values=[
 (1, 4, "2017-10-05", 11000, spanner.COMMIT_TIMESTAMP),
 (1, 19, "2017-11-02", 15000, spanner.COMMIT_TIMESTAMP),
 (2, 42, "2017-12-23", 7000, spanner.COMMIT_TIMESTAMP),
 ],
 )
 print("Inserted data.")

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
# Get commit_timestamp
commit_timestamp=client.commit_timestamp
client.commitdo|c|
c.insert"Performances",[
{SingerId:1,VenueId:4,EventDate:"2017-10-05",Revenue:11_000,LastUpdateTime:commit_timestamp},
{SingerId:1,VenueId:19,EventDate:"2017-11-02",Revenue:15_000,LastUpdateTime:commit_timestamp},
{SingerId:2,VenueId:42,EventDate:"2017-12-23",Revenue:7000,LastUpdateTime:commit_timestamp}
]
end
puts"Inserted data"

What's next

To search and filter code samples for other Google Cloud products, see the Google Cloud sample browser.

Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 4.0 License, and code samples are licensed under the Apache 2.0 License. For details, see the Google Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.