59
59
public class HbaseOutputFormat extends AbstractDtRichOutputFormat <Tuple2 <Boolean , Row >> {
60
60
61
61
private static final Logger LOG = LoggerFactory .getLogger (HbaseOutputFormat .class );
62
- private final List <Row > records = new ArrayList <>();
63
62
private String host ;
64
63
private String zkParent ;
65
64
private String rowkey ;
@@ -79,6 +78,8 @@ public class HbaseOutputFormat extends AbstractDtRichOutputFormat<Tuple2<Boolean
79
78
private transient Connection conn ;
80
79
private transient Table table ;
81
80
private transient ChoreService choreService ;
81
+ private transient List <Row > records ;
82
+ private transient volatile boolean closed = false ;
82
83
/**
83
84
* 批量写入的参数
84
85
*/
@@ -106,6 +107,7 @@ public void configure(Configuration parameters) {
106
107
@ Override
107
108
public void open (int taskNumber , int numTasks ) throws IOException {
108
109
LOG .warn ("---open---" );
110
+ records = new ArrayList <>();
109
111
conf = HBaseConfiguration .create ();
110
112
openConn ();
111
113
table = conn .getTable (TableName .valueOf (tableName ));
@@ -145,10 +147,9 @@ private void initScheduledTask(Long batchWaitInterval) {
145
147
146
148
this .scheduledFuture = this .scheduler .scheduleWithFixedDelay (
147
149
() -> {
148
- synchronized (this ) {
150
+ synchronized (HbaseOutputFormat . this ) {
149
151
if (!records .isEmpty ()) {
150
152
dealBatchOperation (records );
151
- records .clear ();
152
153
}
153
154
}
154
155
}, batchWaitInterval , batchWaitInterval , TimeUnit .MILLISECONDS
@@ -198,7 +199,7 @@ private void openKerberosConn() throws Exception {
198
199
public void writeRecord (Tuple2 <Boolean , Row > record ) {
199
200
if (record .f0 ) {
200
201
if (this .batchSize != 0 ) {
201
- writeBatchRecord (Row . copy ( record .f1 ) );
202
+ writeBatchRecord (record .f1 );
202
203
} else {
203
204
dealInsert (record .f1 );
204
205
}
@@ -210,12 +211,10 @@ public void writeBatchRecord(Row row) {
210
211
// 数据累计到batchSize之后开始处理
211
212
if (records .size () == this .batchSize ) {
212
213
dealBatchOperation (records );
213
- // 添加完数据之后数据清空records
214
- records .clear ();
215
214
}
216
215
}
217
216
218
- protected void dealBatchOperation (List <Row > records ) {
217
+ protected synchronized void dealBatchOperation (List <Row > records ) {
219
218
// A null in the result array means that the call for that action failed, even after retries.
220
219
Object [] results = new Object [records .size ()];
221
220
try {
@@ -245,6 +244,9 @@ protected void dealBatchOperation(List<Row> records) {
245
244
}
246
245
} catch (IOException | InterruptedException e ) {
247
246
LOG .error ("" , e );
247
+ } finally {
248
+ // 添加完数据之后数据清空records
249
+ records .clear ();
248
250
}
249
251
}
250
252
@@ -318,7 +320,12 @@ private Map<String, Object> rowConvertMap(Row record) {
318
320
}
319
321
320
322
@ Override
321
- public void close () throws IOException {
323
+ public synchronized void close () throws IOException {
324
+ if (closed ) {
325
+ return ;
326
+ }
327
+
328
+ closed = true ;
322
329
if (!records .isEmpty ()) {
323
330
dealBatchOperation (records );
324
331
}
@@ -334,7 +341,6 @@ public void close() throws IOException {
334
341
conn .close ();
335
342
conn = null ;
336
343
}
337
-
338
344
}
339
345
340
346
private void fillSyncKerberosConfig (org .apache .hadoop .conf .Configuration config ,
0 commit comments