Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public static void main(String[] args) {
- String sqlQuery = "SELECT id,PrimaryIP from IPTable where PrimaryIP != '' limit 100000;";
- Connection connection = null;
- Statement statement = null;
- File oFile = new File("output.txt");
- System.out.println(new Date());
- try{
- List<String> fileData = FileUtils.readLines(new File("input.txt"));
- System.out.println("File Data Size : "+fileData.size());
- Class.forName("com.mysql.jdbc.Driver");
- connection = DriverManager.getConnection("jdbc:mysql://localhost/db?user=root&password=pwd");
- statement = connection.createStatement();
- ResultSet resultSet = statement.executeQuery(sqlQuery);
- System.out.println("Started with MySQL Querying");
- Map<String, Integer> primaryIPIDMap = new HashMap<String, Integer>();
- while (resultSet.next()) {
- primaryIPIDMap.clear();
- int recordID = resultSet.getInt(1);
- if (resultSet.getString(2).contains("#")) {
- String primaryIP[] = resultSet.getString(2).split("#");
- for (int i = 0; i < primaryIP.length; i++) {
- if (primaryIP[i].contains("/")) {
- String allIP[] = getAllIP(primaryIP[i]);
- for (int allIPi = 0; allIPi < allIP.length; allIPi++) {
- primaryIPIDMap.put(allIP[allIPi].intern(), recordID);
- }
- } else {
- primaryIPIDMap.put(primaryIP[i].intern(), recordID);
- }
- }
- } else {
- primaryIPIDMap.put(resultSet.getString(2).intern(), recordID);
- }
- Iterator entries = fileData.iterator();
- while (entries.hasNext()) {
- String t = (String) entries.next();
- if (primaryIPIDMap.containsKey(t)) {
- FileUtils.writeStringToFile(oFile, recordID + "," + t);
- }
- }
- primaryIPIDMap.clear();
- }
- resultSet.close();
- statement.close();
- connection.close();
- } catch (Exception e) {
- e.printStackTrace();
- } finally {
- try {
- if (statement != null)
- statement.close();
- } catch (Exception se2) {
- }
- try {
- if (connection != null)
- connection.close();
- } catch (Exception se) {
- se.printStackTrace();
- }
- }
- System.out.println("Finished");
- System.out.println("End Time : "+new Date());
- }
- private static String[] getAllIP(String ip) {
- return new SubnetUtils(ip).getInfo().getAllAddresses();
- }
- try ( Connection connection = /*...*/ ;
- Statement statement = /*...*/ ) {
- try ( BufferedReader in = Files.newBufferedReader(input, charset);
- BufferedWriter out = Files.newBufferedWriter(output, charset) ) {
- for ( String line = in.readLine(); line != null; line = in.readLine() ) {
- // 1. parse line
- // 2. query database
- // 3. write out result
- }
- }
- }
- zcat input.gz | java pkg.MyAwesomeFilter | gzip > output.gz
- 10.0.0.0 -> 010 000 000 000
- 192.168.1.11 -> 192 168 001 011
- List<String> fileData = FileUtils.readLines(new File("input.txt"));
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement