兩種方法,一種是直接使用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>