Phoenix使用JAVA编程连接HBase-0.96.0

首先,需要将《使用Phoenix连接HBase-0.96.0》中编译产生的phoenix-2.2.0-SNAPSHOT-client.jar文件添加到工程的classpath中。然后编写程序连接,一个简单的实例程序如下:

public class PhoenixClient {
    private String url = "jdbc:phoenix:l-datalog1.data.cn1,l-datalog2.data.cn1,l-datalog3.data.cn1";
    private String driver = "com.salesforce.phoenix.jdbc.PhoenixDriver";
    private Connection connection = null;

    public PhoenixClient() {
        try {
            Class.forName(driver);
            connection = DriverManager.getConnection(url);
            System.out.println("Connect HBase success..");
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    public void close() {
        if (connection != null) {
            try {
                connection.close();
            } catch (Exception e) {
            } finally {
                connection = null;
            }
        }
    }

    public void createTable() {
        String sql = "create table IF NOT EXISTS phoenix_test(stuid integer not null primary key,name VARCHAR,age integer,score integer,classid integer)";
        try {
            Statement statement = connection.createStatement();
            statement.executeUpdate(sql);
            connection.commit();
            System.out.println("create table success: " + sql);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    public void deleteTable() {
        String sql = "drop table if exists pho_test";
        try {
            Statement statement = connection.createStatement();
            statement.executeUpdate(sql);
            connection.commit();
            System.out.println("delete table success: " + sql);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    public void insertRecord() {
        String sql = "upsert into phoenix_test(stuid,name,age,score,classid) values l (?,?,?,?,?)";
        try {
            PreparedStatement statement = connection.prepareStatement(sql);
            Random random = new Random();
            for (int i = 1; i <= 100; i++) {
                statement.setInt(1,i);
                statement.setString(2,"phoenix_test" + i);
                statement.setInt(3, random.nextInt(18));
                statement.setInt(4, random.nextInt(100));
                statement.setInt(5,random.nextInt(3));
                statement.execute();
            }
            connection.commit();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    public void selectRecord() {
        String sql = "select stuid,name,age,score,classid from phoenix_test";
        ResultSet resultSet = null;
        try {
            Statement statement = connection.createStatement();
            resultSet = statement.executeQuery(sql);
            while (resultSet != null && resultSet.next()) {
                System.out.println("stuid: " + resultSet.getString(1) + "\tname: " + resultSet.getString(2)
                 + "\tage: " + resultSet.getString(3) + "\tscore: " + resultSet.getString(4) + "\tclassid: " + resultSet.getString(5));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        PhoenixClient client = new PhoenixClient();
        client.createTable();
        client.insertRecord();
        client.selectRecord();
        client.deleteTable();
    }
}
注:代码中的l-datalog1.data.cn1,l-datalog2.data.cn1,l-datalog3.data.cn1是zookeeper的地址

转载请注明出处:http://blog.csdn.net/iAm333

你可能感兴趣的:(Phoenix使用JAVA编程连接HBase-0.96.0)