如何在谷歌云平台之外连接谷歌bigtable

时间:2015-06-03 07:57:19

标签: bigtable google-cloud-bigtable

我创建了一个Google Bigtable群集。 我正在尝试连接this tutorial 我试图下载他们的cli项目并构建它,但是当我尝试执行任何命令时它失败了。我还尝试构建更简单的连接器示例。这是:

import org.apache.hadoop.hbase.client.Admin;
import org.apache.hadoop.hbase.client.Connection;
import org.apache.hadoop.hbase.client.ConnectionFactory;
import org.apache.log4j.Logger;

import java.io.IOException;
import java.util.ArrayList;

public class BigTableTestConnector {
    private final static Logger logger = Logger.getLogger(BigTableTestConnector.class);


public void testConnection() throws MasterNotRunningException, ZooKeeperConnectionException {

    try {
        Connection connection  = ConnectionFactory.createConnection();

        String tableName = "testTable";
        ArrayList<String> columnFamilies = new ArrayList<String>();
        columnFamilies.add("columnFamily1");
        columnFamilies.add("columnFamily2");
        columnFamilies.add("justString");
        columnFamilies.add("uhhaha");

        Admin admin = connection.getAdmin();
        HTableDescriptor tableDescriptor = new HTableDescriptor(tableName);
        for (String colFamily : columnFamilies) {
            tableDescriptor.addFamily(new HColumnDescriptor(colFamily));
        }
        admin.createTable(tableDescriptor);

    } catch (IOException e) {
        e.printStackTrace();
    }


    logger.info("done");
}

public static void main(String... args) throws Exception {
        BigTableTestConnector bttc = new BigTableTestConnector();
        bttc.testConnection();
    }
}

但是当我在本地计算机上运行它时,我得到以下异常

java.io.IOException: java.lang.reflect.InvocationTargetException
at org.apache.hadoop.hbase.client.ConnectionFactory.createConnection(ConnectionFactory.java:240)
at org.apache.hadoop.hbase.client.ConnectionFactory.createConnection(ConnectionFactory.java:218)
at org.apache.hadoop.hbase.client.ConnectionFactory.createConnection(ConnectionFactory.java:90)
at com.zoomdata.thrift.provider.BigTableTestConnector.testConnection(BigTableTestConnector.java:33)
at com.zoomdata.thrift.provider.BigTableTestConnector.main(BigTableTestConnector.java:81)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:134)
Caused by: java.lang.reflect.InvocationTargetException
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:422)
at org.apache.hadoop.hbase.client.ConnectionFactory.createConnection(ConnectionFactory.java:238)
... 9 more
Caused by: java.io.IOException: Error getting access token from metadata server at: http://metadata/computeMetadata/v1/instance/service-accounts/default/token
at com.google.cloud.bigtable.hbase.CredentialFactory.getCredentialFromMetadataServiceAccount(CredentialFactory.java:100)
at com.google.cloud.bigtable.hbase.BigtableOptionsFactory.fromConfiguration(BigtableOptionsFactory.java:236)
at org.apache.hadoop.hbase.client.BigtableConnection.<init>(BigtableConnection.java:120)
... 14 more
Caused by: java.io.IOException: ComputeEngineCredentials cannot find the metadata server. This is likely because code is not running on Google Compute Engine.
at com.google.auth.oauth2.ComputeEngineCredentials.refreshAccessToken(ComputeEngineCredentials.java:63)
at com.google.auth.oauth2.OAuth2Credentials.refresh(OAuth2Credentials.java:76)
at com.google.cloud.bigtable.hbase.CredentialFactory.getCredentialFromMetadataServiceAccount(CredentialFactory.java:98)
... 16 more
Caused by: java.net.UnknownHostException: metadata
at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:184)
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
at java.net.Socket.connect(Socket.java:589)
at sun.net.NetworkClient.doConnect(NetworkClient.java:175)
at sun.net.www.http.HttpClient.openServer(HttpClient.java:432)
at sun.net.www.http.HttpClient.openServer(HttpClient.java:527)
at sun.net.www.http.HttpClient.<init>(HttpClient.java:211)
at sun.net.www.http.HttpClient.New(HttpClient.java:308)
at sun.net.www.http.HttpClient.New(HttpClient.java:326)
at sun.net.www.protocol.http.HttpURLConnection.getNewHttpClient(HttpURLConnection.java:1168)
at sun.net.www.protocol.http.HttpURLConnection.plainConnect0(HttpURLConnection.java:1104)
at sun.net.www.protocol.http.HttpURLConnection.plainConnect(HttpURLConnection.java:998)
at sun.net.www.protocol.http.HttpURLConnection.connect(HttpURLConnection.java:932)
at com.google.bigtable.repackaged.com.google.api.client.http.javanet.NetHttpRequest.execute(NetHttpRequest.java:93)
at com.google.bigtable.repackaged.com.google.api.client.http.HttpRequest.execute(HttpRequest.java:965)
at com.google.auth.oauth2.ComputeEngineCredentials.refreshAccessToken(ComputeEngineCredentials.java:61)
... 18 more

它说:

  

ComputeEngineCredentials无法找到元数据服务器。这可能是因为代码未在Google Compute Engine上运行。

这是否意味着此代码只能在Compute Engine中运行? 有没有办法从我的本地计算机连接到它?有没有人有这方面的例子?

1 个答案:

答案 0 :(得分:3)

要在本地运行,您需要确保将环境变量GOOGLE_APPLICATION_CREDENTIALS设置为您从云控制台获得的key.json。这可能是您的原因。