summaryrefslogtreecommitdiff
path: root/src/main/java/cn/ac/iie/dao/DataBaseLoad.java
blob: 05b88b305d1ec5eb7cf5cbc9f5fc74890219689a (plain)
1
2
3
4
5
6
7
8
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
package cn.ac.iie.dao;


import cn.ac.iie.bean.ntc.NTC_CONN_RECORD_LOG;
import cn.ac.iie.common.RealtimeCountConfig;
import com.alibaba.fastjson.JSONObject;
import org.apache.log4j.Logger;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.text.SimpleDateFormat;
import java.util.LinkedList;
import java.util.Map;


/**
 * 日志写入clickHouse insert类
 *
 * @author Administrator
 * @create 2018-10-31 12:35
 */
public class DataBaseLoad {
    private static final Logger logger = Logger.getLogger(DataBaseLoad.class);
    private static DbConnect manger = DbConnect.getInstance();
    private Connection connection;
    private PreparedStatement pstm;

    public DataBaseLoad() {
    }

    public void ntcKilledBatchStorage2CH(LinkedList<String> tmpList) {
        String tableName = RealtimeCountConfig.TABLE_KILLED_NAME;
        String sql = "INSERT INTO " + tableName + " VALUES(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
        try {
            connection = manger.getConnection();
            connection.setAutoCommit(false);
            pstm = connection.prepareStatement(sql);
            int nums = 0;
            for (String ntcLog : tmpList) {
                NTC_CONN_RECORD_LOG ntcConnRecordLog = JSONObject.parseObject(ntcLog, NTC_CONN_RECORD_LOG.class);
                pstm.setInt(1, ntcConnRecordLog.getCfg_id());
                pstm.setInt(2, ntcConnRecordLog.getFound_time());
                pstm.setInt(3, ntcConnRecordLog.getRecv_time());
                pstm.setLong(4, ntcConnRecordLog.getOver_id());
                pstm.setString(5, ntcConnRecordLog.getTrans_proto());
                pstm.setString(6, ntcConnRecordLog.getD_ip());
                pstm.setString(7, ntcConnRecordLog.getS_ip());
                pstm.setInt(8, ntcConnRecordLog.getD_port());
                pstm.setInt(9, ntcConnRecordLog.getS_port());
                pstm.setString(10, ntcConnRecordLog.getNest_protocol());
                pstm.setString(11, ntcConnRecordLog.getNest_server_ip());
                pstm.setString(12, ntcConnRecordLog.getNest_client_ip());
                pstm.setInt(13, ntcConnRecordLog.getNest_server_port());
                pstm.setInt(14, ntcConnRecordLog.getNest_client_port());
                pstm.setInt(15, ntcConnRecordLog.getService());
                pstm.setInt(16, ntcConnRecordLog.getEntrance_id());
                pstm.setString(17, ntcConnRecordLog.getCap_ip());
                pstm.setString(18, ntcConnRecordLog.getScene_file());
                pstm.setString(19, ntcConnRecordLog.getInjected_pkt_file());
                pstm.setString(20, ntcConnRecordLog.getNest_addr_list());
                pstm.setInt(21, ntcConnRecordLog.getAction());
                pstm.setString(22, ntcConnRecordLog.getServer_locate());
                pstm.setString(23, ntcConnRecordLog.getClient_locate());
                pstm.setString(24, ntcConnRecordLog.getApp_label());
                pstm.setLong(25, ntcConnRecordLog.getC2s_pkt_num());
                pstm.setLong(26, ntcConnRecordLog.getS2c_pkt_num());
                pstm.setLong(27, ntcConnRecordLog.getC2s_byte_num());
                pstm.setLong(28, ntcConnRecordLog.getS2c_byte_num());

                pstm.setString(29, ntcConnRecordLog.getUser_region());
                pstm.setInt(30, ntcConnRecordLog.getStream_dir());
                pstm.setString(31, ntcConnRecordLog.getAddr_list());
                pstm.setInt(32, ntcConnRecordLog.getCreate_time());
                pstm.setInt(33, ntcConnRecordLog.getLastmtime());

                pstm.addBatch();
                nums++;
                if (nums >= RealtimeCountConfig.BATCH_CHINSERT_KILLED_NUM) {
                    pstm.executeBatch();
                    connection.commit();
                    nums = 0;
                }
            }
            if (nums != 0) {
                pstm.executeBatch();
                connection.commit();
                nums = 0;
            }
        } catch (Exception e) {
            logger.error("+++++++++insert to " + RealtimeCountConfig.TABLE_KILLED_NAME + " Log write failed!!!+++++++++");
            e.printStackTrace();
        } finally {
            manger.clear(pstm, connection);
        }
    }




    public void dfPzFlowBatchStorage2CH(LinkedList<String> tmpList) {
        String tableName = RealtimeCountConfig.TABLE_NAME;
        String sql = "INSERT INTO " + tableName + " VALUES(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
        try {
            connection = manger.getConnection();
            connection.setAutoCommit(false);
            pstm = connection.prepareStatement(sql);
            int nums = 0;
            for (String ntcLog : tmpList) {
                NTC_CONN_RECORD_LOG ntcConnRecordLog = JSONObject.parseObject(ntcLog, NTC_CONN_RECORD_LOG.class);
                pstm.setInt(1, ntcConnRecordLog.getCfg_id());
                pstm.setInt(2, ntcConnRecordLog.getFound_time());
                pstm.setInt(3, ntcConnRecordLog.getRecv_time());
                pstm.setLong(4, ntcConnRecordLog.getOver_id());
                pstm.setString(5, ntcConnRecordLog.getTrans_proto());
                pstm.setString(6, ntcConnRecordLog.getD_ip());
                pstm.setString(7, ntcConnRecordLog.getS_ip());
                pstm.setInt(8, ntcConnRecordLog.getD_port());
                pstm.setInt(9, ntcConnRecordLog.getS_port());
                pstm.setString(10, ntcConnRecordLog.getNest_protocol());
                pstm.setString(11, ntcConnRecordLog.getNest_server_ip());
                pstm.setString(12, ntcConnRecordLog.getNest_client_ip());
                pstm.setInt(13, ntcConnRecordLog.getNest_server_port());
                pstm.setInt(14, ntcConnRecordLog.getNest_client_port());
                pstm.setInt(15, ntcConnRecordLog.getService());
                pstm.setInt(16, ntcConnRecordLog.getEntrance_id());
                pstm.setString(17, ntcConnRecordLog.getCap_ip());
                pstm.setString(18, ntcConnRecordLog.getScene_file());
                pstm.setString(19, ntcConnRecordLog.getInjected_pkt_file());
                pstm.setString(20, ntcConnRecordLog.getNest_addr_list());
                pstm.setInt(21, ntcConnRecordLog.getAction());
                pstm.setString(22, ntcConnRecordLog.getServer_locate());
                pstm.setString(23, ntcConnRecordLog.getClient_locate());
                pstm.setString(24, ntcConnRecordLog.getApp_label());
                pstm.setLong(25, ntcConnRecordLog.getC2s_pkt_num());
                pstm.setLong(26, ntcConnRecordLog.getS2c_pkt_num());
                pstm.setLong(27, ntcConnRecordLog.getC2s_byte_num());
                pstm.setLong(28, ntcConnRecordLog.getS2c_byte_num());

                pstm.setString(29, ntcConnRecordLog.getUser_region());
                pstm.setInt(30, ntcConnRecordLog.getStream_dir());
                pstm.setString(31, ntcConnRecordLog.getAddr_list());
                pstm.setInt(32, ntcConnRecordLog.getCreate_time());
                pstm.setInt(33, ntcConnRecordLog.getLastmtime());

                pstm.addBatch();
                nums++;
                if (nums >= RealtimeCountConfig.BATCH_CHINSERT_NUM) {
                    pstm.executeBatch();
                    connection.commit();
                    nums = 0;
                }
            }
            if (nums != 0) {
                pstm.executeBatch();
                connection.commit();
                nums = 0;
            }
        } catch (Exception e) {
            logger.error("+++++++++insert to " + RealtimeCountConfig.TABLE_NAME + " Log write failed!!!+++++++++");
            e.printStackTrace();
        } finally {
            manger.clear(pstm, connection);
        }
    }

}