Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.PreparedStatement;
- import java.sql.ResultSet;
- import java.sql.SQLException;
- import java.sql.Statement;
- import java.util.HashMap;
- import java.util.Map;
- /**
- * Created by piyush.mukati on 24/04/17.
- */
- public class BadgerProcessDataChutiyapaCleaner
- {
- public static final String getpids = "select entity.id as id, entity.processData_id as pid,entity.relayerTopic as topic,entity.type as type, CASE WHEN entity.schemaVersion like \"%.%\" THEN 'minor' ELSE 'major' END as version from entity join process_data on entity.processData_id=process_data.id where entity_type='DART' and process_data.active=1\n";
- public static void main(String[] args) throws SQLException
- {
- Map<String, Integer> map = new HashMap<String, Integer>();
- Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/b1_badger_local", "root", "");
- ResultSet rs = conn.createStatement().executeQuery(getpids);
- int removedCnt = 0;
- while (rs.next()) {
- if (map.containsKey(getKey(rs))) {
- System.out.println("key " + getKey(rs));
- updateDb(conn, rs.getInt("pid"), rs.getInt("id"), map.get(getKey(rs)));
- updateOpDb(conn, rs.getInt("pid"), rs.getInt("id"), map.get(getKey(rs)));
- removedCnt++;
- }
- else {
- map.put(getKey(rs), rs.getInt("id"));
- }
- }
- System.out.println("total active" + map.size());
- System.out.println("total removed" + removedCnt);
- rs.close();
- }
- private static void updateDb(Connection conn, Integer oldPid, Integer oldEid, Integer newEid) throws SQLException
- {
- String q = "";
- // adding new dependency
- ResultSet rs = conn.createStatement().executeQuery(String.format("select process_id from process_dependent where parent_id='%s'", oldEid));
- while (rs.next()) {
- try {
- q = String.format("update process_dependent set parent_id ='%s' where parent_id='%s' and process_id='%s'", oldEid, newEid, rs.getInt("process_id"));
- conn.createStatement().executeUpdate(q);
- System.out.println("done: " + q);
- }
- catch (com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException e) {
- System.out.println("failed: " + q);
- }
- }
- q = String.format("update process_data set active=0 where id=%s", oldPid);
- conn.createStatement().executeUpdate(q);
- System.out.println("done: " + q);
- }
- private static void updateOpDb(Connection conn, Integer oldPid, Integer oldEid, Integer newEid) throws SQLException
- {
- String q = "";
- // adding new dependency
- ResultSet rs = conn.createStatement().executeQuery(String.format("select process_id from process_optional_dependent where optional_parent_id='%s'", oldEid));
- while (rs.next()) {
- try {
- q = String.format("update process_optional_dependent set optional_parent_id ='%s' where optional_parent_id='%s' and process_id='%s'", oldEid, newEid, rs.getInt("process_id"));
- conn.createStatement().executeUpdate(q);
- System.out.println("done: " + q);
- }
- catch (Exception e) {
- System.out.println("failed: " + q);
- }
- }
- q = String.format("update process_data set active=0 where id=%s", oldPid);
- conn.createStatement().executeUpdate(q);
- System.out.println("done: " + q);
- }
- private static String getKey(ResultSet rs) throws SQLException
- {
- return rs.getString("type") + rs.getString("version") + rs.getString("topic");
- }
- }
Add Comment
Please, Sign In to add comment