2015-06-03 200 views
3

我有一個谷歌Bigtable集羣創建。 我試圖連接下列this tutorial 我試圖下載他們的cli項目並構建它,但是當我嘗試執行任何命令時失敗。此外,我試圖建立我的簡單的連接器的例子。那就是:如何連接谷歌雲平臺以外的谷歌bigtable

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上運行。

那麼這是否意味着這個代碼可以計算引擎內只運行? 有沒有辦法從我的本地計算機連接到它?有沒有人有這樣的例子?

回答

3

對於本地運行,您需要確保將環境變量GOOGLE_APPLICATION_CREDENTIALS設置爲您從雲控制檯獲得的key.json。這是你的情況可能的原因。

+0

謝謝。你幫我。我通過導出在控制檯中設置了這個變量,但是從我的IDE中看不到。 – user2281439

+0

@Les Vogel - Google DevRel除非我弄錯了,否則沒有辦法將'key.json'的範圍限制爲bigtable。是對的嗎? (我希望你能得到這個回覆,它不會鏈接到你,所以我不確定是否可以識別標籤)。 –

+0

還不快,很快。 Cloud IAM不久將可用於Bigtable。 https://cloud.google.com/iam/ –