blob: e02ead3e372ca36d3547e226dc6a3bad9bd60728 [file] [log] [blame]
Copyright 2015 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
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
See the License for the specific language governing permissions and
limitations under the License.
Package bigtable is an API to Google Cloud Bigtable.
See for general product documentation.
See for authentication, timeouts,
connection pooling and similar aspects of this package.
The principal way to read from a Bigtable is to use the ReadRows method on
*Table. A RowRange specifies a contiguous portion of a table. A Filter may be
provided through RowFilter to limit or transform the data that is returned.
tbl := client.Open("mytable")
// Read all the rows starting with "", but only fetch the columns
// in the "links" family.
rr := bigtable.PrefixRange("")
err := tbl.ReadRows(ctx, rr, func(r Row) bool {
// TODO: do something with r.
return true // Keep going.
}, bigtable.RowFilter(bigtable.FamilyFilter("links")))
if err != nil {
// TODO: handle err.
To read a single row, use the ReadRow helper method:
r, err := tbl.ReadRow(ctx, "") // "" is the entire row key
if err != nil {
// TODO: handle err.
// TODO: use r.
This API exposes two distinct forms of writing to a Bigtable: a Mutation and a
ReadModifyWrite. The former expresses idempotent operations. The latter
expresses non-idempotent operations and returns the new values of updated cells.
These operations are performed by creating a Mutation or ReadModifyWrite (with
NewMutation or NewReadModifyWrite), building up one or more operations on that,
and then using the Apply or ApplyReadModifyWrite methods on a Table.
For instance, to set a couple of cells in a table:
tbl := client.Open("mytable")
mut := bigtable.NewMutation()
// To use numeric values that will later be incremented,
// they need to be big-endian encoded as 64-bit integers.
buf := new(bytes.Buffer)
initialLinkCount := 1 // The initial number of links.
if err := binary.Write(buf, binary.BigEndian, initialLinkCount); err != nil {
// TODO: handle err.
mut.Set("links", "", bigtable.Now(), buf.Bytes())
mut.Set("links", "", bigtable.Now(), buf.Bytes())
err := tbl.Apply(ctx, "", mut)
if err != nil {
// TODO: handle err.
To increment an encoded value in one cell:
tbl := client.Open("mytable")
rmw := bigtable.NewReadModifyWrite()
rmw.Increment("links", "", 12) // add 12 to the cell in column ""
r, err := tbl.ApplyReadModifyWrite(ctx, "", rmw)
if err != nil {
// TODO: handle err.
// TODO: use r.
If a read or write operation encounters a transient error it will be retried
until a successful response, an unretryable error or the context deadline is
reached. Non-idempotent writes (where the timestamp is set to ServerTime) will
not be retried. In the case of ReadRows, retried calls will not re-scan rows
that have already been processed.
package bigtable // import ""
import ""
// Scope constants for authentication credentials. These should be used when
// using credential creation functions such as oauth.NewServiceAccountFromFile.
const (
// Scope is the OAuth scope for Cloud Bigtable data operations.
Scope = ""
// ReadonlyScope is the OAuth scope for Cloud Bigtable read-only data
// operations.
ReadonlyScope = ""
// AdminScope is the OAuth scope for Cloud Bigtable table admin operations.
AdminScope = ""
// InstanceAdminScope is the OAuth scope for Cloud Bigtable instance (and
// cluster) admin operations.
InstanceAdminScope = ""
// clientUserAgent identifies the version of this package.
// It should be the same as
var clientUserAgent = "cbt-go/v" + internal.Version
// resourcePrefixHeader is the name of the metadata header used to indicate
// the resource being operated on.
const resourcePrefixHeader = "google-cloud-resource-prefix"
// requestParamsHeader is the name of the metadata header used for routing
// requests based on resources accessed.
const requestParamsHeader = "x-goog-request-params"