您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符
 
 
 

84 行
1.9 KiB

  1. /*
  2. Copyright 2016 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. package bttest_test
  14. import (
  15. "context"
  16. "fmt"
  17. "log"
  18. "cloud.google.com/go/bigtable"
  19. "cloud.google.com/go/bigtable/bttest"
  20. "google.golang.org/api/option"
  21. "google.golang.org/grpc"
  22. )
  23. func ExampleNewServer() {
  24. srv, err := bttest.NewServer("localhost:0")
  25. if err != nil {
  26. log.Fatalln(err)
  27. }
  28. ctx := context.Background()
  29. conn, err := grpc.Dial(srv.Addr, grpc.WithInsecure())
  30. if err != nil {
  31. log.Fatalln(err)
  32. }
  33. proj, instance := "proj", "instance"
  34. adminClient, err := bigtable.NewAdminClient(ctx, proj, instance, option.WithGRPCConn(conn))
  35. if err != nil {
  36. log.Fatalln(err)
  37. }
  38. if err = adminClient.CreateTable(ctx, "example"); err != nil {
  39. log.Fatalln(err)
  40. }
  41. if err = adminClient.CreateColumnFamily(ctx, "example", "links"); err != nil {
  42. log.Fatalln(err)
  43. }
  44. client, err := bigtable.NewClient(ctx, proj, instance, option.WithGRPCConn(conn))
  45. if err != nil {
  46. log.Fatalln(err)
  47. }
  48. tbl := client.Open("example")
  49. mut := bigtable.NewMutation()
  50. mut.Set("links", "golang.org", bigtable.Now(), []byte("Gophers!"))
  51. if err = tbl.Apply(ctx, "com.google.cloud", mut); err != nil {
  52. log.Fatalln(err)
  53. }
  54. if row, err := tbl.ReadRow(ctx, "com.google.cloud"); err != nil {
  55. log.Fatalln(err)
  56. } else {
  57. for _, column := range row["links"] {
  58. fmt.Println(column.Column)
  59. fmt.Println(string(column.Value))
  60. }
  61. }
  62. // Output:
  63. // links:golang.org
  64. // Gophers!
  65. }