[关闭]
@xtccc 2015-10-20T15:20:43.000000Z 字数 2301 阅读 2145

JDBC SQL

给我写信
GitHub

此处输入图片的描述


Phoenix


Basic Query


  1. package cn.gridx.phoenix.sql.scala
  2. import java.sql.{ResultSet, PreparedStatement, DriverManager}
  3. /**
  4. * Created by tao on 10/20/15.
  5. *
  6. * 通过JDBC SQL的方式来查询Phoenix中的table
  7. * 使用方法:java -cp <target.jar>:<phoenix-client.jar>:<scala-library.jar> <MainClass>
  8. *
  9. */
  10. object TestJDBC {
  11. val Driver = "org.apache.phoenix.jdbc.PhoenixDriver"
  12. val ZK_CONN = "ecs2:2181" // 即使有多个zk node,这里也只需要写一个node就行了
  13. val QUERY = """select * from "TEST" where "ROW" in
  14. ('row-15', 'row-30', 'row-45', 'row-no') """
  15. def main(args: Array[String]): Unit = {
  16. Class.forName(Driver)
  17. val conn = DriverManager.getConnection(s"jdbc:phoenix:${ZK_CONN}")
  18. val pstmt: PreparedStatement = conn.prepareStatement(QUERY)
  19. val rs: ResultSet = pstmt.executeQuery
  20. while (rs.next())
  21. println(s"ROW = ${rs.getString("ROW")}, " +
  22. s"C1 = ${rs.getString("C1")}, " +
  23. s"C2 = ${rs.getString("C2")}, " +
  24. s"C3 = ${rs.getString("C3")}")
  25. rs.close
  26. pstmt.close
  27. conn.close
  28. }
  29. }



Paged Query


原理

在标准的SQL中,分页查询是通过query中的row value constructor来实现的。

Row value constructor是一个有序的值列表,其中各个值之间由逗号分隔,且整个列表由括号包裹起来,例如:

  1. ('A', 'B', 100)
  2. (200, 4.5, 'Hello')

两个row value constructors之间是可以比较大小的,例如:

  1. (2,1,1) < (9,8,5)
  2. ('x','y') > ('a','b','c')



下面是paged query的例子:

  1. package cn.gridx.phoenix.sql.scala
  2. import java.sql.DriverManager
  3. /**
  4. * Created by tao on 10/20/15.
  5. *
  6. * 测试分页查询Phoenix中的一个表,每次查询5条记录
  7. * 使用方法:java -cp <target.jar>:<phoenix-client.jar>:<scala-library.jar> <MainClass>
  8. *
  9. */
  10. object TestPagedQuery {
  11. val Driver = "org.apache.phoenix.jdbc.PhoenixDriver"
  12. val ZK_CONN = "ecs2:2181"
  13. def main(args: Array[String]): Unit = {
  14. Class.forName(Driver)
  15. val conn = DriverManager.getConnection(s"jdbc:phoenix:${ZK_CONN}")
  16. var query = """ select * from "TEST" order by "ROW" limit 5 """
  17. var pstmt = conn.prepareStatement(query)
  18. var rs = pstmt.executeQuery
  19. var row = "" // 记录下最近一次查询到的记录的ROW的值
  20. // 第一次查询
  21. println("-------------------------------------------")
  22. println("# 1 ")
  23. while (rs.next()) {
  24. println(s"ROW = ${rs.getString("ROW")}, " +
  25. s"C1 = ${rs.getString("C1")}, " +
  26. s"C2 = ${rs.getString("C2")}, " +
  27. s"C3 = ${rs.getString("C3")}")
  28. row = rs.getString("ROW")
  29. }
  30. // 后续的分页查询
  31. for (i <- 1 to 10) {
  32. query =
  33. s""" select * from "TEST" where "ROW" > '${row}'
  34. order by "ROW" limit 5 """
  35. pstmt = conn.prepareStatement(query)
  36. rs = pstmt.executeQuery
  37. println("\n---------------------------------------------------------------")
  38. println(s"# ${i} ")
  39. while (rs.next()) {
  40. println(s"ROW = ${rs.getString("ROW")}, " +
  41. s"C1 = ${rs.getString("C1")}, " +
  42. s"C2 = ${rs.getString("C2")}, " +
  43. s"C3 = ${rs.getString("C3")}")
  44. row = rs.getString("ROW")
  45. }
  46. }
  47. rs.close
  48. pstmt.close
  49. conn.close
  50. }
  51. }
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注