• HBase 6、用Phoenix Java api操作HBase


    开发环境准备:eclipse3.5jdk1.7window8hadoop2.2.0hbase0.98.0.2phoenix4.3.0

      1、从集群拷贝以下文件:core-site.xmlhbase-site.xmlhdfs-site.xml文件放到工程src

      2、把phoenixphoenix-4.3.0-client.jarphoenix-core-4.3.0.jar添加到工程classpath

      3、配置集群中各节点的hosts文件,把客户端的hostnameIP添加进去

      4、在客户端host文件中加入集群的hostnameIP

      5、工程截图

       

      例子1:

    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.ResultSet;
    import java.sql.Statement;
    
    public class Phoenix_Test {
    	/**
    	 * 使用phoenix提供的api操作hbase读取数据
    	 */
    	public static void main(String[] args) throws Throwable {
    		try {
    			// 下面的驱动为Phoenix老版本使用2.11使用,对应hbase0.94+
    			// Class.forName("com.salesforce.phoenix.jdbc.PhoenixDriver");
    
    			// phoenix4.3用下面的驱动对应hbase0.98+
    			Class.forName("org.apache.phoenix.jdbc.PhoenixDriver");
    		} catch (Exception e) {
    			e.printStackTrace();
    		}
    		// 这里配置zookeeper的地址,可单个,也可多个。可以是域名或者ip
    		String url = "jdbc:phoenix:node5,node6,node7";
    		// String url =
    		// "jdbc:phoenix:41.byzoro.com,42.byzoro.com,43.byzoro.com:2181";
    		Connection conn = DriverManager.getConnection(url);
    		Statement statement = conn.createStatement();
    		String sql = "select count(1) as num from WEB_STAT";
    		long time = System.currentTimeMillis();
    		ResultSet rs = statement.executeQuery(sql);
    		while (rs.next()) {
    			int count = rs.getInt("num");
    			System.out.println("row count is " + count);
    		}
    		long timeUsed = System.currentTimeMillis() - time;
    		System.out.println("time " + timeUsed + "mm");
    		// 关闭连接
    		rs.close();
    		statement.close();
    		conn.close();
    	}
    }
    

      执行结果:

      

      例子2:

    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.ResultSet;
    import java.sql.Statement;
    
    public class Phoenix_Test2 {
    	/**
    	 * 使用phoenix提供的api操作hbase中读取数据
    	 */
    	public static void main(String[] args) throws Throwable {
    		try {
    			//下面的驱动为Phoenix老版本使用2.11使用,对应hbase0.94+
    			//Class.forName("com.salesforce.phoenix.jdbc.PhoenixDriver");
    			//phoenix4.3用下面的驱动对应hbase0.98+
    			Class.forName("org.apache.phoenix.jdbc.PhoenixDriver");
    		} catch (Exception e) {
    			e.printStackTrace();
    		}
    		//这里配置zk的地址,可单个,也可多个。可以是域名或者ip
    		String url = "jdbc:phoenix:node5,node6,node7";
    		Connection conn = DriverManager.getConnection(url);
    		Statement statement = conn.createStatement();
    		String sql = "select *  from web_stat where core = 1";
    		long time = System.currentTimeMillis();
    		ResultSet rs = statement.executeQuery(sql);
    		while (rs.next()) {
    			//获取core字段值
    			int core = rs.getInt("core");
    			//获取core字段值
    			String host = rs.getString("host");
    			//获取domain字段值
    			String domain = rs.getString("domain");
    			//获取feature字段值
    			String feature = rs.getString("feature");
    			//获取date字段值,数据库中字段为Date类型,这里代码会自动转化为string类型
    			String date = rs.getString("date");
    			//获取db字段值
    			String db = rs.getString("db");
    			System.out.println("host:"+host+"	domain:"+domain+"	feature:"+feature+"	date:"+date+"	core:" + core+"	db:"+db);
    		}
    		long timeUsed = System.currentTimeMillis() - time;
    		System.out.println("time " + timeUsed + "mm");
    		//关闭连接
    		rs.close();
    		statement.close();
    		conn.close();
    	}
    }
    

      执行结果:

      

  • 相关阅读:
    Ext.grid.column.Column主要配置项及示例
    Ext.grid.Panel主要配置及示例
    EF Code First关系规则及配置
    ExtJS4系列目录
    EF Code First数据库连接配置
    ASP.NET MVC系列:ASP.NET MVC简介
    Ext JS下载及配置
    Ext.container.Viewport
    Ext.tab.Panel页签
    ASP.NET MVC系列:Controller/Action
  • 原文地址:https://www.cnblogs.com/raphael5200/p/5267338.html
Copyright © 2020-2023  润新知