You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

124 lines
5.2 KiB

  1. /*
  2. Copyright 2015 Google LLC
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. /*
  14. Package bigtable is an API to Google Cloud Bigtable.
  15. See https://cloud.google.com/bigtable/docs/ for general product documentation.
  16. See https://godoc.org/cloud.google.com/go for authentication, timeouts,
  17. connection pooling and similar aspects of this package.
  18. Setup and Credentials
  19. Use NewClient or NewAdminClient to create a client that can be used to access
  20. the data or admin APIs respectively. Both require credentials that have permission
  21. to access the Cloud Bigtable API.
  22. If your program is run on Google App Engine or Google Compute Engine, using the Application Default Credentials
  23. (https://developers.google.com/accounts/docs/application-default-credentials)
  24. is the simplest option. Those credentials will be used by default when NewClient or NewAdminClient are called.
  25. To use alternate credentials, pass them to NewClient or NewAdminClient using option.WithTokenSource.
  26. For instance, you can use service account credentials by visiting
  27. https://cloud.google.com/console/project/_/apiui/credential,
  28. creating a new OAuth "Client ID", storing the JSON key somewhere accessible, and writing
  29. jsonKey, err := ioutil.ReadFile(pathToKeyFile)
  30. ...
  31. config, err := google.JWTConfigFromJSON(jsonKey, bigtable.Scope) // or bigtable.AdminScope, etc.
  32. ...
  33. client, err := bigtable.NewClient(ctx, project, instance, option.WithTokenSource(config.TokenSource(ctx)))
  34. ...
  35. Here, `google` means the golang.org/x/oauth2/google package
  36. and `option` means the google.golang.org/api/option package.
  37. Reading
  38. The principal way to read from a Bigtable is to use the ReadRows method on *Table.
  39. A RowRange specifies a contiguous portion of a table. A Filter may be provided through
  40. RowFilter to limit or transform the data that is returned.
  41. tbl := client.Open("mytable")
  42. ...
  43. // Read all the rows starting with "com.google.",
  44. // but only fetch the columns in the "links" family.
  45. rr := bigtable.PrefixRange("com.google.")
  46. err := tbl.ReadRows(ctx, rr, func(r Row) bool {
  47. // do something with r
  48. return true // keep going
  49. }, bigtable.RowFilter(bigtable.FamilyFilter("links")))
  50. ...
  51. To read a single row, use the ReadRow helper method.
  52. r, err := tbl.ReadRow(ctx, "com.google.cloud") // "com.google.cloud" is the entire row key
  53. ...
  54. Writing
  55. This API exposes two distinct forms of writing to a Bigtable: a Mutation and a ReadModifyWrite.
  56. The former expresses idempotent operations.
  57. The latter expresses non-idempotent operations and returns the new values of updated cells.
  58. These operations are performed by creating a Mutation or ReadModifyWrite (with NewMutation or NewReadModifyWrite),
  59. building up one or more operations on that, and then using the Apply or ApplyReadModifyWrite
  60. methods on a Table.
  61. For instance, to set a couple of cells in a table,
  62. tbl := client.Open("mytable")
  63. mut := bigtable.NewMutation()
  64. mut.Set("links", "maps.google.com", bigtable.Now(), []byte("1"))
  65. mut.Set("links", "golang.org", bigtable.Now(), []byte("1"))
  66. err := tbl.Apply(ctx, "com.google.cloud", mut)
  67. ...
  68. To increment an encoded value in one cell,
  69. tbl := client.Open("mytable")
  70. rmw := bigtable.NewReadModifyWrite()
  71. rmw.Increment("links", "golang.org", 12) // add 12 to the cell in column "links:golang.org"
  72. r, err := tbl.ApplyReadModifyWrite(ctx, "com.google.cloud", rmw)
  73. ...
  74. Retries
  75. If a read or write operation encounters a transient error it will be retried until a successful
  76. response, an unretryable error or the context deadline is reached. Non-idempotent writes (where
  77. the timestamp is set to ServerTime) will not be retried. In the case of ReadRows, retried calls
  78. will not re-scan rows that have already been processed.
  79. */
  80. package bigtable // import "cloud.google.com/go/bigtable"
  81. // Scope constants for authentication credentials.
  82. // These should be used when using credential creation functions such as oauth.NewServiceAccountFromFile.
  83. const (
  84. // Scope is the OAuth scope for Cloud Bigtable data operations.
  85. Scope = "https://www.googleapis.com/auth/bigtable.data"
  86. // ReadonlyScope is the OAuth scope for Cloud Bigtable read-only data operations.
  87. ReadonlyScope = "https://www.googleapis.com/auth/bigtable.readonly"
  88. // AdminScope is the OAuth scope for Cloud Bigtable table admin operations.
  89. AdminScope = "https://www.googleapis.com/auth/bigtable.admin.table"
  90. // InstanceAdminScope is the OAuth scope for Cloud Bigtable instance (and cluster) admin operations.
  91. InstanceAdminScope = "https://www.googleapis.com/auth/bigtable.admin.cluster"
  92. )
  93. // clientUserAgent identifies the version of this package.
  94. // It should be bumped upon significant changes only.
  95. const clientUserAgent = "cbt-go/20180601"
  96. // resourcePrefixHeader is the name of the metadata header used to indicate
  97. // the resource being operated on.
  98. const resourcePrefixHeader = "google-cloud-resource-prefix"