Advertisement
Guest User

Untitled

a guest
Nov 21st, 2019
159
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.80 KB | None | 0 0
  1. package com.bigdata.org.tes;
  2.  
  3. import java.io.FileOutputStream;
  4. import java.io.IOException;
  5. import java.sql.Connection;
  6. import java.sql.DriverManager;
  7. import java.sql.ResultSet;
  8. import java.sql.ResultSetMetaData;
  9. import java.sql.SQLException;
  10. import java.sql.Statement;
  11. import java.util.ArrayList;
  12. import java.util.HashSet;
  13. import java.util.Set;
  14.  
  15. import org.apache.poi.ss.usermodel.Cell;
  16. import org.apache.poi.ss.usermodel.Row;
  17. import org.apache.poi.ss.usermodel.Sheet;
  18. import org.apache.poi.xssf.usermodel.XSSFWorkbook;
  19.  
  20. public class App {
  21. // private static final String CONNECTION_URL_PROPERTY = "jdbc:impala://10.13.85.8:21050";
  22. private static final String CONNECTION_URL_PROPERTY = "jdbc:impala://10.13.53.29:21050/etimad";
  23.  
  24. private static final String JDBC_DRIVER_NAME_PROPERTY = "com.cloudera.impala.jdbc4.Driver";
  25.  
  26. public static void main(String[] args) throws IOException {
  27. String sqlStatement = "select * from etimad.city";
  28. // loadConfiguration();
  29.  
  30. System.out.println("\n=============================================");
  31. System.out.println("Cloudera Impala JDBC Example");
  32. System.out.println("Using Connection URL: " + CONNECTION_URL_PROPERTY);
  33. System.out.println("Running Query: " + sqlStatement);
  34.  
  35. Connection con = null;
  36. XSSFWorkbook workbook = new XSSFWorkbook();
  37. Sheet mainSheet = workbook.createSheet("Report");
  38. try {
  39.  
  40. Class.forName(JDBC_DRIVER_NAME_PROPERTY);
  41. System.out.println("Driver class found (y)");
  42.  
  43. con = DriverManager.getConnection(CONNECTION_URL_PROPERTY);
  44.  
  45. Statement stmt = con.createStatement();
  46.  
  47. ResultSet rs = stmt.executeQuery(sqlStatement);
  48. ResultSetMetaData rsmd = rs.getMetaData();
  49. int numOfCols = rsmd.getColumnCount();
  50. System.out.println("\n== Begin Query Results ======================");
  51. Row headerRow = mainSheet.createRow(0);
  52. // Create cells
  53. for(int i = 0 ; i < numOfCols ; i ++) {
  54. Cell cell = headerRow.createCell(i);
  55. cell.setCellValue(rsmd.getCatalogName(i + 1));
  56. }
  57.  
  58. int i = 1;
  59. while(rs.next()) {
  60. Row row = mainSheet.createRow(i);
  61. for(int j = 0 ; j < numOfCols ; j ++) {
  62. Cell cell = row.createCell(j);
  63. cell.setCellValue(rs.getString(j + 1));
  64. System.out.println(rs.getString(j + 1));
  65. }
  66. }
  67.  
  68. FileOutputStream fileOut = new FileOutputStream("Query Data.xlsx");
  69. workbook.write(fileOut);
  70. fileOut.close();
  71.  
  72. // Closing the workbook
  73. workbook.close();
  74.  
  75.  
  76. System.out.println("== End Query Results =======================\n\n");
  77.  
  78. } catch (SQLException e) {
  79. e.printStackTrace();
  80. } catch (Exception e) {
  81. e.printStackTrace();
  82. } finally {
  83. try {
  84. con.close();
  85. } catch (Exception e) {
  86. // swallow
  87. }
  88. }
  89. }
  90. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement