29
29
import org .apache .kudu .client .KuduTable ;
30
30
import org .apache .kudu .client .PartialRow ;
31
31
import org .apache .kudu .client .SessionConfiguration ;
32
+ import org .apache .kudu .client .Upsert ;
32
33
import org .slf4j .Logger ;
33
34
import org .slf4j .LoggerFactory ;
34
35
@@ -45,26 +46,25 @@ public class KuduOutputFormat
45
46
46
47
private static final Logger LOGGER = LoggerFactory .getLogger (KuduOutputFormat .class );
47
48
48
- private String kuduMaster ;
49
- private String kuduTableName ;
49
+ public static final long TIMEOUTMS = 18000 ;
50
+ public static final long SESSIONTIMEOUTMS = 100000 ;
51
+
52
+ private final String kuduMaster ;
53
+ private final String kuduTableName ;
54
+ private final KuduSinkConfig .SaveMode saveMode ;
50
55
private KuduClient kuduClient ;
51
56
private KuduSession kuduSession ;
52
57
private KuduTable kuduTable ;
53
- public static final long TIMEOUTMS = 18000 ;
54
- public static final long SESSIONTIMEOUTMS = 100000 ;
58
+
55
59
public KuduOutputFormat (KuduSinkConfig kuduSinkConfig ) {
56
60
this .kuduMaster = kuduSinkConfig .getKuduMaster ();
57
61
this .kuduTableName = kuduSinkConfig .getKuduTableName ();
62
+ this .saveMode = kuduSinkConfig .getSaveMode ();
58
63
init ();
59
64
}
60
65
61
- public void write (SeaTunnelRow element ) {
62
-
63
- Insert insert = kuduTable .newInsert ();
64
- Schema schema = kuduTable .getSchema ();
65
-
66
+ private void transform (PartialRow row , SeaTunnelRow element , Schema schema ) {
66
67
int columnCount = schema .getColumnCount ();
67
- PartialRow row = insert .getRow ();
68
68
for (int columnIndex = 0 ; columnIndex < columnCount ; columnIndex ++) {
69
69
ColumnSchema col = schema .getColumnByIndex (columnIndex );
70
70
try {
@@ -114,24 +114,54 @@ public void write(SeaTunnelRow element) {
114
114
throw new IllegalArgumentException ("Unsupported column type: " + col .getType ());
115
115
}
116
116
} catch (ClassCastException e ) {
117
- e .printStackTrace ();
118
117
throw new IllegalArgumentException (
119
118
"Value type does not match column type " + col .getType () +
120
119
" for column " + col .getName ());
121
120
}
122
121
123
122
}
123
+ }
124
124
125
+ private void upsert (SeaTunnelRow element ) {
126
+ Upsert upsert = kuduTable .newUpsert ();
127
+ Schema schema = kuduTable .getSchema ();
128
+ PartialRow row = upsert .getRow ();
129
+ transform (row , element , schema );
130
+ try {
131
+ kuduSession .apply (upsert );
132
+ } catch (KuduException e ) {
133
+ LOGGER .error ("Failed to upsert." , e );
134
+ throw new RuntimeException ("Failed to upsert." , e );
135
+ }
136
+ }
137
+
138
+ private void insert (SeaTunnelRow element ) {
139
+ Insert insert = kuduTable .newInsert ();
140
+ Schema schema = kuduTable .getSchema ();
141
+ PartialRow row = insert .getRow ();
142
+ transform (row , element , schema );
125
143
try {
126
144
kuduSession .apply (insert );
127
145
} catch (KuduException e ) {
128
- LOGGER .warn ( "kudu session insert data fail ." , e );
129
- throw new RuntimeException ("kudu session insert data fail ." , e );
146
+ LOGGER .error ( "Failed to insert." , e );
147
+ throw new RuntimeException ("Failed to insert." , e );
130
148
}
149
+ }
131
150
151
+ public void write (SeaTunnelRow element ) {
152
+ switch (saveMode ) {
153
+ case APPEND :
154
+ insert (element );
155
+ break ;
156
+ case OVERWRITE :
157
+ upsert (element );
158
+ break ;
159
+ default :
160
+ throw new IllegalArgumentException (String .format ("Unsupported saveMode: %s." , saveMode .name ()));
161
+ }
132
162
}
133
163
134
- public void init () {
164
+ private void init () {
135
165
KuduClient .KuduClientBuilder kuduClientBuilder = new
136
166
KuduClient .KuduClientBuilder (kuduMaster );
137
167
kuduClientBuilder .defaultOperationTimeoutMs (TIMEOUTMS );
@@ -142,19 +172,19 @@ public void init() {
142
172
try {
143
173
kuduTable = kuduClient .openTable (kuduTableName );
144
174
} catch (KuduException e ) {
145
- LOGGER .warn ("Failed to initialize the Kudu client." , e );
175
+ LOGGER .error ("Failed to initialize the Kudu client." , e );
146
176
throw new RuntimeException ("Failed to initialize the Kudu client." , e );
147
177
}
148
- LOGGER .info ("The Kudu client is successfully initialized" , kuduMaster , kuduClient );
178
+ LOGGER .info ("The Kudu client for Master: {} is initialized successfully. " , kuduMaster );
149
179
}
150
180
151
181
public void closeOutputFormat () {
152
182
if (kuduClient != null ) {
153
183
try {
154
184
kuduClient .close ();
155
185
kuduSession .close ();
156
- } catch (KuduException e ) {
157
- LOGGER .warn ("Kudu Client close failed ." , e );
186
+ } catch (KuduException ignored ) {
187
+ LOGGER .warn ("Failed to close Kudu Client ." , ignored );
158
188
} finally {
159
189
kuduClient = null ;
160
190
kuduSession = null ;
0 commit comments