Could not find an appropriate constructor for com.

2019-08-01 05:30发布

I tried to create a simple class to create a table and add some columns with HBase and Google app engine. I have already created a project and an instance in Google Cloud platform. I cloned this repository : https://github.com/GoogleCloudPlatform/cloud-bigtable-examples/blob/master/java/hello-world/src/main/java/com/example/cloud/bigtable/helloworld/HelloWorld.java It works like a charm to create a table in my instance. But when I'm trying to create a new maven project with the same config, it doesn't work, I can't create anything. I got this issue :

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;

here is my AudioBridgeData.java file :

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;
  }

}

Here is my pom.xml file :

<?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>

And I tried to run this command :

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

Thanks a lot for your help ! :)

1条回答
混吃等死
2楼-- · 2019-08-01 06:16

I had the same error message, weirdly it was caused by missing application credentials. If its the same issue you should set this environment variable:

GOOGLE_APPLICATION_CREDENTIALS

It should be set to the location of the client credentials file you (might have) downloaded after creating a service account key. This is a good page:

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

查看更多
登录 后发表回答