Phoenix(四)JDBC CRUD操作

来源:互联网 发布:奥飞游戏 知乎 编辑:程序博客网 时间:2024/05/29 13:12

1. 说明


本篇主要介绍使用JDBC的方式来对HBase中的数据进行CRUD操作,项目为maven项目。请自行将phoenix-4.2.2-client.jar(可以在下载的phoenix-4.2.2-bin.tar.gz中找到该jar包)添加到项目的classpath中,将HBase集群的hbase-site.xml配置文件添加到项目的resources目录下。为了查看日志输出配置了一个简单的log4j.properties,也一并放到resources目录下。


log4j.properties 内容如下:


log4j.rootLogger=WARN, A1# A1 is set to be a ConsoleAppender.log4j.appender.A1=org.apache.log4j.ConsoleAppender# A1 uses PatternLayout.log4j.appender.A1.layout=org.apache.log4j.PatternLayoutlog4j.appender.A1.layout.ConversionPattern=%-4r [%t] %-5p %c %x - %m%n


2. 获得连接


/**  * project:hadoop-phoenix  * file:BaseDB.java  * time:2015年5月4日 下午2:19:57  * description:  */package cn.com.dimensoft.hadoop.phoenix.jdbc;import java.sql.Connection;import java.sql.DriverManager;/** * class: BaseDB * package: cn.com.dimensoft.hadoop.phoenix.jdbc * time: 2015年5月4日 下午2:19:57 * description:  */public class BaseDB {    /**     *      * name:getConnection     * time:2015年5月6日 下午2:07:06     * description: get JDBC connection     * @return connection     */    public static Connection getConnection() {        try {            // load driver            Class.forName("org.apache.phoenix.jdbc.PhoenixDriver");            // get connection            // jdbc 的 url 类似为 jdbc:phoenix [ :<zookeeper quorum> [ :<port number> ] [ :<root node> ] ],            // 需要引用三个参数:hbase.zookeeper.quorum、hbase.zookeeper.property.clientPort、and zookeeper.znode.parent,            // 这些参数可以缺省不填而在 hbase-site.xml 中定义。            return DriverManager.getConnection("jdbc:phoenix");        } catch (Exception e) {            e.printStackTrace();            return null;        }    }}


3. 创建表


/**     *      * name:create     * time:2015年5月4日 下午2:58:31     * description:create table     */    public static void create() {        Connection conn = null;        try {            // get connection            conn = BaseDB.getConnection();            // check connection            if (conn == null) {                System.out.println("conn is null...");                return;            }            // check if the table exist            ResultSet rs = conn.getMetaData().getTables(null, null, "USER",                    null);            if (rs.next()) {                System.out.println("table user is exist...");                return;            }            // create sql            String sql = "CREATE TABLE user (id varchar PRIMARY KEY,INFO.account varchar ,INFO.passwd varchar)";            PreparedStatement ps = conn.prepareStatement(sql);            // execute            ps.execute();            System.out.println("create success...");        } catch (SQLException e) {            e.printStackTrace();        } finally {            if (conn != null) {                try {                    conn.close();                } catch (SQLException e) {                    e.printStackTrace();                }            }        }    }


4. 插入数据


/**     *      * name:insert     * time:2015年5月4日 下午2:59:11     * description:     */    public static void upsert() {        Connection conn = null;        try {            // get connection            conn = BaseDB.getConnection();            // check connection            if (conn == null) {                System.out.println("conn is null...");                return;            }            // create sql            String sql = "upsert into user(id, INFO.account, INFO.passwd) values('001', 'admin', 'admin')";            PreparedStatement ps = conn.prepareStatement(sql);            // execute upsert            String msg = ps.executeUpdate() > 0 ? "insert success..."                    : "insert fail...";            // you must commit            conn.commit();            System.out.println(msg);        } catch (SQLException e) {            e.printStackTrace();        } finally {            if (conn != null) {                try {                    conn.close();                } catch (SQLException e) {                    e.printStackTrace();                }            }        }    }


5. 查询数据


/**     *      * name:query     * time:2015年5月4日 下午3:58:12     * description:query data     */    public static void query() {        Connection conn = null;        try {            // get connection            conn = BaseDB.getConnection();            // check connection            if (conn == null) {                System.out.println("conn is null...");                return;            }            // create sql            String sql = "select * from user";            PreparedStatement ps = conn.prepareStatement(sql);            ResultSet rs = ps.executeQuery();            System.out.println("id" + "\t" + "account" + "\t" + "passwd");            System.out.println("======================");            if (rs != null) {                while (rs.next()) {                    System.out.print(rs.getString("id") + "\t");                    System.out.print(rs.getString("account") + "\t");                    System.out.println(rs.getString("passwd"));                }            }        } catch (SQLException e) {            e.printStackTrace();        } finally {            if (conn != null) {                try {                    conn.close();                } catch (SQLException e) {                    e.printStackTrace();                }            }        }    }


6. 更新数据


更新数据的操作与插入数据相同


7. 删除数据


/**     *      * name:delete     * time:2015年5月4日 下午4:03:11     * description:delete data     */    public static void delete() {        Connection conn = null;        try {            // get connection            conn = BaseDB.getConnection();            // check connection            if (conn == null) {                System.out.println("conn is null...");                return;            }            // create sql            String sql = "delete from user where id='001'";            PreparedStatement ps = conn.prepareStatement(sql);            // execute upsert            String msg = ps.executeUpdate() > 0 ? "delete success..."                    : "delete fail...";            // you must commit            conn.commit();            System.out.println(msg);        } catch (SQLException e) {            e.printStackTrace();        } finally {            if (conn != null) {                try {                    conn.close();                } catch (SQLException e) {                    e.printStackTrace();                }            }        }    }


8. 删除表


/**     *      * name:drop     * time:2015年5月4日 下午4:03:35     * description:drop table     */    public static void drop() {        Connection conn = null;        try {            // get connection            conn = BaseDB.getConnection();            // check connection            if (conn == null) {                System.out.println("conn is null...");                return;            }            // create sql            String sql = "drop table user";            PreparedStatement ps = conn.prepareStatement(sql);            // execute            ps.execute();            System.out.println("drop success...");        } catch (SQLException e) {            e.printStackTrace();        } finally {            if (conn != null) {                try {                    conn.close();                } catch (SQLException e) {                    e.printStackTrace();                }            }        }    }
0 0
原创粉丝点击