How to connect to a running bigtable emulator from java
Asked Answered
I

2

6

I am trying to use the bigtable emulator from gcloud beta emulators. I launch the emulator, grab the hostname (localhost) and port (in this instance 8885)

gcloud beta emulators bigtable start

Executing: /usr/local/Caskroom/google-cloud-sdk/latest/google-cloud-sdk/platform/bigtable-emulator/cbtemulator --host=localhost --port=8885

I am trying to connect to the emulator from a java test client, here is what I provide:

Configuration conf = BigtableConfiguration.configure(projectId, instanceId);

if(!Strings.isNullOrEmpty(host)){
    conf.set(BigtableOptionsFactory.BIGTABLE_HOST_KEY, host);
    conf.set(BigtableOptionsFactory.BIGTABLE_PORT_KEY, Integer.toString(port));
}
connection = BigtableConfiguration.connect(configuration);
try (Table table = connection.getTable("tName")){
    table.put(<Put instance>);
} 

When I execute the test code I get:

16:36:37.369 [bigtable-batch-pool-1] INFO com.google.cloud.bigtable.grpc.async.AbstractRetryingRpcListener - Retrying failed call. Failure #1, got: Status{code=UNAVAILABLE, description=null, cause=java.net.ConnectException: Connection refused: localhost/0:0:0:0:0:0:0:1:8885}
java.net.ConnectException: Connection refused: localhost/0:0:0:0:0:0:0:1:8885

I am using the library: com.google.cloud.bigtable:bigtable-hbase-1.2:0.9.1

Any idea of what I am doing wrong ?

Thanks !

Isotropic answered 25/7, 2016 at 20:48 Comment(5)
The way you've shown is not the way this page says to start the emulator.Dachi
I am not trying to launch it from java, I am trying to connect to an already launched emulator from javaIsotropic
@JonnyHenly this is a log output while running gcloud beta emulators bigtable startIsotropic
This may be a trivial question, but ... are you running the emulator and client code on the same machine?Est
@JonnyHenly yes I am :DIsotropic
O
4

You need one additional config property to be set:

conf.set(BigtableOptionsFactory.BIGTABLE_USE_PLAINTEXT_NEGOTIATION, true);

Also, from the log message it looks like it's trying to connect to an IPv6 address, which I don't think will work. Double-check that host is a valid IPv4 address.

The java client will make this easier to do in the near future.

Oldwife answered 26/7, 2016 at 19:55 Comment(2)
Thanks ! the plaintext negotiation did the trick ! the IP was IPv4, I guess the logger logs both v4 and v6 addressesIsotropic
This just got easier! The java client now supports the emulator directly with the proper environment variable set: cloud.google.com/bigtable/docs/emulatorOldwife
N
2

Now you can set
configuration.set(BigtableOptionsFactory.BIGTABLE_EMULATOR_HOST_KEY,<HOST:PORT>); to connect to an emulator.

Also "https://github.com/googleapis/java-bigtable/tree/master/google-cloud-bigtable-emulator" can be used to start emulators programmatically for tests etc.

Nett answered 25/5, 2020 at 15:29 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.