使用Phoenix连接hbase数据库

来源:互联网 发布:适合冥想的音乐 知乎 编辑:程序博客网 时间:2024/05/16 17:31

本文在Phoenix及hbase均已安装成功的前提下。在eclipse中使用jdbc连接hbase数据库。

导入所需jar包

<dependency>            <groupId>org.apache.phoenix</groupId>            <artifactId>phoenix-core</artifactId>            <version>4.7.0-HBase-1.1</version>        </dependency>        <dependency>            <groupId>org.apache.hbase</groupId>            <artifactId>hbase-client</artifactId>            <version>1.1.2</version>        </dependency>

编写连接hbase的工具类

package com.util;import java.sql.Connection;import java.sql.DriverManager;import java.sql.SQLException;public class PhoenixUtil {    public static  Connection getConnection(){        try {            Class.forName("org.apache.phoenix.jdbc.PhoenixDriver");            Connection connection = null;    connection=DriverManager.getConnection("此处为zookeeper地址,可以为一个或多个:zookeeper的端口号:zookeeper.znode.parent(此处默认为 /hbase默认可不填写)");     //connection=DriverManager.getConnection("jdbc:phoenix:192.168.23.44,192.168.23.43,192.168.23.45:2181:/hbase-unsecure");            return connection;        } catch (ClassNotFoundException e) {            // TODO Auto-generated catch block            e.printStackTrace();        } catch (SQLException e) {            // TODO Auto-generated catch block            e.printStackTrace();        }        return null;    }    public static void closeConnection(Connection conn){        try {            conn.close();        } catch (SQLException e) {            // TODO Auto-generated catch block            e.printStackTrace();        }    }}

编写增删改查的测试类

package com.test;import java.sql.Connection;import java.sql.PreparedStatement;import java.sql.ResultSet;import java.sql.SQLException;import org.junit.Test;import com.util.PhoenixUtil;public class HbaseTest {    /**     * 创建表     */    @Test    public void createTable() {        Connection conn = PhoenixUtil.getConnection();        String sql = "CREATE TABLE user (id varchar PRIMARY KEY,name varchar ,password varchar)";        try {            PreparedStatement sta = conn.prepareStatement(sql);            sta.execute();            System.out.println("create success....");        } catch (SQLException e) {            // TODO Auto-generated catch block            e.printStackTrace();        } finally {            if (conn != null) {                PhoenixUtil.closeConnection(conn);            }        }    }    /**     * 插入数据     */    @Test    public void insertData() {        Connection conn = PhoenixUtil.getConnection();        String sql = "upsert into user(id, name, password) values('001', 'admin', 'admin')";        try {            PreparedStatement ps = conn.prepareStatement(sql);            String msg = ps.executeUpdate() > 0 ? "insert success..."                    : "insert fail...";            conn.commit();            System.out.println(msg);        } catch (SQLException e) {            // TODO Auto-generated catch block            e.printStackTrace();        } finally {            if (conn != null) {                PhoenixUtil.closeConnection(conn);            }        }    }    /**     * 查询表     */    @Test    public void findData() {        Connection conn = PhoenixUtil.getConnection();        String sql = "select * from user";        try {            PreparedStatement ps = conn.prepareStatement(sql);            ResultSet rs = ps.executeQuery();            System.out.println("id" + "\t" + "name" + "\t" + "password");            System.out.println("==========================");            if (rs != null) {                while (rs.next()) {                    System.out.print(rs.getString("id") + "\t");                    System.out.print(rs.getString("name") + "\t");                    System.out.println(rs.getString("password"));                }            }        } catch (SQLException e) {            // TODO Auto-generated catch block            e.printStackTrace();        } finally {            if (conn != null) {                PhoenixUtil.closeConnection(conn);            }        }    }    /**     * 删除表     */    @Test    public void dropTable() {        Connection conn = PhoenixUtil.getConnection();        String sql = "drop table user";        try {            PreparedStatement ps = conn.prepareStatement(sql);            ps.execute();            System.out.println("drop success...");        } catch (SQLException e) {            // TODO Auto-generated catch block            e.printStackTrace();        } finally {            if (conn != null) {                PhoenixUtil.closeConnection(conn);            }        }    }    /**     * 删除数据     */    @Test    public void delData() {        Connection conn = PhoenixUtil.getConnection();        String sql = "delete from user where id='001'";        try {            PreparedStatement ps = conn.prepareStatement(sql);            String msg = ps.executeUpdate() > 0 ? "delete success..."                    : "delete fail...";            conn.commit();            System.out.println(msg);        } catch (SQLException e) {            // TODO Auto-generated catch block            e.printStackTrace();        } finally {            if (conn != null) {                PhoenixUtil.closeConnection(conn);            }        }    }}

完整资源连接:phoenix连接hbase完整代码

原创粉丝点击