-
Notifications
You must be signed in to change notification settings - Fork 1
/
receive.ktr
358 lines (323 loc) · 17 KB
/
receive.ktr
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
346
347
348
349
350
351
352
353
354
355
356
357
358
<?xml version="1.0" encoding="UTF-8"?>
<transformation>
<info>
<name>receive</name>
<description/>
<extended_description/>
<trans_version/>
<trans_type>Normal</trans_type>
<trans_status>0</trans_status>
<directory>/</directory>
<parameters>
</parameters>
<log>
<trans-log-table><connection/>
<schema/>
<table/>
<size_limit_lines/>
<interval/>
<timeout_days/>
<field><id>ID_BATCH</id><enabled>Y</enabled><name>ID_BATCH</name></field><field><id>CHANNEL_ID</id><enabled>Y</enabled><name>CHANNEL_ID</name></field><field><id>TRANSNAME</id><enabled>Y</enabled><name>TRANSNAME</name></field><field><id>STATUS</id><enabled>Y</enabled><name>STATUS</name></field><field><id>LINES_READ</id><enabled>Y</enabled><name>LINES_READ</name><subject/></field><field><id>LINES_WRITTEN</id><enabled>Y</enabled><name>LINES_WRITTEN</name><subject/></field><field><id>LINES_UPDATED</id><enabled>Y</enabled><name>LINES_UPDATED</name><subject/></field><field><id>LINES_INPUT</id><enabled>Y</enabled><name>LINES_INPUT</name><subject/></field><field><id>LINES_OUTPUT</id><enabled>Y</enabled><name>LINES_OUTPUT</name><subject/></field><field><id>LINES_REJECTED</id><enabled>Y</enabled><name>LINES_REJECTED</name><subject/></field><field><id>ERRORS</id><enabled>Y</enabled><name>ERRORS</name></field><field><id>STARTDATE</id><enabled>Y</enabled><name>STARTDATE</name></field><field><id>ENDDATE</id><enabled>Y</enabled><name>ENDDATE</name></field><field><id>LOGDATE</id><enabled>Y</enabled><name>LOGDATE</name></field><field><id>DEPDATE</id><enabled>Y</enabled><name>DEPDATE</name></field><field><id>REPLAYDATE</id><enabled>Y</enabled><name>REPLAYDATE</name></field><field><id>LOG_FIELD</id><enabled>Y</enabled><name>LOG_FIELD</name></field><field><id>EXECUTING_SERVER</id><enabled>N</enabled><name>EXECUTING_SERVER</name></field><field><id>EXECUTING_USER</id><enabled>N</enabled><name>EXECUTING_USER</name></field><field><id>CLIENT</id><enabled>N</enabled><name>CLIENT</name></field></trans-log-table>
<perf-log-table><connection/>
<schema/>
<table/>
<interval/>
<timeout_days/>
<field><id>ID_BATCH</id><enabled>Y</enabled><name>ID_BATCH</name></field><field><id>SEQ_NR</id><enabled>Y</enabled><name>SEQ_NR</name></field><field><id>LOGDATE</id><enabled>Y</enabled><name>LOGDATE</name></field><field><id>TRANSNAME</id><enabled>Y</enabled><name>TRANSNAME</name></field><field><id>STEPNAME</id><enabled>Y</enabled><name>STEPNAME</name></field><field><id>STEP_COPY</id><enabled>Y</enabled><name>STEP_COPY</name></field><field><id>LINES_READ</id><enabled>Y</enabled><name>LINES_READ</name></field><field><id>LINES_WRITTEN</id><enabled>Y</enabled><name>LINES_WRITTEN</name></field><field><id>LINES_UPDATED</id><enabled>Y</enabled><name>LINES_UPDATED</name></field><field><id>LINES_INPUT</id><enabled>Y</enabled><name>LINES_INPUT</name></field><field><id>LINES_OUTPUT</id><enabled>Y</enabled><name>LINES_OUTPUT</name></field><field><id>LINES_REJECTED</id><enabled>Y</enabled><name>LINES_REJECTED</name></field><field><id>ERRORS</id><enabled>Y</enabled><name>ERRORS</name></field><field><id>INPUT_BUFFER_ROWS</id><enabled>Y</enabled><name>INPUT_BUFFER_ROWS</name></field><field><id>OUTPUT_BUFFER_ROWS</id><enabled>Y</enabled><name>OUTPUT_BUFFER_ROWS</name></field></perf-log-table>
<channel-log-table><connection/>
<schema/>
<table/>
<timeout_days/>
<field><id>ID_BATCH</id><enabled>Y</enabled><name>ID_BATCH</name></field><field><id>CHANNEL_ID</id><enabled>Y</enabled><name>CHANNEL_ID</name></field><field><id>LOG_DATE</id><enabled>Y</enabled><name>LOG_DATE</name></field><field><id>LOGGING_OBJECT_TYPE</id><enabled>Y</enabled><name>LOGGING_OBJECT_TYPE</name></field><field><id>OBJECT_NAME</id><enabled>Y</enabled><name>OBJECT_NAME</name></field><field><id>OBJECT_COPY</id><enabled>Y</enabled><name>OBJECT_COPY</name></field><field><id>REPOSITORY_DIRECTORY</id><enabled>Y</enabled><name>REPOSITORY_DIRECTORY</name></field><field><id>FILENAME</id><enabled>Y</enabled><name>FILENAME</name></field><field><id>OBJECT_ID</id><enabled>Y</enabled><name>OBJECT_ID</name></field><field><id>OBJECT_REVISION</id><enabled>Y</enabled><name>OBJECT_REVISION</name></field><field><id>PARENT_CHANNEL_ID</id><enabled>Y</enabled><name>PARENT_CHANNEL_ID</name></field><field><id>ROOT_CHANNEL_ID</id><enabled>Y</enabled><name>ROOT_CHANNEL_ID</name></field></channel-log-table>
<step-log-table><connection/>
<schema/>
<table/>
<timeout_days/>
<field><id>ID_BATCH</id><enabled>Y</enabled><name>ID_BATCH</name></field><field><id>CHANNEL_ID</id><enabled>Y</enabled><name>CHANNEL_ID</name></field><field><id>LOG_DATE</id><enabled>Y</enabled><name>LOG_DATE</name></field><field><id>TRANSNAME</id><enabled>Y</enabled><name>TRANSNAME</name></field><field><id>STEPNAME</id><enabled>Y</enabled><name>STEPNAME</name></field><field><id>STEP_COPY</id><enabled>Y</enabled><name>STEP_COPY</name></field><field><id>LINES_READ</id><enabled>Y</enabled><name>LINES_READ</name></field><field><id>LINES_WRITTEN</id><enabled>Y</enabled><name>LINES_WRITTEN</name></field><field><id>LINES_UPDATED</id><enabled>Y</enabled><name>LINES_UPDATED</name></field><field><id>LINES_INPUT</id><enabled>Y</enabled><name>LINES_INPUT</name></field><field><id>LINES_OUTPUT</id><enabled>Y</enabled><name>LINES_OUTPUT</name></field><field><id>LINES_REJECTED</id><enabled>Y</enabled><name>LINES_REJECTED</name></field><field><id>ERRORS</id><enabled>Y</enabled><name>ERRORS</name></field><field><id>LOG_FIELD</id><enabled>N</enabled><name>LOG_FIELD</name></field></step-log-table>
<metrics-log-table><connection/>
<schema/>
<table/>
<timeout_days/>
<field><id>ID_BATCH</id><enabled>Y</enabled><name>ID_BATCH</name></field><field><id>CHANNEL_ID</id><enabled>Y</enabled><name>CHANNEL_ID</name></field><field><id>LOG_DATE</id><enabled>Y</enabled><name>LOG_DATE</name></field><field><id>METRICS_DATE</id><enabled>Y</enabled><name>METRICS_DATE</name></field><field><id>METRICS_CODE</id><enabled>Y</enabled><name>METRICS_CODE</name></field><field><id>METRICS_DESCRIPTION</id><enabled>Y</enabled><name>METRICS_DESCRIPTION</name></field><field><id>METRICS_SUBJECT</id><enabled>Y</enabled><name>METRICS_SUBJECT</name></field><field><id>METRICS_TYPE</id><enabled>Y</enabled><name>METRICS_TYPE</name></field><field><id>METRICS_VALUE</id><enabled>Y</enabled><name>METRICS_VALUE</name></field></metrics-log-table>
</log>
<maxdate>
<connection/>
<table/>
<field/>
<offset>0.0</offset>
<maxdiff>0.0</maxdiff>
</maxdate>
<size_rowset>10000</size_rowset>
<sleep_time_empty>50</sleep_time_empty>
<sleep_time_full>50</sleep_time_full>
<unique_connections>N</unique_connections>
<feedback_shown>Y</feedback_shown>
<feedback_size>50000</feedback_size>
<using_thread_priorities>Y</using_thread_priorities>
<shared_objects_file/>
<capture_step_performance>N</capture_step_performance>
<step_performance_capturing_delay>1000</step_performance_capturing_delay>
<step_performance_capturing_size_limit>100</step_performance_capturing_size_limit>
<dependencies>
</dependencies>
<partitionschemas>
</partitionschemas>
<slaveservers>
</slaveservers>
<clusterschemas>
</clusterschemas>
<created_user>-</created_user>
<created_date>2015/07/15 17:03:36.926</created_date>
<modified_user>-</modified_user>
<modified_date>2015/07/15 17:03:36.926</modified_date>
<key_for_session_key>H4sIAAAAAAAAAAMAAAAAAAAAAAA=</key_for_session_key>
<is_key_private>N</is_key_private>
</info>
<notepads>
<notepad>
<note>This transformation expect this variables set:

-amqp.user
-amqp.host
-amqp.password
-amqp.routing_key
-amqp.exchange
-amqp.queue
-limit.messages.to.get
</note>
<xloc>92</xloc>
<yloc>148</yloc>
<width>401</width>
<heigth>240</heigth>
<fontname>Arial</fontname>
<fontsize>10</fontsize>
<fontbold>N</fontbold>
<fontitalic>N</fontitalic>
<fontcolorred>0</fontcolorred>
<fontcolorgreen>0</fontcolorgreen>
<fontcolorblue>0</fontcolorblue>
<backgroundcolorred>255</backgroundcolorred>
<backgroundcolorgreen>205</backgroundcolorgreen>
<backgroundcolorblue>112</backgroundcolorblue>
<bordercolorred>100</bordercolorred>
<bordercolorgreen>100</bordercolorgreen>
<bordercolorblue>100</bordercolorblue>
<drawshadow>Y</drawshadow>
</notepad>
</notepads>
<connection>
<name>importers</name>
<server>${importer.db.host}</server>
<type>POSTGRESQL</type>
<access>Native</access>
<database>${importer.db.name}</database>
<port>5432</port>
<username>${importer.db.user}</username>
<password>${importer.db.password}</password>
<servername/>
<data_tablespace/>
<index_tablespace/>
<attributes>
<attribute><code>FORCE_IDENTIFIERS_TO_LOWERCASE</code><attribute>N</attribute></attribute>
<attribute><code>FORCE_IDENTIFIERS_TO_UPPERCASE</code><attribute>N</attribute></attribute>
<attribute><code>IS_CLUSTERED</code><attribute>N</attribute></attribute>
<attribute><code>PORT_NUMBER</code><attribute>5432</attribute></attribute>
<attribute><code>PRESERVE_RESERVED_WORD_CASE</code><attribute>N</attribute></attribute>
<attribute><code>QUOTE_ALL_FIELDS</code><attribute>N</attribute></attribute>
<attribute><code>SUPPORTS_BOOLEAN_DATA_TYPE</code><attribute>Y</attribute></attribute>
<attribute><code>SUPPORTS_TIMESTAMP_DATA_TYPE</code><attribute>Y</attribute></attribute>
<attribute><code>USE_POOLING</code><attribute>N</attribute></attribute>
</attributes>
</connection>
<connection>
<name>km77</name>
<server>${km77.db.host}</server>
<type>POSTGRESQL</type>
<access>Native</access>
<database>${km77.db.name}</database>
<port>5432</port>
<username>${km77.db.user}</username>
<password>${km77.db.password}</password>
<servername/>
<data_tablespace/>
<index_tablespace/>
<attributes>
<attribute><code>FORCE_IDENTIFIERS_TO_LOWERCASE</code><attribute>N</attribute></attribute>
<attribute><code>FORCE_IDENTIFIERS_TO_UPPERCASE</code><attribute>N</attribute></attribute>
<attribute><code>IS_CLUSTERED</code><attribute>N</attribute></attribute>
<attribute><code>PORT_NUMBER</code><attribute>5432</attribute></attribute>
<attribute><code>PRESERVE_RESERVED_WORD_CASE</code><attribute>N</attribute></attribute>
<attribute><code>QUOTE_ALL_FIELDS</code><attribute>N</attribute></attribute>
<attribute><code>SUPPORTS_BOOLEAN_DATA_TYPE</code><attribute>Y</attribute></attribute>
<attribute><code>SUPPORTS_TIMESTAMP_DATA_TYPE</code><attribute>Y</attribute></attribute>
<attribute><code>USE_POOLING</code><attribute>N</attribute></attribute>
</attributes>
</connection>
<order>
<hop> <from>Get X Mensajes</from><to>Dummy</to><enabled>Y</enabled> </hop>
</order>
<step>
<name>Get X Mensajes</name>
<type>UserDefinedJavaClass</type>
<description/>
<distribute>Y</distribute>
<custom_distribution/>
<copies>1</copies>
<partitioning>
<method>none</method>
<schema_name/>
</partitioning>
<definitions>
<definition>
<class_type>TRANSFORM_CLASS</class_type>
<class_name>Processor</class_name>
<class_source><![CDATA[/*
* RabbitMQ consumer
*/
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.ConnectionFactory;
import com.rabbitmq.client.GetResponse;
import com.rabbitmq.client.AMQP;
import com.rabbitmq.client.ConsumerCancelledException;
import com.rabbitmq.client.ShutdownSignalException;
import java.io.IOException;
private FieldHelper outputField = null;
Connection connection = null;
Channel channel = null;
String encoding = "UTF-8";
String exchangeName = null;
String routingKey = null;
String queueName = null;
boolean autoAck = false; //false if the server should expect explicit acknowledgements
boolean durable = true; //=survive server restart
int limite = 0;
int linea = 0;
public boolean processRow(StepMetaInterface smi, StepDataInterface sdi) throws KettleException
{
if (first) {
outputField = get(Fields.Out, getParameter("OUTPUT_FIELD"));
first = false;
}
Object[] outputRow = createOutputRow(new Object[0], data.outputRowMeta.size());
try {
GetResponse response = channel.basicGet(queueName, autoAck);
// i want to receive only X number of messages. This controls if i got the 10 messages i want to receive
if( linea < limite ){
linea = linea + 1;
}else{
// stop transformation
setOutputDone();
return false;
}
if (response == null) {
// No message retrieved => stop transformation
setOutputDone();
return false;
} else {
AMQP.BasicProperties props = response.getProps();
String messageBody = new String (response.getBody(), encoding);
long deliveryTag = response.getEnvelope().getDeliveryTag();
outputField.setValue(outputRow, messageBody);
putRow(data.outputRowMeta, outputRow);
channel.basicAck(deliveryTag, false); // acknowledge receipt of the message
return true;
}
} catch (IOException ex) {
putError(data.outputRowMeta, outputRow, 1, ex.toString(), "","" );
return true;
}
}
public boolean init(StepMetaInterface stepMetaInterface, StepDataInterface stepDataInterface)
{
if (parent.initImpl(stepMetaInterface, stepDataInterface)){
try {
ConnectionFactory factory = new ConnectionFactory();
factory.setUsername(getVariable("amqp.user"));
factory.setPassword(getVariable("amqp.password"));
factory.setVirtualHost("/");
factory.setHost(getVariable("amqp.host"));
factory.setPort(5672);
connection = factory.newConnection();
channel = connection.createChannel();
durable = true; //=survive server restart
exchangeName = getVariable("amqp.exchange");
channel.exchangeDeclare(exchangeName, "topic", durable);
routingKey = getVariable("amqp.routing_key");
queueName = getVariable("amqp.queue");
boolean exclusive = false; // Exclusive queues may only be accessed by the current connection, and are deleted when that connection closes
boolean autoDelete = false; //If true, the exchange is deleted when all queues have finished using it.
channel.queueDeclare(queueName, durable, exclusive, autoDelete, null);
//String queueName = channel.queueDeclare().getQueue(); //a non-durable, exclusive, autodelete queue with a generated name
channel.queueBind(queueName, exchangeName, routingKey);
limite = Integer.parseInt( getVariable("limit.messages.to.get") );
return true;
} catch (InterruptedException ex) {
logError("InterruptedException: ", ex);
return false;
} catch (NumberFormatException ex) {
logError("NumberFormatException: ", ex);
return false;
} catch (ShutdownSignalException ex) {
logError("ShutdownSignalException: ", ex);
return false;
} catch (ConsumerCancelledException ex) {
logError("ConsumerCancelledException: ", ex);
return false;
} catch (NullPointerException ex) {
logError("NullPointerException: ", ex);
return false;
} catch (Exception ex) {
logError("Exception: ", ex);
return false;
}
}
return false;
}
public void dispose(StepMetaInterface smi, StepDataInterface sdi)
{
if ((channel != null) || (connection != null)) {
try{
channel.close();
connection.close();
} catch (Exception ex) {
logError("Exception: ", ex);
}
}
parent.disposeImpl(smi, sdi);
}]]></class_source>
</definition>
</definitions>
<fields>
<field>
<field_name>mensaje</field_name>
<field_type>String</field_type>
<field_length>-1</field_length>
<field_precision>-1</field_precision>
</field>
</fields><clear_result_fields>N</clear_result_fields>
<info_steps></info_steps><target_steps></target_steps><usage_parameters><usage_parameter><parameter_tag>OUTPUT_FIELD</parameter_tag>
<parameter_value>mensaje</parameter_value>
<parameter_description/>
</usage_parameter></usage_parameters> <cluster_schema/>
<remotesteps> <input> </input> <output> </output> </remotesteps> <GUI>
<xloc>189</xloc>
<yloc>65</yloc>
<draw>Y</draw>
</GUI>
</step>
<step>
<name>Dummy</name>
<type>Dummy</type>
<description/>
<distribute>Y</distribute>
<custom_distribution/>
<copies>1</copies>
<partitioning>
<method>none</method>
<schema_name/>
</partitioning>
<cluster_schema/>
<remotesteps> <input> </input> <output> </output> </remotesteps> <GUI>
<xloc>607</xloc>
<yloc>73</yloc>
<draw>Y</draw>
</GUI>
</step>
<step_error_handling>
</step_error_handling>
<slave-step-copy-partition-distribution>
</slave-step-copy-partition-distribution>
<slave_transformation>N</slave_transformation>
</transformation>