Hbase的SQL介面之Phoenix使用總結(1)
阿新 • • 發佈:2019-02-03
#最近在寫操作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- publicclass HBaseUtility {
- static {
- try {
- Class.forName("com.salesforce.phoenix.jdbc.PhoenixDriver");
- } catch (ClassNotFoundException e) {
- thrownew HBaseException(e);
- }
- }
-
publicstatic Connection getConnection() {
- String getDBConnectionString = "jdbc:phoenix:hadoop.master"; // 從配置檔案中讀取連結字串
- try {
- Connection _Connection = DriverManager
- .getConnection(getDBConnectionString);
- return _Connection;
- } catch (SQLException e) {
-
throw
- }
- }
- }
- publicclass HBaseHelper {
- static HBaseHelper _HBaseHelper = null;
- Connection _Connection = null;
- Statement _Statement = null;
- PreparedStatement _PreparedStatement = null;
- String _getExceptionInfoString = "";
- String _getDBConnectionString = "";
- private HBaseHelper() {}
- /*
- * Initialization
- */
- publicstatic HBaseHelper getInstanceBaseHelper() {
- if (_HBaseHelper == null)
- synchronized (HBaseHelper.class) {
- if(_HBaseHelper==null)
- _HBaseHelper = new HBaseHelper();
- }
- return _HBaseHelper;
- }
- /*
- * Insert , Delete , Update
- */
- public Object ExcuteNonQuery(String sql) {
- int n = 0;
- try {
- _Connection =HBaseUtility.getConnection();
- _Statement = _Connection.createStatement();
- n = _Statement.executeUpdate(sql);
- _Connection.commit();
- } catch (Exception e) {
- Dispose();
- thrownew HBaseException(e.getMessage(),e);
- }
- return n;
- }
- public Object ExcuteNonQuery(String sql, Object[] args) {
- int n = 0;
- try {
- _Connection =HBaseUtility.getConnection();
- _PreparedStatement = _Connection.prepareStatement(sql);
- for (int i = 0; i < args.length; i++)
- _PreparedStatement.setObject(i + 1, args[i]);
- n = _PreparedStatement.executeUpdate();
- _Connection.commit();
- } catch (SQLException e) {
- Dispose();
- thrownew HBaseException(e.getMessage(),e);
- }
- return n;
- }
- /*
- * Query
- */
- public ResultSet ExecuteQuery(String sql) {
- ResultSet rsResultSet = null;
- try {
- _Connection =HBaseUtility.getConnection();
- _Statement = _Connection.createStatement();
- rsResultSet = _Statement.executeQuery(sql);
- } catch (Exception e) {
- Dispose();
- thrownew HBaseException(e.getMessage(),e);
- }
- return rsResultSet;
- }
- public ResultSet ExceteQuery(String sql, Object[] args) {
- ResultSet rsResultSet = null;
- try {
- _Connection =HBaseUtility.getConnection();
- _PreparedStatement = _Connection.prepareStatement(sql);
- for (int i = 0; i < args.length; i++)
- _PreparedStatement.setObject(i + 1, args[i]);
- rsResultSet = _PreparedStatement.executeQuery();
- } catch (Exception e) {
- Dispose();
- thrownew HBaseException(e.getMessage(),e);
- }
- return rsResultSet;
- }
- publicvoid Dispose() {
- try {
- if (_Connection != null)
- _Connection.close();
- if (_Statement != null)
- _Statement.close();
- } catch (Exception e) {
- // TODO: handle exception
- _getExceptionInfoString = e.getMessage();
- }
- }
- }
以上是我寫的一個基本的DBHelper類。因為自己不太會寫Java程式碼,如果有不足之處,請各位指出。
關於Phoenix的詳細資訊,請參考:
待續。。。。