MySQLUtil.java 5.77 KB
Newer Older
wang-jinfeng committed
1 2
package mobvista.dmp.util;

3
import mobvista.dmp.datasource.rtdmp.entity.KV;
WangJinfeng committed
4
import org.apache.commons.lang3.StringUtils;
5

wang-jinfeng committed
6 7
import java.sql.*;
import java.text.SimpleDateFormat;
8
import java.util.Date;
wang-jinfeng committed
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68

/**
 * @package: mobvista.dmp.util
 * @author: wangjf
 * @date: 2019-11-27
 * @time: 18:26
 * @email: jinfeng.wang@mobvista.com
 * @phone: 152-1062-7698
 */
public class MySQLUtil {

    static SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd");
    static SimpleDateFormat sdf2 = new SimpleDateFormat("yyyyMMdd");

    static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";
    static final String URL = PropertyUtil.getProperty("config.properties", "mysql.dmp.url");
    static final String DB_URL = "jdbc:mysql://" + URL + ":3306/dmp?useUnicode=true&characterEncoding=utf8&useSSL=false";
    static final String USER = PropertyUtil.getProperty("config.properties", "mysql.dmp.user");
    static final String PASS = PropertyUtil.getProperty("config.properties", "mysql.dmp.password");

    public static String getLastPartition(String dbName, String tbName) {
        Connection conn = null;
        Statement stmt = null;
        String partition = "";

        try {
            Class.forName(JDBC_DRIVER);
            conn = DriverManager.getConnection(DB_URL, USER, PASS);

            stmt = conn.createStatement();
            String sql;
            //  last partition
            sql = "SELECT part FROM table_info WHERE db_name = '" + dbName + "' AND tb_name = '" + tbName + "' AND flag = 1 ORDER BY part DESC LIMIT 1";
            ResultSet rs = stmt.executeQuery(sql);
            while (rs.next()) {
                partition = rs.getString("part");
            }
            rs.close();
            stmt.close();
            conn.close();
        } catch (Exception se) {
            se.printStackTrace();
        } finally {
            try {
                if (stmt != null) {
                    stmt.close();
                }
            } catch (SQLException ignored) {
                try {
                    if (conn != null) {
                        conn.close();
                    }
                } catch (SQLException se) {
                    se.printStackTrace();
                }
            }
        }
        return partition;
    }

69 70 71 72 73 74 75 76 77 78 79 80 81
    public static KV getPartitionTime(String dbName, String tbName) {
        Connection conn = null;
        Statement stmt = null;
        String partition = "";
        Timestamp utime = new Timestamp(0L);

        try {
            Class.forName(JDBC_DRIVER);
            conn = DriverManager.getConnection(DB_URL, USER, PASS);

            stmt = conn.createStatement();
            String sql;
            //  last partition
82
            sql = "SELECT part,utime FROM dmp.table_info WHERE db_name = '" + dbName + "' AND tb_name = '" + tbName + "' AND flag = 1 ORDER BY part DESC LIMIT 1";
WangJinfeng committed
83
            System.out.println("getPartitionTime,SQL --->>> " + sql);
84 85 86 87
            ResultSet rs = stmt.executeQuery(sql);
            while (rs.next()) {
                partition = rs.getString("part");
                utime = rs.getTimestamp("utime");
WangJinfeng committed
88
                System.out.println("partition:" + partition + ",utime:" + utime);
89
            }
WangJinfeng committed
90 91 92
            if (StringUtils.isNotBlank(partition)) {
                System.out.println("getPartitionTime Null,SQL --->>> " + sql);
            }
WangJinfeng committed
93
            System.out.println("getPartitionTime,SQL --->>> " + sql);
94 95 96 97
            rs.close();
            stmt.close();
            conn.close();
        } catch (Exception se) {
WangJinfeng committed
98
            System.out.println(se.getMessage());
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116
        } finally {
            try {
                if (stmt != null) {
                    stmt.close();
                }
            } catch (SQLException ignored) {
                try {
                    if (conn != null) {
                        conn.close();
                    }
                } catch (SQLException se) {
                    se.printStackTrace();
                }
            }
        }
        return new KV(partition, String.valueOf(utime.getTime() / 1000 + 28800));
    }

wang-jinfeng committed
117 118 119 120 121 122 123 124 125
    public static boolean update(String dbName, String tbName, String partition) {
        Connection conn = null;
        boolean flag = false;

        try {
            Class.forName(JDBC_DRIVER);
            conn = DriverManager.getConnection(DB_URL, USER, PASS);
            // update last partition
            String sql = "REPLACE INTO table_info(db_name, tb_name, part, flag) VALUES('" + dbName + "','" + tbName + "','" + partition + "', true)";
126 127 128 129
            //  String lastPart = DateUtil.getDayByString(partition, "yyyyMMdd", -1);
            //  String lastSql = "REPLACE INTO table_info(db_name, tb_name, part, flag) VALUES('" + dbName + "','" + tbName + "','" + lastPart + "', false)";
            //  + conn.prepareStatement(lastSql).executeUpdate() == 2
            if (conn.prepareStatement(sql).executeUpdate() == 1) {
wang-jinfeng committed
130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149
                flag = true;
            } else {
                flag = false;
            }
            conn.close();
        } catch (Exception se) {
            se.printStackTrace();
        } finally {
            try {
                if (conn != null) {
                    conn.close();
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        return flag;
    }

    public static void main(String[] args) {
150
        KV kv = getPartitionTime("dwh", "audience_merge_v1");
151 152 153 154 155

        long nowTime = DateUtil.parse(DateUtil.format(new Date(), "yyyy-MM-dd HH:mm:ss"), "yyyy-MM-dd HH:mm:ss").getTime() / 1000;

        System.out.println(kv.getV());
        System.out.println(Long.parseLong(kv.getV()) > nowTime - 1200);
156 157
        //  System.out.println(getLastPartition("dwh", "audience_merge_v1"));
        //  System.out.println(update("dwh", "audience_merge_v1", "2021072913"));
wang-jinfeng committed
158 159
    }
}