无法为com.google.cloud.bigtable.hbase1_x.BigtableConnection找到合适的构造函数

时间:2017-08-04 18:28:51

标签: java maven google-app-engine hbase pom.xml

我尝试创建一个简单的类来创建表,并使用HBase和Google应用引擎添加一些列。 我已经在Google Cloud平台中创建了一个项目和一个实例。 我克隆了这个存储库:https://github.com/GoogleCloudPlatform/cloud-bigtable-examples/blob/master/java/hello-world/src/main/java/com/example/cloud/bigtable/helloworld/HelloWorld.java 它就像一个魅力,在我的实例中创建一个表。 但是,当我尝试使用相同的配置创建一个新的maven项目时,它不起作用,我无法创建任何东西。 我遇到了这个问题:

InvocationTargetException: Could not find an appropriate constructor for com.google.cloud.bigtable.hbase1_x.BigtableConnection: com.google.common.util.concurrent.MoreExecutors.platformThreadFactory()Ljava/util/concurrent/ThreadFactory;

这是我的AudioBridgeData.java文件:

package com.xxx.xxx;


import com.google.cloud.bigtable.hbase.BigtableConfiguration;
import org.apache.hadoop.hbase.HColumnDescriptor;
import org.apache.hadoop.hbase.HTableDescriptor;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.Admin;
import org.apache.hadoop.hbase.client.Connection;
import org.apache.hadoop.hbase.client.Get;
import org.apache.hadoop.hbase.client.Put;
import org.apache.hadoop.hbase.client.Result;
import org.apache.hadoop.hbase.client.ResultScanner;
import org.apache.hadoop.hbase.client.Scan;
import org.apache.hadoop.hbase.client.Table;
import org.apache.hadoop.hbase.util.Bytes;

import java.io.IOException;

/**
 * A minimal application that connects to Cloud Bigtable using the native HBase API
 * and performs some basic operations.
 */
public class AudioBridgeData {

  private static final byte[] TABLE_NAME = Bytes.toBytes("audio-bridge");
  private static final byte[] COLUMN_FAMILY_NAME = Bytes.toBytes("phone-number");
  private static final byte[] COLUMN_NAME = Bytes.toBytes("number");
  private static final String[] NUMBERS =
      { "+33697584976", "+19553560976", "+4879665676" };


  /**
   * Connects to Cloud Bigtable, runs some basic operations and prints the results.
   */
  private static void doAudioBridge(String projectId, String instanceId) {

    try (Connection connection = BigtableConfiguration.connect(projectId, instanceId)) {
      Admin admin = connection.getAdmin();
      HTableDescriptor descriptor = new HTableDescriptor(TableName.valueOf(TABLE_NAME));
      descriptor.addFamily(new HColumnDescriptor(COLUMN_FAMILY_NAME));

      print("Create table " + descriptor.getNameAsString());
      admin.createTable(descriptor);
      Table table = connection.getTable(TableName.valueOf(TABLE_NAME));
      print("Write some numbers to the table");
      for (int i = 0; i < NUMBERS.length; i++) {
        Put put = new Put(Bytes.toBytes(i));
        put.addColumn(COLUMN_FAMILY_NAME, COLUMN_NAME, Bytes.toBytes(NUMBERS[i]));
        table.put(put);
      }
      int rowKey = 0;
      Result getResult = table.get(new Get(Bytes.toBytes(rowKey)));
      String number = Bytes.toString(getResult.getValue(COLUMN_FAMILY_NAME, COLUMN_NAME));
      System.out.println("Get a single number by row key");
      System.out.printf("\t%s = %s\n", rowKey, number);
      Scan scan = new Scan();

      print("Scan for all numbers:");
      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));
      }

    } catch (IOException e) {
      System.err.println("Exception while running HelloWorld: " + e.getMessage());
      e.printStackTrace();
      System.exit(1);
    }

    System.exit(0);
  }

  private static void print(String msg) {
    System.out.println("Number: " + msg);
  }

  public static void main(String[] args) {
    String projectId = requiredProperty("bigtable.projectID");
    String instanceId = requiredProperty("bigtable.instanceID");

    doAudioBridge(projectId, instanceId);
  }

  private static String requiredProperty(String prop) {
    String value = System.getProperty(prop);
    if (value == null) {
      throw new IllegalArgumentException("Missing required system property: " + prop);
    }
    return value;
  }

}

这是我的pom.xml文件:

<?xml version="1.0" encoding="UTF-8"?>
<project>
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.xxx</groupId>
    <artifactId>xxx</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>jar</packaging>
    <url>http://maven.apache.org</url>

    <name>xxx</name>

    <properties>
        <bigtable.version>1.0.0-pre1</bigtable.version>
        <hbase.version>1.1.5</hbase.version>
        <maven.compiler.target>1.8</maven.compiler.target>
        <maven.compiler.source>1.8</maven.compiler.source>
    </properties>

    <repositories>
    <repository>
        <id>snapshots-repo</id>
        <url>https://oss.sonatype.org/content/repositories/snapshots</url>
        <releases><enabled>false</enabled></releases>
        <snapshots><enabled>true</enabled></snapshots>
    </repository>
    </repositories>

    <dependencies>
        <dependency>
            <groupId>com.google.cloud.bigtable</groupId>
            <artifactId>bigtable-hbase-1.x</artifactId>
            <version>${bigtable.version}</version>
        </dependency>

        <dependency>
            <groupId>io.netty</groupId>
            <artifactId>netty-tcnative-boringssl-static</artifactId>
            <version>1.1.33.Fork26</version>
        </dependency>

        <dependency>
            <groupId>org.apache.hbase</groupId>
            <artifactId>hbase-client</artifactId>
            <version>0.98.11-hadoop2</version>
        </dependency>

    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>exec-maven-plugin</artifactId>
                <version>1.5.0</version>
                <configuration>
                    <mainClass>com.xxx.xxx.AudioBridgeData</mainClass>
                </configuration>
            </plugin>
        </plugins>
    </build>

</project>

我试图运行这个命令:

mvn exec:java -Dbigtable.projectID=xxx -Dbigtable.instanceID=quickstart-instance

非常感谢你的帮助! :)

1 个答案:

答案 0 :(得分:0)

我有相同的错误消息,奇怪的是它是由丢失的应用程序凭据引起的。如果它是同一个问题,你应该设置这个环境变量:

GOOGLE_APPLICATION_CREDENTIALS

应将其设置为创建服务帐户密钥后您(可能已)下载的客户端凭据文件的位置。这是一个很好的页面:

https://developers.google.com/identity/protocols/application-default-credentials