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


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


1.如何让HBase支持Phoenix?


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

Hbase的SQL接口之Phoenix使用总结(1)_第1张图片

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


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


Hbase的SQL接口之Phoenix使用总结(1)_第2张图片


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

public class HBaseUtility {

	static {
		try {
			Class.forName("com.salesforce.phoenix.jdbc.PhoenixDriver");
		} catch (ClassNotFoundException e) {
			throw new HBaseException(e);
		}
	}

	public static Connection getConnection() {
		String getDBConnectionString = "jdbc:phoenix:hadoop.master"; // 从配置文件中读取链接字符串
		try {
			Connection _Connection = DriverManager
					.getConnection(getDBConnectionString);
			return _Connection;
		} catch (SQLException e) {
			throw new HBaseException(e.getMessage(), e);
		}
	}

}


public class HBaseHelper {

	static HBaseHelper _HBaseHelper = null;
	Connection _Connection = null;
	Statement _Statement = null;
	PreparedStatement _PreparedStatement = null;
	String _getExceptionInfoString = "";
	String _getDBConnectionString = "";

	private HBaseHelper() {}		

	/*
	 * Initialization
	 */
	public static 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();
			throw new 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();
			throw new 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();
			throw new 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();
			throw new HBaseException(e.getMessage(),e);
		} 
		return rsResultSet;
	}

	public void Dispose() {
		try {
			if (_Connection != null)
				_Connection.close();
			if (_Statement != null)
				_Statement.close();
		} catch (Exception e) {
			// TODO: handle exception
			_getExceptionInfoString = e.getMessage();
		}
	}
}


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


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

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

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


待续。。。。

你可能感兴趣的:(Hbase的SQL接口之Phoenix使用总结(1))