Apache Phoenix创建视图会抛出TableAlreadyExistException

时间:2016-11-16 08:26:16

标签: java hbase phoenix

我在Hbase中使用包含2列族的shell创建了一个表。我可以使用Hbase客户端API写入它。我正在使用phoenix JDBC驱动程序从中查询数据。我了解到HBase和Phoenix表之间没有一对一的映射。所以,我为我现有的表创建了一个视图,但每当我运行我的代码时,我都会得到

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扫描表并可以查看数据,我知道表名是区分大小写的(特别是凤凰使一切都是大写)但不知道为什么我不能创建视图。请帮忙

1 个答案:

答案 0 :(得分:0)

按照@rickmoritz的建议,我在创建视图之前删除了视图,但这确实有效。

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