Advertisement
Guest User

Untitled

a guest
Aug 11th, 2017
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.53 KB | None | 0 0
  1. public static void main(String[] args) {
  2.  
  3. String sqlQuery = "SELECT id,PrimaryIP from IPTable where PrimaryIP != '' limit 100000;";
  4. Connection connection = null;
  5. Statement statement = null;
  6. File oFile = new File("output.txt");
  7. System.out.println(new Date());
  8. try{
  9. List<String> fileData = FileUtils.readLines(new File("input.txt"));
  10. System.out.println("File Data Size : "+fileData.size());
  11.  
  12. Class.forName("com.mysql.jdbc.Driver");
  13. connection = DriverManager.getConnection("jdbc:mysql://localhost/db?user=root&password=pwd");
  14.  
  15. statement = connection.createStatement();
  16. ResultSet resultSet = statement.executeQuery(sqlQuery);
  17.  
  18. System.out.println("Started with MySQL Querying");
  19.  
  20. Map<String, Integer> primaryIPIDMap = new HashMap<String, Integer>();
  21.  
  22. while (resultSet.next()) {
  23. primaryIPIDMap.clear();
  24. int recordID = resultSet.getInt(1);
  25.  
  26. if (resultSet.getString(2).contains("#")) {
  27. String primaryIP[] = resultSet.getString(2).split("#");
  28.  
  29. for (int i = 0; i < primaryIP.length; i++) {
  30. if (primaryIP[i].contains("/")) {
  31. String allIP[] = getAllIP(primaryIP[i]);
  32. for (int allIPi = 0; allIPi < allIP.length; allIPi++) {
  33. primaryIPIDMap.put(allIP[allIPi].intern(), recordID);
  34. }
  35. } else {
  36. primaryIPIDMap.put(primaryIP[i].intern(), recordID);
  37. }
  38. }
  39. } else {
  40. primaryIPIDMap.put(resultSet.getString(2).intern(), recordID);
  41. }
  42.  
  43. Iterator entries = fileData.iterator();
  44. while (entries.hasNext()) {
  45. String t = (String) entries.next();
  46. if (primaryIPIDMap.containsKey(t)) {
  47. FileUtils.writeStringToFile(oFile, recordID + "," + t);
  48. }
  49. }
  50. primaryIPIDMap.clear();
  51. }
  52.  
  53. resultSet.close();
  54. statement.close();
  55. connection.close();
  56. } catch (Exception e) {
  57. e.printStackTrace();
  58. } finally {
  59. try {
  60. if (statement != null)
  61. statement.close();
  62. } catch (Exception se2) {
  63. }
  64. try {
  65. if (connection != null)
  66. connection.close();
  67. } catch (Exception se) {
  68. se.printStackTrace();
  69. }
  70. }
  71.  
  72. System.out.println("Finished");
  73. System.out.println("End Time : "+new Date());
  74. }
  75.  
  76. private static String[] getAllIP(String ip) {
  77. return new SubnetUtils(ip).getInfo().getAllAddresses();
  78. }
  79.  
  80. try ( Connection connection = /*...*/ ;
  81. Statement statement = /*...*/ ) {
  82. try ( BufferedReader in = Files.newBufferedReader(input, charset);
  83. BufferedWriter out = Files.newBufferedWriter(output, charset) ) {
  84. for ( String line = in.readLine(); line != null; line = in.readLine() ) {
  85. // 1. parse line
  86. // 2. query database
  87. // 3. write out result
  88. }
  89. }
  90. }
  91.  
  92. zcat input.gz | java pkg.MyAwesomeFilter | gzip > output.gz
  93.  
  94. 10.0.0.0 -> 010 000 000 000
  95. 192.168.1.11 -> 192 168 001 011
  96.  
  97. List<String> fileData = FileUtils.readLines(new File("input.txt"));
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement