如何从GoLang应用程序连接到Bigtable Emulator?如何使用它?

时间:2018-06-17 15:06:43

标签: go bigtable google-cloud-bigtable

我正在尝试使用BigTable Emulator。我以前从未使用过它。我按照documentation但无法理解,

  1. 如何将应用程序连接到模拟器。
  2. 如何设置BIGTABLE_EMULATOR_HOST环境变量。
  3. 请通过解释或指向任何示例或链接来帮助您。 谢谢。

1 个答案:

答案 0 :(得分:7)

The BIGTABLE_EMULATOR_HOST environment variable overrides the normal connection logic。设置它会导致客户端连接到模拟器而代码中不需要任何特殊更改(即传递给NewAdminClient() / NewClient()的项目/实例值将被忽略)。

step 2 of the Using the emulator instructions中提供的命令为您设置环境变量。

有关与BigTable实例交互的简单具体示例,请参阅GoogleCloudPlatform/golang-samples/bigtable/helloworld/main.go的实现。

操作实例

为了演示使用Bigtable Emulator,我使用free tier, f1-micro Compute Engine instance。这是我以前没用过的一个例子,所以它基本上是一个干净的名单。

首先,我设置了go环境:

$ sudo apt install golang
<... SNIP apt output ...>
$ mkdir ~/go
$ export GOPATH=$HOME/go

然后我更新了google-cloud-sdk包并安装了google-cloud-sdk-bigtable-emulator包:

$ sudo apt install google-cloud-sdk
<... SNIP apt output ...>
$ sudo apt install google-cloud-sdk-bigtable-emulator
<... SNIP apt output ...>

此时仿真器已准备好运行,因此我使用记录的命令启动它:

$ gcloud beta emulators bigtable start
Executing: /usr/lib/google-cloud-sdk/platform/bigtable-emulator/cbtemulator --host=localhost --port=8086
[bigtable] Cloud Bigtable emulator running on 127.0.0.1:8086

启动后,它在前台运行,所以我独自离开了那个终端并开始了一个新终端。

在新终端中,首先要设置BIGTABLE_EMULATOR_HOST环境变量。此步骤记录在仿真器指令中。以下命令生成用于设置变量的shell命令。要查看它生成它的命令,请直接运行它:

$ gcloud beta emulators bigtable env-init
export BIGTABLE_EMULATOR_HOST=localhost:8086

运行命令$(...)中包含的命令告诉shell执行命令的输出,从而设置变量:

$ $(gcloud beta emulators bigtable env-init)

现在模拟器正在运行且环境已准备就绪,我需要一个客户端应用程序来连接它。我选择使用GoogleCloudPlatform/golang-samples repository中的helloworld应用程序。

$ git clone https://github.com/GoogleCloudPlatform/golang-samples.git
$ cd golang-samples/bigtable/helloworld

在上述目录的main.go文件中,导入了cloud.google.com/go/bigtablegolang.org/x/net/context个包,因此我运行go get来获取它们。

$ go get cloud.google.com/go/bigtable
$ go get golang.org/x/net/context

然后我运行示例应用程序,使用-project-instance标志的虚拟值,因为它将连接到本地运行的模拟器。

$ go run main.go -project foo -instance bar
2018/06/18 00:39:27 Creating table Hello-Bigtable
2018/06/18 00:39:27 Writing greeting rows to table
2018/06/18 00:39:27 Getting a single greeting by row key:
2018/06/18 00:39:27     greeting0 = Hello World!
2018/06/18 00:39:27 Reading all greeting rows:
2018/06/18 00:39:27     greeting0 = Hello World!
2018/06/18 00:39:27     greeting1 = Hello Cloud Bigtable!
2018/06/18 00:39:27     greeting2 = Hello golang!
2018/06/18 00:39:27 Deleting the table

仿真器终端中没有任何事情表明它正在被使用。因此,为了证明它实际上与模拟器交互,我停止了模拟器进程(在其终端中按Ctrl-C)并尝试再次执行客户端应用程序:

$ go run main.go -project foo -instance bar
2018/06/18 00:40:03 Retryable error: rpc error: code = Unavailable desc = all SubConns are in TransientFailure, latest connection error: connection error: desc = "transport: Error while dialing dial tcp [::1]:8086: getsockopt: connection refused", retrying in 47.77941ms
2018/06/18 00:40:03 Retryable error: rpc error: code = Unavailable desc = all SubConns are in TransientFailure, latest connection error: connection error: desc = "transport: Error while dialing dial tcp [::1]:8086: getsockopt: connection refused", retrying in 2.153551ms
2018/06/18 00:40:03 Retryable error: rpc error: code = Unavailable desc = all SubConns are in TransientFailure, latest connection error: connection error: desc = "transport: Error while dialing dial tcp [::1]:8086: getsockopt: connection refused", retrying in 114.145821ms
<... SNIP repeated errors ...>
^Csignal: interrupt
$

因此,使用未修改的客户端应用程序只需设置适当的环境变量即可与模拟器进行交互。

相关问题