ApachePhoenix创建视图引发TableReadyExistException

3zwtqj6y  于 2021-06-10  发布在  Hbase
关注(0)|答案(1)|浏览(449)

我使用shell在hbase中创建了一个表,其中包含2列族。我可以使用hbase客户端api写入它。我使用phoenix jdbc驱动程序从中查询数据。我了解到hbase和phoenix表之间没有一对一的Map。因此,我为现有表创建了一个视图,但是每当我运行代码时

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 shell扫描表并可以查看数据,我知道表名区分大小写(特别是phoenix使所有内容都大写),但不确定为什么不能创建视图。请帮忙

x8goxv8g

x8goxv8g1#

按照@rickmoritz的建议,我在创建视图之前放弃了它,这就成功了。

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

相关问题