Advertisement
Guest User

Untitled

a guest
Jun 21st, 2017
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.85 KB | None | 0 0
  1. package org.kududb.examples.sample.test;
  2.  
  3. import com.cloudera.impala.jdbc41.DataSource;
  4.  
  5. import java.io.IOException;
  6. import java.nio.charset.Charset;
  7. import java.nio.file.Files;
  8. import java.nio.file.Paths;
  9. import java.sql.Connection;
  10. import java.sql.PreparedStatement;
  11. import java.sql.ResultSet;
  12. import java.sql.SQLException;
  13. import java.sql.Statement;
  14. import java.util.Arrays;
  15. import java.util.List;
  16. import java.util.Random;
  17. import java.util.UUID;
  18.  
  19. public class ImpalaJDBCInsertFile {
  20.  
  21. private static Connection connectViaDS() throws Exception
  22. {
  23. DataSource ds = new com.cloudera.impala.jdbc41.DataSource();
  24. // ds.setURL("jdbc:impala://swat-audits-1.gce.cloudera.com:21050"); //get
  25. // a connection refused with this
  26. // ds.setURL("jdbc:impala://swat-audits-2.gce.cloudera.com:21050");
  27. ds.setURL("jdbc:impala://swat-kudu-2.gce.cloudera.com:21050");
  28. return ds.getConnection();
  29. }
  30.  
  31. public static void main(String[] argv) throws SQLException {
  32. Connection conn = null;
  33. try {
  34. conn = connectViaDS();
  35. } catch (Exception e) {
  36. System.out.println("Connection Failed! Check output console");
  37. e.printStackTrace();
  38. }
  39. if(conn!=null){
  40. System.out.println("connection success!!!");
  41. }else{
  42. System.out.println("connection failure!!!");
  43.  
  44. }
  45. Statement stmt = null;
  46. try {
  47. //create table
  48. stmt = conn.createStatement();
  49.  
  50. //insert into table
  51. PreparedStatement s=conn.prepareStatement("INSERT INTO " +
  52. "new_jdbc_impala_table (" +
  53. "id," +
  54. "name) " +
  55. "VALUES (?,?)");
  56. String file = readFile("test.txt", Charset.defaultCharset());
  57. for(int i=0;i<10;i++) {
  58. s.setLong(1, i);
  59.  
  60. s.setString(2, file);
  61. s.executeUpdate();
  62. System.out.println("Inserted " + i + " records into the table " );
  63. }
  64. //query from the table
  65. String sql2 = "SELECT id, name FROM new_jdbc_impala_table";
  66. ResultSet rs = stmt.executeQuery(sql2);
  67. //STEP 5: Extract data from result set
  68. while(rs.next()){
  69. //Retrieve by column name
  70. int id = rs.getInt("id");
  71. String name = rs.getString("name");
  72.  
  73. //Display values
  74. System.out.print("ID: " + id);
  75. System.out.print(", Name: " + name);
  76. }
  77. rs.close();
  78. }catch (Exception e){
  79. e.printStackTrace();
  80. }finally {
  81. try {
  82. if (stmt != null)
  83. conn.close();
  84. } catch (SQLException se) {
  85. }
  86. try {
  87. if (conn != null)
  88. conn.close();
  89. } catch (SQLException se) {
  90. se.printStackTrace();
  91. }
  92. }
  93.  
  94. }
  95. static String readFile(String path, Charset encoding)
  96. throws IOException
  97. {
  98. byte[] encoded = Files.readAllBytes(Paths.get(path));
  99. return new String(encoded, encoding);
  100. }
  101. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement