【问题标题】:Bigtable emulator. Not find an appropriate constructor大表模拟器。找不到合适的构造函数
【发布时间】:2021-01-08 04:34:48
【问题描述】:

最近,我正在尝试在 IntelliJ IDEA 工具上使用 Bigtable 模拟器和 java(Spring Boot) 开发一些东西。

我做了什么:

  1. Bigtable 模拟器在我的电脑 (MacOs 10.15.6) 上运行良好。

  2. “cbt”在我的 Mac 上运行的 Bigtable 模拟器正常工作,例如 this

我已经检查过运行 Bigtable 模拟器不需要真正的 gcloud 凭据。

  1. 我在 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。

一切都有帮助!!!!

非常感谢。

【问题讨论】:

    标签: java gcloud bigtable


    【解决方案1】:

    您似乎错过了BigtableConnection的构造函数:BigtableConnection(org.apache.hadoop.conf.Configuration conf)

    我建议您按照Google Documentation 中提到的步骤尝试创建一个Connection 对象

    private static Connection connection = null;
       
    public static void connect() throws IOException {
        Configuration config = BigtableConfiguration.configure(PROJECT_ID, INSTANCE_ID);
        // Include the following line if you are using app profiles.
        // If you do not include the following line, the connection uses the
        // default app profile.
        config.set(BigtableOptionsFactory.APP_PROFILE_ID_KEY, APP_PROFILE_ID);
        connection = BigtableConfiguration.connect(config);
    }
    

    【讨论】:

    • 感谢您的回答!我发现我的问题中有重复的代码。你提到的步骤我已经在我的代码中完成了@@
    • 我已经编辑了我的问题,将重复的代码标记为已删除。
    • 可能config.set()上的某些参数没有正确传递。您能否评论if 语句并按照上述文档中建议的方式传递参数?
    猜你喜欢
    • 2022-01-11
    • 2016-03-24
    • 2018-01-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多