繁体   English   中英

HashMap MySQL-最佳实践

[英]HashMap MySQL - Best Practice

我遇到了一个场景,任务是读取一个包含3 Millions IP地址的文件。
有一个MySQL表,其中包含Id,PrimaryIPPrimaryIP可以由多个IP分隔,并用#分隔,此外PrimaryIP还可以包含CIDR IP

因此,总共有8000 records ,每个记录具有multiple IPCIDR IP

现在,我的任务是读取该文件,使用数据库对其进行检查,然后将匹配的IP,ID写入文件。

最初,当我运行我的程序时,我的程序由于以下原因而失败: java.lang.OutOfMemoryError: Java heap space ,因此我将其增加了3GB,但仍然失败,然后将文件拆分为6 subfiles ,每个6 subfiles0.5 Millions个。

为了找到CIDR IP列表,我使用了Apache SubnetUtils

下面是我的代码:

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();
    }  

有人可以告诉我解决此问题的最佳做法。
今天只有300万,明天可能是500万。 我无法继续创建子文件。

我使用解决了问题

  • line-by-line读取输入文件
  • 我没有更改MySQL表结构,因为它在许多地方都具有依赖性,并且table was not designed by me

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM