PhoenixDML.java 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. package com.saga.hbase.phoenix.app;
  2. import com.saga.hbase.phoenix.constant.PhoenixConstant;
  3. import java.sql.*;
  4. public class PhoenixDML {
  5. private static Connection connection = null;
  6. private static Statement statement = null;
  7. static {
  8. System.out.println("init phoenix config ... ");
  9. try {
  10. Class.forName(PhoenixConstant.PHOENIX_DRIVER);
  11. connection = DriverManager.getConnection(PhoenixConstant.PHOENIX_SERVER);
  12. statement = connection.createStatement();
  13. } catch (ClassNotFoundException e) {
  14. e.printStackTrace();
  15. } catch (SQLException e) {
  16. e.printStackTrace();
  17. }
  18. }
  19. public static void main(String[] args) throws ClassNotFoundException, SQLException {
  20. delete();
  21. closeConnection();
  22. }
  23. //TODO 查询
  24. public static void select(String schema, String tableName) throws ClassNotFoundException, SQLException {
  25. Class.forName(PhoenixConstant.PHOENIX_DRIVER);
  26. Connection connection = DriverManager.getConnection(PhoenixConstant.PHOENIX_SERVER);
  27. Statement statement = connection.createStatement();
  28. ResultSet resultSet = statement.executeQuery("select * from SYSTEM.US_POPULATION");
  29. int columnCount = resultSet.getMetaData().getColumnCount();
  30. while (resultSet.next()){
  31. for (int i = 1; i <= columnCount; i++) {
  32. String state = resultSet.getString(i);
  33. System.out.print(state + " ");
  34. }
  35. System.out.println();
  36. }
  37. }
  38. //TODO 更新插入
  39. public static Integer upsert() throws SQLException {
  40. String sql1="upsert into SAGA_LOG.test_phoenix_api values(1,'test1')";
  41. String sql2="upsert into SAGA_LOG.test_phoenix_api values(2,'test2')";
  42. String sql3="upsert into SAGA_LOG.test_phoenix_api values(3,'test3')";
  43. statement.executeUpdate(sql1);
  44. statement.executeUpdate(sql2);
  45. statement.executeUpdate(sql3);
  46. connection.commit();
  47. return 0;
  48. }
  49. //TODO 插入
  50. public Integer insert(){
  51. return 0;
  52. }
  53. //TODO 删除
  54. public static Integer delete() throws SQLException {
  55. String sql1="delete from SAGA_LOG.test_phoenix_api where mykey = 1";
  56. statement.executeUpdate(sql1);
  57. connection.commit();
  58. return 0;
  59. }
  60. public static void closeConnection(){
  61. try {
  62. if (statement != null)
  63. statement.close();
  64. if (connection != null)
  65. connection.close();
  66. } catch (SQLException e) {
  67. e.printStackTrace();
  68. }
  69. }
  70. }