Hbase的SQL接口之Phoenix使用总结(1)

转载自:http://blog.csdn.net/sunthx/article/details/8707898

#最近在写操作HBase的接口,顺便研究了一下Phoenix,简单的介绍下Phoenix,Phoenix用Java实现,人们通过Phoenix,可以用自己所熟悉的SQL语言来操作HBase,当然它是开源的。


1.如何让HBase支持Phoenix?


将phoenix-1.1.jar复制到HBase集群每个节点的HBase文件夹下的lib目录,然后重启HBase

2.客户端怎么通过Phoenix访问或操作Hbase?


在你自己的Java项目下,引用phoenix-1.1-client.jar



下面给出使用Phoenix基本的代码:

[java]  view plain copy print ?
  1. public class HBaseUtility {  
  2.   
  3.     static {  
  4.         try {  
  5.             Class.forName("com.salesforce.phoenix.jdbc.PhoenixDriver");  
  6.         } catch (ClassNotFoundException e) {  
  7.             throw new HBaseException(e);  
  8.         }  
  9.     }  
  10.   
  11.     public static Connection getConnection() {  
  12.         String getDBConnectionString = "jdbc:phoenix:hadoop.master"// 从配置文件中读取链接字符串  
  13.         try {  
  14.             Connection _Connection = DriverManager  
  15.                     .getConnection(getDBConnectionString);  
  16.             return _Connection;  
  17.         } catch (SQLException e) {  
  18.             throw new HBaseException(e.getMessage(), e);  
  19.         }  
  20.     }  
  21.   
  22. }  


[java]  view plain copy print ?
  1. public class HBaseHelper {  
  2.   
  3.     static HBaseHelper _HBaseHelper = null;  
  4.     Connection _Connection = null;  
  5.     Statement _Statement = null;  
  6.     PreparedStatement _PreparedStatement = null;  
  7.     String _getExceptionInfoString = "";  
  8.     String _getDBConnectionString = "";  
  9.   
  10.     private HBaseHelper() {}          
  11.   
  12.     /* 
  13.      * Initialization 
  14.      */  
  15.     public static HBaseHelper getInstanceBaseHelper() {  
  16.         if (_HBaseHelper == null)  
  17.             synchronized (HBaseHelper.class) {  
  18.                 if(_HBaseHelper==null)  
  19.                     _HBaseHelper = new HBaseHelper();  
  20.             }             
  21.         return _HBaseHelper;  
  22.     }  
  23.   
  24.     /* 
  25.      * Insert , Delete , Update 
  26.      */  
  27.     public Object ExcuteNonQuery(String sql) {  
  28.         int n = 0;  
  29.         try {  
  30.             _Connection =HBaseUtility.getConnection();  
  31.             _Statement = _Connection.createStatement();  
  32.             n = _Statement.executeUpdate(sql);  
  33.             _Connection.commit();  
  34.         } catch (Exception e) {  
  35.             Dispose();  
  36.             throw new HBaseException(e.getMessage(),e);  
  37.         }   
  38.         return n;  
  39.     }  
  40.   
  41.     public Object ExcuteNonQuery(String sql, Object[] args) {  
  42.         int n = 0;  
  43.         try {  
  44.             _Connection =HBaseUtility.getConnection();  
  45.             _PreparedStatement = _Connection.prepareStatement(sql);  
  46.             for (int i = 0; i < args.length; i++)  
  47.                 _PreparedStatement.setObject(i + 1, args[i]);  
  48.             n = _PreparedStatement.executeUpdate();  
  49.             _Connection.commit();  
  50.         } catch (SQLException e) {  
  51.             Dispose();  
  52.             throw new HBaseException(e.getMessage(),e);  
  53.         }  
  54.         return n;  
  55.     }  
  56.   
  57.     /* 
  58.      * Query 
  59.      */  
  60.     public ResultSet ExecuteQuery(String sql) {  
  61.         ResultSet rsResultSet = null;  
  62.         try {  
  63.             _Connection =HBaseUtility.getConnection();  
  64.             _Statement = _Connection.createStatement();  
  65.             rsResultSet = _Statement.executeQuery(sql);  
  66.         } catch (Exception e) {  
  67.             Dispose();  
  68.             throw new HBaseException(e.getMessage(),e);  
  69.         }   
  70.         return rsResultSet;  
  71.     }  
  72.   
  73.     public ResultSet ExceteQuery(String sql, Object[] args) {  
  74.         ResultSet rsResultSet = null;  
  75.         try {  
  76.             _Connection =HBaseUtility.getConnection();  
  77.             _PreparedStatement = _Connection.prepareStatement(sql);  
  78.             for (int i = 0; i < args.length; i++)  
  79.                 _PreparedStatement.setObject(i + 1, args[i]);  
  80.             rsResultSet = _PreparedStatement.executeQuery();  
  81.   
  82.         } catch (Exception e) {  
  83.             Dispose();  
  84.             throw new HBaseException(e.getMessage(),e);  
  85.         }   
  86.         return rsResultSet;  
  87.     }  
  88.   
  89.     public void Dispose() {  
  90.         try {  
  91.             if (_Connection != null)  
  92.                 _Connection.close();  
  93.             if (_Statement != null)  
  94.                 _Statement.close();  
  95.         } catch (Exception e) {  
  96.             // TODO: handle exception  
  97.             _getExceptionInfoString = e.getMessage();  
  98.         }  
  99.     }  
  100. }  


以上是我写的一个基本的DBHelper类。因为自己不太会写Java代码,如果有不足之处,请各位指出。


关于Phoenix的详细信息,请参考:

1.http://blog.csdn.net/ricohzhanglong/article/details/8587493

2.https://github.com/forcedotcom/phoenix


待续。。。。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值