【发布时间】:2021-01-08 04:34:48
【问题描述】:
最近,我正在尝试在 IntelliJ IDEA 工具上使用 Bigtable 模拟器和 java(Spring Boot) 开发一些东西。
我做了什么:
-
“cbt”在我的 Mac 上运行的 Bigtable 模拟器正常工作,例如 this。
我已经检查过运行 Bigtable 模拟器不需要真正的 gcloud 凭据。
- 我在 IEDA 上编写了一个单元测试,例如 this 工作正常。 我在这样的设置中添加了环境变量:
我。连接初始化:
Configuration conf;
Connection connection = null;
conf = BigtableConfiguration.configure("fake-project", "fake-instance");
String host = "localhost";
String port = "8086";
二。常量数据将写入表中。
final byte[] TABLE_NAME = Bytes.toBytes("Hello-Bigtable");
final byte[] COLUMN_FAMILY_NAME = Bytes.toBytes("cf1");
final byte[] COLUMN_NAME = Bytes.toBytes("greeting");
final String[] GREETINGS = {
"Hello World!", "Hello Cloud Bigtable!", "Hello!!"
};
三。正在连接:(复制到 I.Connect init。)
Configuration conf;
Connection connection = null;
conf = BigtableConfiguration.configure("fake-project", "fake-instance");
String host = "localhost";
String port = "8086";
三。连接:(已编辑)
if(!Strings.isNullOrEmpty(host)){
conf.set(BigtableOptionsFactory.BIGTABLE_HOST_KEY, host);
conf.set(BigtableOptionsFactory.BIGTABLE_PORT_KEY,port);
conf.set(BigtableOptionsFactory.BIGTABLE_USE_PLAINTEXT_NEGOTIATION, "true");
}
connection = BigtableConfiguration.connect(conf);
四。写入和读取数据:
Admin admin = connection.getAdmin();
Table table = connection.getTable(TableName.valueOf(TABLE_NAME));
if(!admin.tableExists(TableName.valueOf(TABLE_NAME))){
HTableDescriptor descriptor = new HTableDescriptor(TableName.valueOf(TABLE_NAME));
descriptor.addFamily(new HColumnDescriptor(COLUMN_FAMILY_NAME));
System.out.print("Create table " + descriptor.getNameAsString());
admin.createTable(descriptor);
}
for (int i = 0; i < GREETINGS.length; i++) {
String rowKey = "greeting" + i;
Put put = new Put(Bytes.toBytes(rowKey));
put.addColumn(COLUMN_FAMILY_NAME, COLUMN_NAME, Bytes.toBytes(GREETINGS[i]));
table.put(put);
}
Scan scan = new Scan();
ResultScanner scanner = table.getScanner(scan);
for (Result row : scanner) {
byte[] valueBytes = row.getValue(COLUMN_FAMILY_NAME, COLUMN_NAME);
System.out.println('\t' + Bytes.toString(valueBytes));
}
V。输出
Hello World!
Hello Cloud Bigtable!
Hello!!
在我将此代码添加到我的项目后,问题就来了。 当我使用“调试” 运行代码时。
我得到这样的东西 当它试图连接大表时: 似乎它不能基于我创建的配置创建新实例。
最终,它向我显示了一个错误,例如
Could not find an appropriate constructor for com.google.cloud.bigtable.hbase1_x.BigtableConnection
附:我尝试使用运行 IntelliJ IDEA 的命令。我这样做的原因是因为我在使用单元测试时缺少环境变量。 在我的 .zshrc 中:
我的 CMD 工具是带有 oh-myzsh 的 iTerm2。
一切都有帮助!!!!
非常感谢。
【问题讨论】: