1919package com .dtstack .flink .sql .dirty .mysql ;
2020
2121import com .dtstack .flink .sql .dirtyManager .consumer .AbstractDirtyDataConsumer ;
22+ import com .dtstack .flink .sql .dirtyManager .entity .DirtyDataEntity ;
2223
2324import java .sql .Connection ;
2425import java .sql .DriverManager ;
2526import java .sql .PreparedStatement ;
2627import java .sql .SQLException ;
28+ import java .sql .Statement ;
29+ import java .text .SimpleDateFormat ;
30+ import java .util .ArrayList ;
31+ import java .util .Arrays ;
32+ import java .util .Collections ;
33+ import java .util .List ;
2734import java .util .Map ;
35+ import java .util .stream .Collectors ;
2836
2937/**
3038 * @author tiezhu
3139 * Company dtstack
3240 * Date 2020/8/27 星期四
3341 */
3442public class MysqlDirtyDataConsumer extends AbstractDirtyDataConsumer {
35- //TODO 添加batchSize 和 定时任务
43+
3644 private static final long serialVersionUID = -2959753658786001679L ;
3745
3846 private static final String DRIVER_NAME = "com.mysql.jdbc.Driver" ;
3947
40- private final Object LOCK_STR = new Object () ;
48+ private static final int CONN_VALID_TIME = 1000 ;
4149
42- private boolean isCreatedTable = false ;
50+ private static final Integer FIELD_NUMBER = 5 ;
4351
44- private final String [] tableField = { "id" , "dirtyData" , "processTime" , "cause" , "field" } ;
52+ private final Object LOCK_STR = new Object () ;
4553
46- private String SQL = "INSERT INTO ? (?, ?, ?, ?) VALUES (?, ?, ?, ?) " ;
54+ private final String [] tableField = { "id" , "dirtyData" , "processTime" , "cause" , "field" } ;
4755
4856 private PreparedStatement statement ;
4957
5058 private Connection connection ;
5159
52- private String tableName ;
60+ private Long batchSize ;
5361
54- private void setStatement (String url ,
55- String userName ,
56- String password ) throws ClassNotFoundException , SQLException {
62+ private void beforeConsume (String url ,
63+ String userName ,
64+ String password ,
65+ String tableName ,
66+ boolean isCreatedTable ) throws ClassNotFoundException , SQLException {
5767 synchronized (LOCK_STR ) {
5868 Class .forName (DRIVER_NAME );
59-
6069 connection = DriverManager .getConnection (url , userName , password );
61- statement = connection .prepareStatement (SQL );
70+
71+ // create table for dirty data
72+ if (!isCreatedTable ) {
73+ createTable (tableName );
74+ }
75+
76+ String insertField = Arrays .stream (tableField )
77+ .map (this ::quoteIdentifier )
78+ .collect (Collectors .joining (", " ));
79+ String insertSql = "INSERT INTO " + quoteIdentifier (tableName )
80+ + "(" + insertField + ") VALUES (?, ?, ?, ?, ?)" ;
81+ statement = connection .prepareStatement (insertSql );
6282 }
6383 }
6484
6585 private String quoteIdentifier (String tableName ) {
66- return "\" " + tableName + "\" " ;
86+ return "`" + tableName + "` " ;
6787 }
6888
6989 /**
7090 * 创建存储脏数据的表
7191 *
7292 * @param tableName 表名
73- * @return 是否创建成功
7493 * @throws SQLException SQL异常
7594 */
76- private boolean createTable (String tableName ) {
95+ private void createTable (String tableName ) throws SQLException {
96+ Statement statement = null ;
7797 try {
78- String defaultTable = "" ;
7998 String sql =
80- "CREATE TABLE ` " + tableName + "` (" +
99+ "CREATE TABLE IF NOT EXISTS \n "
100+ + quoteIdentifier (tableName ) + " (\n " +
81101 " `id` int(11) not null AUTO_INCREMENT,\n " +
82- " `dirtyData` varchar(100 ) DEFAULT NULL,\n " +
83- " `processTime` varchar(100 ) DEFAULT NULL,\n " +
84- " `cause` date DEFAULT NULL,\n " +
85- " `field` varchar(100 ) DEFAULT NULL,\n " +
102+ " `dirtyData` varchar(255 ) DEFAULT NULL,\n " +
103+ " `processTime` varchar(255 ) DEFAULT NULL,\n " +
104+ " `cause` varchar(255) DEFAULT NULL,\n " +
105+ " `field` varchar(255 ) DEFAULT NULL,\n " +
86106 " PRIMARY KEY (id)\n " +
87107 ") DEFAULT CHARSET=utf8;" ;
88- return statement .execute (sql );
108+ statement = connection .createStatement ();
109+ statement .execute (sql );
89110 } catch (SQLException e ) {
90111 throw new RuntimeException ("create table error !" , e );
112+ } finally {
113+ if (statement != null && !statement .isClosed ()) {
114+ statement .close ();
115+ }
91116 }
92117 }
93118
94119 @ Override
95120 public void consume () throws Exception {
96- if (!isCreatedTable ) {
97- createTable (tableName );
121+ DirtyDataEntity entity = queue .take ();
122+ count ++;
123+ List <String > data = new ArrayList <>();
124+ data .add (String .valueOf (count ));
125+ Collections .addAll (data , entity .get ());
126+ for (int i = 0 ; i < FIELD_NUMBER ; i ++) {
127+ statement .setString (i + 1 , data .get (i ));
98128 }
99129
130+ statement .addBatch ();
131+
132+ if (count % batchSize == 0 ) {
133+ statement .executeBatch ();
134+ }
100135 }
101136
102137 @ Override
103138 public void close () {
139+ isRunning .compareAndSet (true , false );
140+
104141 try {
105- if (connection != null && !connection .isValid (1000 )) {
142+ if (connection != null && !connection .isValid (CONN_VALID_TIME )) {
106143 connection .close ();
107144 }
108145
@@ -116,11 +153,18 @@ public void close() {
116153
117154 @ Override
118155 public void init (Map <String , String > properties ) throws Exception {
119- tableName = properties .get ("tableName" );
156+ SimpleDateFormat timeFormat = new SimpleDateFormat ("yyyy_MM_dd_HH_mm_ss" );
157+ String tableName = properties .getOrDefault ("tableName" ,
158+ "DirtyDataFromMysql_" + timeFormat .format (System .currentTimeMillis ()));
120159 String userName = properties .get ("userName" );
121160 String password = properties .get ("password" );
122161 String url = properties .get ("url" );
123- isCreatedTable = Boolean .parseBoolean (properties .get ("isCreatedTable" ));
124- setStatement (url , userName , password );
162+ batchSize = Long .parseLong (properties .getOrDefault ("batchSize" , "10000" ));
163+ errorLimit = Long .parseLong (properties .getOrDefault ("errorLimit" , "1000" ));
164+
165+ boolean isCreatedTable = Boolean .parseBoolean (
166+ properties .getOrDefault ("isCreatedTable" , "false" ));
167+
168+ beforeConsume (url , userName , password , tableName , isCreatedTable );
125169 }
126170}
0 commit comments