Skip to content

Commit

Permalink
加入Phoenix例子
Browse files Browse the repository at this point in the history
  • Loading branch information
Kyofin committed Jul 1, 2019
1 parent c52f4b5 commit 642efe2
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 0 deletions.
6 changes: 6 additions & 0 deletions hbase-starter/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -35,5 +35,11 @@
<version>4.12</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.apache.phoenix</groupId>
<artifactId>phoenix-core</artifactId>
<version>4.14.0-HBase-1.2</version>
</dependency>
</dependencies>
</project>
61 changes: 61 additions & 0 deletions hbase-starter/src/test/java/TestPhoenixJDBC.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@


import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;


/**
* Desc: create table, create index, insert data, select table.
*
* 如果出现错误:
* Mutable secondary indexes must have the hbase.regionserver.wal.codec property set to org.apache.hadoop.hbase.regionserver.wal.IndexedWALEditCodec in the hbase-sites.xml of every region server. tableName=TEST_IDX
*
* 解决办法: hbase-site.xml中加入
* <property>
* <name>hbase.regionserver.wal.codec</name>
* <value>org.apache.hadoop.hbase.regionserver.wal.IndexedWALEditCodec</value>
* </property> 
*/
public class TestPhoenixJDBC {


private static String driver = "org.apache.phoenix.jdbc.PhoenixDriver";


public static void main(String[] args) throws SQLException {


Connection conn = null;
Statement stmt = null;
ResultSet rs = null;
try {
Class.forName(driver);
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
conn = DriverManager.getConnection("jdbc:phoenix:localhost:2181");
stmt = conn.createStatement();
stmt.execute("drop table if exists test");
stmt.execute("create table test (mykey integer not null primary key, mycolumn varchar)");
stmt.execute("create index test_idx on test(mycolumn)");
stmt.executeUpdate("upsert into test values (1,'World!')");
stmt.executeUpdate("upsert into test values (2,'Hello')");
stmt.executeUpdate("upsert into test values (3,'World!')");
stmt.executeUpdate("upsert into test values (3,'====World!====')");
conn.commit();
rs = stmt.executeQuery("select mykey from test where mycolumn='Hello'");
while (rs.next()) {
System.out.println(rs.getInt(1));
}
stmt.close();
rs.close();
conn.close();


}


}

0 comments on commit 642efe2

Please sign in to comment.