导航

Java访问Phoenix连接

Posted on 2017-04-18 09:10  天一涯  阅读(11794)  评论(0编辑  收藏  举报

两种方法,一种是直接使用jdbc连接,一种是使用spring连接。

jdbc连接和访问oracle步骤相同:

///////////// 测试Phoenix连接 ///////////////
		String testSQL = "select \"1\",\"2\"  from fengji where rowid like '1-20170413%'";
		try {
			Class.forName("org.apache.phoenix.jdbc.PhoenixDriver");
		} catch (ClassNotFoundException e1) {
			System.out.println("org.apache.phoenix.jdbc.PhoenixDriver未找到");
		}
		List<Integer>resList = new ArrayList<Integer>();
		Connection con1 = DriverManager.getConnection("jdbc:phoenix:ccs01a:2181/hbase","","");
		Statement stmt = con1.createStatement();
		ResultSet rset = stmt.executeQuery(testSQL);
		while (rset.next()) {
			resList.add(rset.getInt("1"));
		}
		System.out.println(resList.size());
		stmt.close();
		con1.close();
		
		/////////////////////////////////////

  2、spring与oracle也基本相同,需要修改配置文件:

 1     <!-- phoenix配置 -->
 2     <bean id="phoenixJdbcTemplate"
 3           class="org.springframework.jdbc.core.JdbcTemplate">
 4         <constructor-arg ref="phoenixDataSource"/>
 5         <qualifier value="phoenixJdbcTemplate"></qualifier>
 6     </bean>
 7     <bean id="phoenixDataSource" class="org.apache.commons.dbcp.BasicDataSource">
 8         <property name="driverClassName" value="org.apache.phoenix.jdbc.PhoenixDriver"/>
 9         <property name="url" value="jdbc:phoenix:ccs01a:2181/hbase"/>
10         <property name="username" value=""/>
11         <property name="password" value=""/>
12         <property name="initialSize" value="20"/>
13         <property name="maxActive" value="0"/>
14         <!--因为Phoenix进行数据更改时不会自动的commit,必须要添加defaultAutoCommit属性,否则会导致数据无法提交的情况-->
15         <property name="defaultAutoCommit" value="true"/>
16     </bean>