2016-11-16 64 views
0

我已經使用包含2列族的shell在Hbase中創建了一個表。我可以使用Hbase客戶端API寫入它。我正在使用phoenix JDBC驅動程序從中查詢數據。我瞭解到HBase和Phoenix表之間沒有一對一的映射關係。所以,我創建一個視圖我現有的表,但每當我跑我的代碼,我得到Apache Phoenix創建視圖拋出TableAlreadyExistException

org.apache.phoenix.schema.TableAlreadyExistsException: ERROR 1013 (42M04): Table already exists. tableName=test 
at org.apache.phoenix.schema.MetaDataClient.createTableInternal(MetaDataClient.java:1911) 
at org.apache.phoenix.schema.MetaDataClient.createTable(MetaDataClient.java:744) 
at org.apache.phoenix.compile.CreateTableCompiler$2.execute(CreateTableCompiler.java:186) 
at org.apache.phoenix.jdbc.PhoenixStatement$2.call(PhoenixStatement.java:303) 
at org.apache.phoenix.jdbc.PhoenixStatement$2.call(PhoenixStatement.java:295) 
at org.apache.phoenix.call.CallRunner.run(CallRunner.java:53) 
at org.apache.phoenix.jdbc.PhoenixStatement.executeMutation(PhoenixStatement.java:293) 
at org.apache.phoenix.jdbc.PhoenixStatement.executeUpdate(PhoenixStatement.java:1236) 
at com.lam.app.PhoenixClient.main(PhoenixClient.java:49) 

我的客戶端代碼是這樣的

import java.sql.Connection; 
import java.sql.DriverManager; 
import java.sql.PreparedStatement; 
import java.sql.ResultSet; 
import java.sql.SQLException; 
import java.sql.Statement; 

public class PhoenixClient 
{ 

    public static void main(String[] args) 
    { 
     // Create variables 
     Connection connection = null; 
     Statement statement = null; 
     ResultSet rs = null; 
     PreparedStatement ps = null; 

     try 
     { 
      // Connect to the database 
      connection = DriverManager.getConnection("jdbc:phoenix:zkserver:2181:/hbase-unsecure"); 

      // Create a JDBC statement 
      statement = connection.createStatement(); 

      //HBase table schema - Table name : test, columnfamily 1 - data, ColumnFamily2 - context 
      // Execute our statements 
      statement.executeUpdate("CREATE VIEW \"test\" (pk VARCHAR not null PRIMARY KEY, " 
        + "\"data\".\"mydata\" VARCHAR," + " \"context\".\"mycontext\" VARCHAR)"); 

      //connection.commit(); 

      // Query for table 
      /*ps = connection 
       .prepareStatement("select * from \"test\" WHERE \"mydata\" = \"names\""); 
      rs = ps.executeQuery(); 
      System.out.println("Table Values"); 
      while (rs.next()) 
      { 
       Integer myKey = rs.getInt(1); 
       String myColumn = rs.getString(2); 
       System.out.println("\tRow: " + myKey + " = " + myColumn); 
      }*/ 

     } 
     catch (SQLException e) 
     { 
      e.printStackTrace(); 
     } 
     finally 
     { 
      if (ps != null) 
      { 
       try 
       { 
        ps.close(); 
       } 
       catch (Exception e) 
       { 
       } 
      } 
      if (rs != null) 
      { 
       try 
       { 
        rs.close(); 
       } 
       catch (Exception e) 
       { 
       } 
      } 
      if (statement != null) 
      { 
       try 
       { 
        statement.close(); 
       } 
       catch (Exception e) 
       { 
       } 
      } 
      if (connection != null) 
      { 
       try 
       { 
        connection.close(); 
       } 
       catch (Exception e) 
       { 
       } 
      } 
     } 

    } 
} 

我可以使用HBase的外殼掃描表並可以查看數據而且我知道表名是區分大小寫的(特別是phoenix使所有的都是大寫),但不知道爲什麼我不能創建視圖。請幫助

+0

如錯誤所述,您已經在Hbase中有名爲「test」的表。給另一個名稱 –

+0

@NirmalRam我知道表「測試」已經存在,因爲我使用Hbase shell創建它,但我想要做的是使用Phoenix爲它創建一個視圖,以便我可以使用該查詢。 – AnswerSeeker

+1

也許你可以在創建它之前嘗試刪除視圖? –

回答

0

按照@ rickmoritz的建議,我在創建它之前放棄了這個視圖,並且工作。

statement.executeUpdate("DROP VIEW \"test\"");