ossiejhmoore

Build Paths Using JDBC

Jan 22nd, 2016
411
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.69 KB | None | 0 0
  1. package deleteme;
  2.  
  3. import java.io.File;
  4. import java.io.FileOutputStream;
  5. import java.io.OutputStream;
  6. import java.sql.Connection;
  7. import java.sql.DriverManager;
  8. import java.sql.PreparedStatement;
  9. import java.sql.ResultSet;
  10. import java.util.ArrayList;
  11. import java.util.HashMap;
  12. import java.util.List;
  13. import java.util.Map;
  14. import org.apache.commons.io.IOUtils;
  15.  
  16. public class DeleteMe31 {
  17.  
  18.     public static void main(String[] args) throws Exception {
  19.         String url = "jdbcurlhere";
  20.         String user = "usernamehere";
  21.         String pass = "passwordhere";
  22.         long start = System.currentTimeMillis();
  23.  
  24.         DriverManager.registerDriver(new oracle.jdbc.OracleDriver());
  25.         Connection con = DriverManager.getConnection(url, user, pass);
  26.         List<Integer> subtypes = new ArrayList<Integer>();
  27.         {
  28.             subtypes.add(144);
  29.             subtypes.add(749);
  30.         }
  31.         int ancestorid = 107297448;
  32.         discover(con, ancestorid, subtypes);
  33.         System.out.printf("time: %s%n", System.currentTimeMillis()-start);
  34.     }
  35.  
  36.     private static void discover(Connection con, Integer ancestor, List<Integer> subtypes) throws Exception {
  37.         final Map<Integer, Row> mapNodeInfo = new HashMap<Integer, Row>();
  38.         final List<Integer> nodeIds = new ArrayList<Integer>();
  39.  
  40.         final PreparedStatement stmtAncestorNodes = con.prepareStatement("select d.dataid, d.name, abs(d.parentid) parentid, d.subtype from rmnausr.dtree d join rmnausr.dtreeancestors da on d.dataid = da.dataid where da.ancestorid = ?");
  41.         stmtAncestorNodes.setInt(1, ancestor);
  42.  
  43.         final ResultSet rs = stmtAncestorNodes.executeQuery();
  44.         int counter = 0;
  45.         long start = System.currentTimeMillis();
  46.         while (rs.next()) {
  47.             final Row row = new Row(rs.getInt("dataid"), rs.getString("name"), rs.getInt("parentid"), rs.getInt("subtype"));
  48.             if (subtypes.contains(row.getSubtype())) {
  49.                 nodeIds.add(row.getDataid());
  50.             }
  51.             mapNodeInfo.put(row.getDataid(), row);
  52.             counter = rs.getRow();
  53.             if ((counter % 10000) == 0) {
  54.                 long rate = (counter/((System.currentTimeMillis()-start)/1000));
  55.                 System.out.printf("ancestor child nodes read: %s items per second: %s%n", counter, rate);
  56.             }
  57.         }
  58.         rs.close();
  59.         stmtAncestorNodes.close();
  60.         con.close();
  61.         System.out.printf("ancestor child nodes read: %s%n", counter);
  62.  
  63.         final File output = new File("output.csv");
  64.         final OutputStream os = new FileOutputStream(output);
  65.         counter = 0;
  66.         for (Integer nodeId : nodeIds) {
  67.             final String line = String.format("%s,\"%s\"",
  68.                     nodeId,
  69.                     getPathFromAncestorFor(nodeId, mapNodeInfo).replaceAll("\"", "\"\"")
  70.             );
  71.             IOUtils.write(line, os);
  72.             if ((++counter % 1000) == 0) {
  73.                 System.out.printf("path nodes written: %s%n", counter);
  74.             }
  75.         }
  76.         System.out.printf("path nodes written: %s%n", counter);
  77.         os.close();
  78.     }
  79.  
  80.     private static String getPathFromAncestorFor(final Integer nodeId, final Map<Integer, Row> mapNodeInfo) {
  81.         final StringBuilder sb = new StringBuilder();
  82.         Row row;
  83.         int nextId = nodeId;
  84.         do {
  85.             row = mapNodeInfo.get(nextId);
  86.             if (null == row) {
  87.                 break;
  88.             }
  89.             if (sb.length() > 0) {
  90.                 sb.insert(0, ":");
  91.             }
  92.             sb.insert(0, row.getName());
  93.             nextId = row.getParentid();
  94.         } while (true);
  95.         return sb.toString();
  96.     }
  97. }
Advertisement
Add Comment
Please, Sign In to add comment