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 ;
2322
2423import java .sql .Connection ;
2524import java .sql .DriverManager ;
2625import java .sql .PreparedStatement ;
2726import java .sql .SQLException ;
2827import java .util .Map ;
29- import java .util .concurrent .LinkedBlockingQueue ;
3028
3129/**
3230 * @author tiezhu
3331 * Company dtstack
3432 * Date 2020/8/27 星期四
3533 */
3634public class MysqlDirtyDataConsumer extends AbstractDirtyDataConsumer {
35+ //TODO 添加batchSize 和 定时任务
3736 private static final long serialVersionUID = -2959753658786001679L ;
3837
3938 private static final String DRIVER_NAME = "com.mysql.jdbc.Driver" ;
4039
40+ private final Object LOCK_STR = new Object ();
41+
4142 private boolean isCreatedTable = false ;
4243
43- private String [] tableField = {"id" , "dirtyData" , "processTime" , "cause" , "field" };
44+ private final String [] tableField = {"id" , "dirtyData" , "processTime" , "cause" , "field" };
4445
4546 private String SQL = "INSERT INTO ? (?, ?, ?, ?) VALUES (?, ?, ?, ?) " ;
4647
@@ -50,14 +51,15 @@ public class MysqlDirtyDataConsumer extends AbstractDirtyDataConsumer {
5051
5152 private String tableName ;
5253
53- private PreparedStatement getStatement (String url ,
54- String userName ,
55- String password ) throws ClassNotFoundException , SQLException {
56- Class .forName (DRIVER_NAME );
54+ private void setStatement (String url ,
55+ String userName ,
56+ String password ) throws ClassNotFoundException , SQLException {
57+ synchronized (LOCK_STR ) {
58+ Class .forName (DRIVER_NAME );
5759
58- connection = DriverManager .getConnection (url , userName , password );
59- statement = connection .prepareStatement (SQL );
60- return statement ;
60+ connection = DriverManager .getConnection (url , userName , password );
61+ statement = connection .prepareStatement (SQL );
62+ }
6163 }
6264
6365 private String quoteIdentifier (String tableName ) {
@@ -66,37 +68,59 @@ private String quoteIdentifier(String tableName) {
6668
6769 /**
6870 * 创建存储脏数据的表
69- * @param tableName 表名
71+ *
72+ * @param tableName 表名
7073 * @return 是否创建成功
7174 * @throws SQLException SQL异常
7275 */
73- private boolean createTable (String tableName ) throws SQLException {
74- String sql =
75- "CREATE TABLE ` " + tableName + "` (" +
76- " `id` int(11) not null AUTO_INCREMENT,\n " +
77- " `dirtyData` varchar(100) DEFAULT NULL,\n " +
78- " `processTime` varchar(100) DEFAULT NULL,\n " +
79- " `cause` date DEFAULT NULL,\n " +
80- " `field` varchar(100) DEFAULT NULL,\n " +
81- " PRIMARY KEY (id)\n " +
82- ") DEFAULT CHARSET=utf8;" ;
83- return statement .execute (sql );
76+ private boolean createTable (String tableName ) {
77+ try {
78+ String defaultTable = "" ;
79+ String sql =
80+ "CREATE TABLE ` " + tableName + "` (" +
81+ " `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 " +
86+ " PRIMARY KEY (id)\n " +
87+ ") DEFAULT CHARSET=utf8;" ;
88+ return statement .execute (sql );
89+ } catch (SQLException e ) {
90+ throw new RuntimeException ("create table error !" , e );
91+ }
8492 }
8593
8694 @ Override
87- public void consume () throws Exception {
95+ public void consume () throws Exception {
8896 if (!isCreatedTable ) {
89- createTable (" tableName" );
97+ createTable (tableName );
9098 }
99+
91100 }
92101
93102 @ Override
94103 public void close () {
95-
104+ try {
105+ if (connection != null && !connection .isValid (1000 )) {
106+ connection .close ();
107+ }
108+
109+ if (statement != null && !statement .isClosed ()) {
110+ statement .close ();
111+ }
112+ } catch (SQLException e ) {
113+ throw new RuntimeException ("close mysql resource error !" );
114+ }
96115 }
97116
98117 @ Override
99- public void init (Map <String , String > properties ) {
100-
118+ public void init (Map <String , String > properties ) throws Exception {
119+ tableName = properties .get ("tableName" );
120+ String userName = properties .get ("userName" );
121+ String password = properties .get ("password" );
122+ String url = properties .get ("url" );
123+ isCreatedTable = Boolean .parseBoolean (properties .get ("isCreatedTable" ));
124+ setStatement (url , userName , password );
101125 }
102126}
0 commit comments