10
10
import org .apache .kudu .Schema ;
11
11
import org .apache .kudu .Type ;
12
12
import org .apache .kudu .client .*;
13
+ import org .apache .kudu .shaded .org .checkerframework .checker .units .qual .K ;
13
14
import org .slf4j .Logger ;
14
15
import org .slf4j .LoggerFactory ;
16
+ import sun .rmi .runtime .Log ;
15
17
16
18
import java .nio .charset .Charset ;
17
19
import java .util .*;
20
+ import java .util .concurrent .SynchronousQueue ;
21
+ import java .util .concurrent .ThreadFactory ;
22
+ import java .util .concurrent .ThreadPoolExecutor ;
23
+ import java .util .concurrent .TimeUnit ;
18
24
import java .util .concurrent .atomic .AtomicInteger ;
19
25
20
26
/**
@@ -47,10 +53,10 @@ public static KuduClient getKuduClient(String kuduConfig) {
47
53
Map <String , Object > conf = Kudu11xHelper .getKuduConfiguration (kuduConfig );
48
54
KuduClient kuduClient = null ;
49
55
try {
50
- String masterAddress = (String )conf .get (Key .KUDU_MASTER );
56
+ String masterAddress = (String ) conf .get (Key .KUDU_MASTER );
51
57
kuduClient = new KuduClient .KuduClientBuilder (masterAddress )
52
58
.defaultAdminOperationTimeoutMs ((Long ) conf .get (Key .KUDU_ADMIN_TIMEOUT ))
53
- .defaultOperationTimeoutMs ((Long )conf .get (Key .KUDU_SESSION_TIMEOUT ))
59
+ .defaultOperationTimeoutMs ((Long ) conf .get (Key .KUDU_SESSION_TIMEOUT ))
54
60
.build ();
55
61
} catch (Exception e ) {
56
62
throw DataXException .asDataXException (Kudu11xWriterErrorcode .GET_KUDU_CONNECTION_ERROR , e );
@@ -106,17 +112,17 @@ public static void createTable(Configuration configuration) {
106
112
throw DataXException .asDataXException (Kudu11xWriterErrorcode .GREATE_KUDU_TABLE_ERROR , e );
107
113
} finally {
108
114
AtomicInteger i = new AtomicInteger (5 );
109
- while (i .get ()> 0 ) {
115
+ while (i .get () > 0 ) {
110
116
try {
111
- if (kuduClient .isCreateTableDone (tableName )){
117
+ if (kuduClient .isCreateTableDone (tableName )) {
112
118
Kudu11xHelper .closeClient (kuduClient );
113
- LOG .info ("Table " + tableName +" is created!" );
119
+ LOG .info ("Table " + tableName + " is created!" );
114
120
break ;
115
121
}
116
122
i .decrementAndGet ();
117
123
LOG .error ("timeout!" );
118
- } catch (KuduException e ) {
119
- LOG .info ("Wait for the table to be created..... " + i );
124
+ } catch (KuduException e ) {
125
+ LOG .info ("Wait for the table to be created..... " + i );
120
126
try {
121
127
Thread .sleep (1000L );
122
128
} catch (InterruptedException ex ) {
@@ -135,6 +141,44 @@ public static void createTable(Configuration configuration) {
135
141
}
136
142
}
137
143
144
+ public static ThreadPoolExecutor createRowAddThreadPool (int coreSize ) {
145
+ return new ThreadPoolExecutor (coreSize ,
146
+ coreSize ,
147
+ 60L ,
148
+ TimeUnit .SECONDS ,
149
+ new SynchronousQueue <Runnable >(),
150
+ new ThreadFactory () {
151
+ private final ThreadGroup group = System .getSecurityManager () == null ? Thread .currentThread ().getThreadGroup () : System .getSecurityManager ().getThreadGroup ();
152
+ private final AtomicInteger threadNumber = new AtomicInteger (1 );
153
+
154
+ @ Override
155
+ public Thread newThread (Runnable r ) {
156
+ Thread t = new Thread (group , r ,
157
+ "pool-kudu_rows_add-thread-" + threadNumber .getAndIncrement (),
158
+ 0 );
159
+ if (t .isDaemon ())
160
+ t .setDaemon (false );
161
+ if (t .getPriority () != Thread .NORM_PRIORITY )
162
+ t .setPriority (Thread .NORM_PRIORITY );
163
+ return t ;
164
+ }
165
+ }, new ThreadPoolExecutor .CallerRunsPolicy ());
166
+ }
167
+
168
+ public static List <List <Configuration >> getColumnLists (List <Configuration > columns ) {
169
+ int quota = 8 ;
170
+ int num = (columns .size () - 1 ) / quota + 1 ;
171
+ int gap = columns .size () / num ;
172
+ List <List <Configuration >> columnLists = new ArrayList <>(num );
173
+ for (int j = 0 ; j < num - 1 ; j ++) {
174
+ List <Configuration > destList = new ArrayList <>(columns .subList (j * gap , (j + 1 ) * gap ));
175
+ columnLists .add (destList );
176
+ }
177
+ List <Configuration > destList = new ArrayList <>(columns .subList (gap * (num - 1 ), columns .size ()));
178
+ columnLists .add (destList );
179
+ return columnLists ;
180
+ }
181
+
138
182
public static boolean isTableExists (Configuration configuration ) {
139
183
String tableName = configuration .getString (Key .TABLE );
140
184
String kuduConfig = configuration .getString (Key .KUDU_CONFIG );
@@ -154,7 +198,7 @@ public static void closeClient(KuduClient kuduClient) {
154
198
kuduClient .close ();
155
199
}
156
200
} catch (KuduException e ) {
157
- LOG .warn ("kudu client is not gracefully closed !" );
201
+ LOG .warn ("The \" kudu client\" was not stopped gracefully. !" );
158
202
159
203
}
160
204
@@ -172,8 +216,8 @@ public static Schema getSchema(Configuration configuration) {
172
216
173
217
String type = "BIGINT" .equals (column .getNecessaryValue (Key .TYPE , Kudu11xWriterErrorcode .REQUIRED_VALUE ).toUpperCase ()) ||
174
218
"LONG" .equals (column .getNecessaryValue (Key .TYPE , Kudu11xWriterErrorcode .REQUIRED_VALUE ).toUpperCase ()) ?
175
- "INT64" : "INT" .equals (column .getNecessaryValue (Key .TYPE , Kudu11xWriterErrorcode .REQUIRED_VALUE ).toUpperCase ())?
176
- "INT32" : column .getNecessaryValue (Key .TYPE , Kudu11xWriterErrorcode .REQUIRED_VALUE ).toUpperCase ();
219
+ "INT64" : "INT" .equals (column .getNecessaryValue (Key .TYPE , Kudu11xWriterErrorcode .REQUIRED_VALUE ).toUpperCase ()) ?
220
+ "INT32" : column .getNecessaryValue (Key .TYPE , Kudu11xWriterErrorcode .REQUIRED_VALUE ).toUpperCase ();
177
221
String name = column .getNecessaryValue (Key .NAME , Kudu11xWriterErrorcode .REQUIRED_VALUE );
178
222
Boolean key = column .getBool (Key .PRIMARYKEY , false );
179
223
String encoding = column .getString (Key .ENCODING , Constant .ENCODING ).toUpperCase ();
@@ -194,9 +238,9 @@ public static Schema getSchema(Configuration configuration) {
194
238
return schema ;
195
239
}
196
240
197
- public static Integer getPrimaryKeyIndexUntil (List <Configuration > columns ){
241
+ public static Integer getPrimaryKeyIndexUntil (List <Configuration > columns ) {
198
242
int i = 0 ;
199
- while ( i < columns .size () ) {
243
+ while (i < columns .size ()) {
200
244
Configuration col = columns .get (i );
201
245
if (!col .getBool (Key .PRIMARYKEY , false )) {
202
246
break ;
@@ -244,6 +288,7 @@ public static void setTablePartition(Configuration configuration,
244
288
}
245
289
246
290
public static void validateParameter (Configuration configuration ) {
291
+ LOG .info ("Start validating parameters!" );
247
292
configuration .getNecessaryValue (Key .KUDU_CONFIG , Kudu11xWriterErrorcode .REQUIRED_VALUE );
248
293
configuration .getNecessaryValue (Key .TABLE , Kudu11xWriterErrorcode .REQUIRED_VALUE );
249
294
String encoding = configuration .getString (Key .ENCODING , Constant .DEFAULT_ENCODING );
@@ -268,7 +313,39 @@ public static void validateParameter(Configuration configuration) {
268
313
269
314
Boolean isSkipFail = configuration .getBool (Key .SKIP_FAIL , false );
270
315
configuration .set (Key .SKIP_FAIL , isSkipFail );
271
- LOG .info ("==validate parameter complete!" );
316
+ List <Configuration > columns = configuration .getListConfiguration (Key .COLUMN );
317
+ List <Configuration > goalColumns = new ArrayList <>();
318
+ //column参数验证
319
+ int indexFlag = 0 ;
320
+ boolean primaryKey = true ;
321
+ int primaryKeyFlag = 0 ;
322
+ for (int i = 0 ; i < columns .size (); i ++) {
323
+ Configuration col = columns .get (i );
324
+ String index = col .getString (Key .INDEX );
325
+ if (index == null ) {
326
+ index = String .valueOf (i );
327
+ col .set (Key .INDEX , index );
328
+ indexFlag ++;
329
+ }
330
+ if (primaryKey != col .getBool (Key .PRIMARYKEY , false )){
331
+ primaryKey = col .getBool (Key .PRIMARYKEY , false );
332
+ primaryKeyFlag ++;
333
+ }
334
+ goalColumns .add (col );
335
+ }
336
+ if (indexFlag != 0 && indexFlag != columns .size ()) {
337
+ throw DataXException .asDataXException (Kudu11xWriterErrorcode .ILLEGAL_VALUE ,
338
+ "\" index\" either has values for all of them, or all of them are null!" );
339
+ }
340
+ if (primaryKeyFlag > 1 ){
341
+ throw DataXException .asDataXException (Kudu11xWriterErrorcode .ILLEGAL_VALUE ,
342
+ "\" primaryKey\" must be written in the front!" );
343
+ }
344
+ configuration .set (Key .COLUMN , goalColumns );
345
+ // LOG.info("------------------------------------");
346
+ // LOG.info(configuration.toString());
347
+ // LOG.info("------------------------------------");
348
+ LOG .info ("validate parameter complete!" );
272
349
}
273
350
274
351
public static void truncateTable (Configuration configuration ) {
0 commit comments