Download as pdf or txt
Download as pdf or txt
You are on page 1of 4

Write examples

cloud.google.com/bigtable/docs/writing-data

The following code samples illustrate the various types of write requests that you can send
to Cloud Bigtable when you use the Bigtable client libraries.

Before you try these samples, make sure you understand when and when not to use each
type of write request.

Perform a simple write


The following code samples demonstrate how to make simple write requests to Bigtable.
This type of write makes a MutateRow API request.

import (
"context"
"encoding/binary"
"fmt"
"io"
"bytes"
"cloud.google.com/go/bigtable"
)func writeSimple(w io.Writer, projectID, instanceID string, tableName string)
error {
// projectID := "my-project-id"
// instanceID := "my-instance-id"
// tableName := "mobile-time-series" ctx := context.Background()
client, err := bigtable.NewClient(ctx, projectID, instanceID)
if err != nil {
return fmt.Errorf("bigtable.NewClient: %v", err)
}
defer client.Close()
tbl := client.Open(tableName)
columnFamilyName := "stats_summary"
timestamp := bigtable.Now() mut := bigtable.NewMutation()
buf := new(bytes.Buffer)
binary.Write(buf, binary.BigEndian, int64(1))
mut.Set(columnFamilyName, "connected_cell", timestamp, buf.Bytes())
mut.Set(columnFamilyName, "connected_wifi", timestamp, buf.Bytes())
mut.Set(columnFamilyName, "os_build", timestamp,
[]byte("PQ2A.190405.003")) rowKey := "phone#4c410523#20190501"
if err := tbl.Apply(ctx, rowKey, mut); err != nil {
return fmt.Errorf("Apply: %v", err)
} fmt.Fprintf(w, "Successfully wrote row: %s\n", rowKey)
return nil
}

Increment an existing value


The following code samples demonstrate how to send a write request that increments an
existing numeric value. This type of write makes a ReadModifyWriteRow API request.

GoHBaseJavaPythonC#C++Node.jsPHPRuby

1/4
bigtable/writes/write_increment.go
View on GitHub

import (
"context"
"fmt"
"io"
"cloud.google.com/go/bigtable"
)func writeIncrement(w io.Writer, projectID, instanceID string, tableName string)
error {
// projectID := "my-project-id"
// instanceID := "my-instance-id"
// tableName := "mobile-time-series" ctx := context.Background()
client, err := bigtable.NewClient(ctx, projectID, instanceID)
if err != nil {
return fmt.Errorf("bigtable.NewAdminClient: %v", err)
}
defer client.Close()
tbl := client.Open(tableName)
columnFamilyName := "stats_summary" increment :=
bigtable.NewReadModifyWrite()
increment.Increment(columnFamilyName, "connected_wifi", -1) rowKey
:= "phone#4c410523#20190501"
if _, err := tbl.ApplyReadModifyWrite(ctx, rowKey, increment); err != nil
{
return fmt.Errorf("ApplyReadModifyWrite: %v", err)
} fmt.Fprintf(w, "Successfully updated row: %s\n", rowKey)
return nil
}

Conditionally write a value


The following code samples demonstrate how to send a conditional write request, which
checks a row for a condition and then, depending on the result, writes data to that row.
This type of write makes a CheckAndMutateRow API request.

GoHBaseJavaPythonC#C++Node.jsPHPRuby
bigtable/writes/write_conditionally.go
View on GitHub

2/4
import (
"context"
"fmt"
"io"
"cloud.google.com/go/bigtable"
)func writeConditionally(w io.Writer, projectID, instanceID string, tableName
string) error {
// projectID := "my-project-id"
// instanceID := "my-instance-id"
// tableName := "mobile-time-series" ctx := context.Background()
client, err := bigtable.NewClient(ctx, projectID, instanceID)
if err != nil {
return fmt.Errorf("bigtable.NewAdminClient: %v", err)
}
defer client.Close()
tbl := client.Open(tableName)
columnFamilyName := "stats_summary"
timestamp := bigtable.Now() mut := bigtable.NewMutation()
mut.Set(columnFamilyName, "os_name", timestamp, []byte("android"))
filter := bigtable.ChainFilters(
bigtable.FamilyFilter(columnFamilyName),
bigtable.ColumnFilter("os_build"),
bigtable.ValueFilter("PQ2A\\..*"))
conditionalMutation := bigtable.NewCondMutation(filter, mut, nil)
rowKey := "phone#4c410523#20190501"
if err := tbl.Apply(ctx, rowKey, conditionalMutation); err != nil {
return fmt.Errorf("Apply: %v", err)
} fmt.Fprintln(w, "Successfully updated row's os_name")
return nil
}

Perform batch writes


The following code samples demonstrate how to make batch write requests to Bigtable.
This type of write makes a MutateRows API request.

3/4
import (
"bytes"
"context"
"encoding/binary"
"fmt"
"io"
"cloud.google.com/go/bigtable"
)func writeBatch(w io.Writer, projectID, instanceID string, tableName string)
error {
// projectID := "my-project-id"
// instanceID := "my-instance-id"
// tableName := "mobile-time-series" ctx := context.Background()
client, err := bigtable.NewClient(ctx, projectID, instanceID)
if err != nil {
return fmt.Errorf("bigtable.NewAdminClient: %v", err)
}
defer client.Close()
tbl := client.Open(tableName)
columnFamilyName := "stats_summary"
timestamp := bigtable.Now()
var muts []*bigtable.Mutation binary1 := new(bytes.Buffer)
binary.Write(binary1, binary.BigEndian, int64(1)) mut :=
bigtable.NewMutation()
mut.Set(columnFamilyName, "connected_wifi", timestamp, binary1.Bytes())
mut.Set(columnFamilyName, "os_build", timestamp, []byte("12155.0.0-rc1"))
muts = append(muts, mut) mut = bigtable.NewMutation()
mut.Set(columnFamilyName, "connected_wifi", timestamp, binary1.Bytes())
mut.Set(columnFamilyName, "os_build", timestamp, []byte("12145.0.0-rc6"))
muts = append(muts, mut) rowKeys :=
[]string{"tablet#a0b81f74#20190501", "tablet#a0b81f74#20190502"}
if _, err := tbl.ApplyBulk(ctx, rowKeys, muts); err != nil {
return fmt.Errorf("ApplyBulk: %v", err)
} fmt.Fprintf(w, "Successfully wrote 2 rows: %s\n", rowKeys)
return nil
}

What's next
Was this helpful?

4/4

You might also like