-
Notifications
You must be signed in to change notification settings - Fork 3.4k
/
SimpleMapping.java
345 lines (299 loc) · 13.1 KB
/
SimpleMapping.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
/*! ******************************************************************************
*
* Pentaho Data Integration
*
* Copyright (C) 2002-2017 by Hitachi Vantara : http://www.pentaho.com
*
*******************************************************************************
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
******************************************************************************/
package org.pentaho.di.trans.steps.simplemapping;
import java.util.ArrayList;
import java.util.List;
import org.pentaho.di.core.Const;
import org.pentaho.di.core.exception.KettleException;
import org.pentaho.di.core.exception.KettleStepException;
import org.pentaho.di.core.row.RowMetaInterface;
import org.pentaho.di.i18n.BaseMessages;
import org.pentaho.di.trans.RowProducer;
import org.pentaho.di.trans.StepWithMappingMeta;
import org.pentaho.di.trans.Trans;
import org.pentaho.di.trans.TransMeta;
import org.pentaho.di.trans.TransMeta.TransformationType;
import org.pentaho.di.trans.step.BaseStep;
import org.pentaho.di.trans.step.RowListener;
import org.pentaho.di.trans.step.StepDataInterface;
import org.pentaho.di.trans.step.StepInterface;
import org.pentaho.di.trans.step.StepMeta;
import org.pentaho.di.trans.step.StepMetaInterface;
import org.pentaho.di.trans.steps.TransStepUtil;
import org.pentaho.di.trans.steps.mapping.MappingValueRename;
import org.pentaho.di.trans.steps.mappinginput.MappingInput;
import org.pentaho.di.trans.steps.mappingoutput.MappingOutput;
/**
* Execute a mapping: a re-usuable transformation
*
* @author Matt
* @since 22-nov-2005
*/
public class SimpleMapping extends BaseStep implements StepInterface {
private static Class<?> PKG = SimpleMappingMeta.class; // for i18n purposes, needed by Translator2!!
private SimpleMappingMeta meta;
private SimpleMappingData data;
public SimpleMapping( StepMeta stepMeta, StepDataInterface stepDataInterface, int copyNr, TransMeta transMeta,
Trans trans ) {
super( stepMeta, stepDataInterface, copyNr, transMeta, trans );
}
/**
* Process a single row. In our case, we send one row of data to a piece of transformation. In the transformation, we
* look up the MappingInput step to send our rows to it. As a consequence, for the time being, there can only be one
* MappingInput and one MappingOutput step in the Mapping.
*/
public boolean processRow( StepMetaInterface smi, StepDataInterface sdi ) throws KettleException {
meta = (SimpleMappingMeta) smi;
setData( (SimpleMappingData) sdi );
SimpleMappingData simpleMappingData = getData();
try {
if ( first ) {
first = false;
simpleMappingData.wasStarted = true;
// Rows read are injected into the one available Mapping Input step
//
String mappingInputStepname = simpleMappingData.mappingInput.getStepname();
RowProducer rowProducer = simpleMappingData.mappingTrans.addRowProducer( mappingInputStepname, 0 );
simpleMappingData.rowDataInputMapper = new RowDataInputMapper( meta.getInputMapping(), rowProducer );
// Rows produced by the mapping are read and passed on.
//
String mappingOutputStepname = simpleMappingData.mappingOutput.getStepname();
StepInterface outputStepInterface = simpleMappingData.mappingTrans.findStepInterface( mappingOutputStepname, 0 );
RowOutputDataMapper outputDataMapper =
new RowOutputDataMapper( meta.getInputMapping(), meta.getOutputMapping(), new PutRowInterface() {
@Override
public void putRow( RowMetaInterface rowMeta, Object[] rowData ) throws KettleStepException {
SimpleMapping.this.putRow( rowMeta, rowData );
}
} );
outputStepInterface.addRowListener( outputDataMapper );
// Start the mapping/sub-transformation threads
//
simpleMappingData.mappingTrans.startThreads();
}
// The data we read we pass to the mapping
//
Object[] row = getRow();
boolean rowWasPut = false;
if ( row != null ) {
while ( !( data.mappingTrans.isFinishedOrStopped() || rowWasPut ) ) {
rowWasPut = data.rowDataInputMapper.putRow( getInputRowMeta(), row );
}
}
if ( !rowWasPut ) {
simpleMappingData.rowDataInputMapper.finished();
simpleMappingData.mappingTrans.waitUntilFinished();
setOutputDone();
return false;
}
return true;
} catch ( Throwable t ) {
// Some unexpected situation occurred.
// Better to stop the mapping transformation.
//
if ( simpleMappingData.mappingTrans != null ) {
simpleMappingData.mappingTrans.stopAll();
}
// Forward the exception...
//
throw new KettleException( t );
}
}
public void prepareMappingExecution() throws KettleException {
SimpleMappingData simpleMappingData = getData();
// Create the transformation from meta-data...
simpleMappingData.mappingTrans = new Trans( simpleMappingData.mappingTransMeta, this );
// Set the parameters values in the mapping.
//
StepWithMappingMeta.activateParams( simpleMappingData.mappingTrans, simpleMappingData.mappingTrans, this, simpleMappingData.mappingTransMeta.listParameters(),
meta.getMappingParameters().getVariable(), meta.getMappingParameters().getInputField() );
if ( simpleMappingData.mappingTransMeta.getTransformationType() != TransformationType.Normal ) {
simpleMappingData.mappingTrans.getTransMeta().setUsingThreadPriorityManagment( false );
}
// Leave a path up so that we can set variables in sub-transformations...
//
simpleMappingData.mappingTrans.setParentTrans( getTrans() );
// Pass down the safe mode flag to the mapping...
//
simpleMappingData.mappingTrans.setSafeModeEnabled( getTrans().isSafeModeEnabled() );
// Pass down the metrics gathering flag:
//
simpleMappingData.mappingTrans.setGatheringMetrics( getTrans().isGatheringMetrics() );
// Also set the name of this step in the mapping transformation for logging
// purposes
//
simpleMappingData.mappingTrans.setMappingStepName( getStepname() );
initServletConfig();
// We launch the transformation in the processRow when the first row is
// received.
// This will allow the correct variables to be passed.
// Otherwise the parent is the init() thread which will be gone once the
// init is done.
//
try {
simpleMappingData.mappingTrans.prepareExecution( getTrans().getArguments() );
} catch ( KettleException e ) {
throw new KettleException( BaseMessages.getString( PKG,
"SimpleMapping.Exception.UnableToPrepareExecutionOfMapping" ), e );
}
// If there is no read/write logging step set, we can insert the data from
// the first mapping input/output step...
//
MappingInput[] mappingInputs = simpleMappingData.mappingTrans.findMappingInput();
if ( mappingInputs.length == 0 ) {
throw new KettleException(
"The simple mapping step needs one Mapping Input step to write to in the sub-transformation" );
}
if ( mappingInputs.length > 1 ) {
throw new KettleException(
"The simple mapping step does not support multiple Mapping Input steps to write to in the sub-transformation" );
}
simpleMappingData.mappingInput = mappingInputs[0];
simpleMappingData.mappingInput.setConnectorSteps( new StepInterface[0], new ArrayList<MappingValueRename>(), null );
// LogTableField readField = data.mappingTransMeta.getTransLogTable().findField(TransLogTable.ID.LINES_READ);
// if (readField.getSubject() == null) {
// readField.setSubject(data.mappingInput.getStepMeta());
// }
MappingOutput[] mappingOutputs = simpleMappingData.mappingTrans.findMappingOutput();
if ( mappingOutputs.length == 0 ) {
throw new KettleException(
"The simple mapping step needs one Mapping Output step to read from in the sub-transformation" );
}
if ( mappingOutputs.length > 1 ) {
throw new KettleException( "The simple mapping step does not support "
+ "multiple Mapping Output steps to read from in the sub-transformation" );
}
simpleMappingData.mappingOutput = mappingOutputs[0];
// LogTableField writeField = data.mappingTransMeta.getTransLogTable().findField(TransLogTable.ID.LINES_WRITTEN);
// if (writeField.getSubject() == null && data.mappingOutputs != null && data.mappingOutputs.length >= 1) {
// writeField.setSubject(data.mappingOutputs[0].getStepMeta());
// }
// Finally, add the mapping transformation to the active sub-transformations
// map in the parent transformation
//
getTrans().addActiveSubTransformation( getStepname(), simpleMappingData.mappingTrans );
}
void initServletConfig() {
TransStepUtil.initServletConfig( getTrans(), getData().getMappingTrans() );
}
public static void addInputRenames( List<MappingValueRename> renameList, List<MappingValueRename> addRenameList ) {
for ( MappingValueRename rename : addRenameList ) {
if ( renameList.indexOf( rename ) < 0 ) {
renameList.add( rename );
}
}
}
public boolean init( StepMetaInterface smi, StepDataInterface sdi ) {
meta = (SimpleMappingMeta) smi;
setData( (SimpleMappingData) sdi );
SimpleMappingData simpleMappingData = getData();
if ( super.init( smi, sdi ) ) {
// First we need to load the mapping (transformation)
try {
// Pass the repository down to the metadata object...
//
meta.setRepository( getTransMeta().getRepository() );
simpleMappingData.mappingTransMeta =
SimpleMappingMeta.loadMappingMeta( meta, meta.getRepository(), meta.getMetaStore(), this, meta.getMappingParameters().isInheritingAllVariables() );
if ( simpleMappingData.mappingTransMeta != null ) { // Do we have a mapping at all?
// OK, now prepare the execution of the mapping.
// This includes the allocation of RowSet buffers, the creation of the
// sub-transformation threads, etc.
//
prepareMappingExecution();
// That's all for now...
return true;
} else {
logError( "No valid mapping was specified!" );
return false;
}
} catch ( Exception e ) {
logError( "Unable to load the mapping transformation because of an error : " + e.toString() );
logError( Const.getStackTracker( e ) );
}
}
return false;
}
public void dispose( StepMetaInterface smi, StepDataInterface sdi ) {
// Close the running transformation
if ( getData().wasStarted ) {
if ( !getData().mappingTrans.isFinished() ) {
// Wait until the child transformation has finished.
getData().mappingTrans.waitUntilFinished();
}
// Remove it from the list of active sub-transformations...
//
getTrans().removeActiveSubTransformation( getStepname() );
// See if there was an error in the sub-transformation, in that case, flag error etc.
if ( getData().mappingTrans.getErrors() > 0 ) {
logError( BaseMessages.getString( PKG, "SimpleMapping.Log.ErrorOccurredInSubTransformation" ) );
setErrors( 1 );
}
}
super.dispose( smi, sdi );
}
public void stopRunning( StepMetaInterface stepMetaInterface, StepDataInterface stepDataInterface ) throws KettleException {
if ( getData().mappingTrans != null ) {
getData().mappingTrans.stopAll();
}
}
public void stopAll() {
// Stop the mapping step.
if ( getData().mappingTrans != null ) {
getData().mappingTrans.stopAll();
}
// Also stop this step
super.stopAll();
}
public Trans getMappingTrans() {
return getData().mappingTrans;
}
/**
* For preview of the main data path, make sure we pass the row listener down to the Mapping Output step...
*/
public void addRowListener( RowListener rowListener ) {
MappingOutput[] mappingOutputs = getData().mappingTrans.findMappingOutput();
if ( mappingOutputs == null || mappingOutputs.length == 0 ) {
return; // Nothing to do here...
}
// Simple case: one output mapping step : add the row listener over there
//
/*
* if (mappingOutputs.length==1) { mappingOutputs[0].addRowListener(rowListener); } else { // Find the main data
* path... //
*
*
* }
*/
// Add the row listener to all the outputs in the mapping...
//
for ( MappingOutput mappingOutput : mappingOutputs ) {
mappingOutput.addRowListener( rowListener );
}
}
public SimpleMappingData getData() {
return data;
}
private void setData( SimpleMappingData data ) {
this.data = data;
}
}