/
ckan.java
executable file
·284 lines (234 loc) · 7.96 KB
/
ckan.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
package org.ckan;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedHashMap;
import java.util.List;
import org.pentaho.di.core.exception.KettleException;
import org.pentaho.di.core.row.RowMetaInterface;
import org.pentaho.di.trans.Trans;
import org.pentaho.di.trans.TransMeta;
import org.pentaho.di.trans.step.BaseStep;
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.CKANclient.CKANException;
import org.CKANclient.Client;
import org.CKANclient.Connection;
import org.CKANclient.Dataset;
import org.CKANclient.DataStore;
import org.CKANclient.Field;
import org.CKANclient.Resource;
public class ckan extends BaseStep implements StepInterface {
private ckanData data;
private ckanMeta meta;
private String ckanDomain = "";
private String ckanApiKey = "";
private String ckanPackageId = "";
private String ckanResourceTitle = "";
private String ckanResourceDescription = "";
private String ckanResourceId = "";
private int ckanBatchSize = 5000;
private List<String> ckanPrimaryKeyList = new ArrayList<String>();
private String outputResourceId = "";
private boolean primaryKeyExists = false;
private int fieldLength = 0;
private int rowCount = 0;
private List<String> primaryKeyField = new ArrayList<String>();
private List<Field> fields = new ArrayList<Field>();
private LinkedHashMap<String, Object> dataRow = new LinkedHashMap<String, Object>();
private List<LinkedHashMap<String, Object>> records = new ArrayList<LinkedHashMap<String, Object>>();
public ckan(StepMeta s, StepDataInterface stepDataInterface, int c, TransMeta t, Trans dis) {
super(s,stepDataInterface,c,t,dis);
}
public boolean processRow(StepMetaInterface smi, StepDataInterface sdi) throws KettleException {
meta = (ckanMeta)smi;
data = (ckanData)sdi;
String fieldName = "";
String fieldType = "";
int indexOfValue = 0;
String fieldValue = "";
// Get row, blocks when needed!
Object[] r = getRow();
// No more input to be expected...
if (r==null) {
if (rowCount % ckanBatchSize != 0) {
uploadDataStore(records);
}
setOutputDone();
return false;
}
if (first) {
first = false;
// Determine output field structure
data.inputRowMeta = getInputRowMeta();
data.outputRowMeta = (RowMetaInterface)getInputRowMeta().clone();
meta.getFields(data.outputRowMeta, getStepname(), null, null, this);
fieldLength = data.outputRowMeta.getFieldNames().length;
for(int i=0; i < fieldLength; i++) {
fieldName = data.outputRowMeta.getFieldNames()[i];
fieldType = data.outputRowMeta.getFieldNamesAndTypes(0)[i].trim();
if (fieldType.equals("(Integer)") || fieldType.equals("(Number)")) {
fields.add(new Field(fieldName, "numeric"));
}
else if (fieldType.equals("(Date)")) {
fields.add(new Field(fieldName, "timestamp"));
}
else {
fields.add(new Field(fieldName, "text"));
}
}
List<String> fieldList = Arrays.asList(data.outputRowMeta.getFieldNames());
if ( fieldList.containsAll(ckanPrimaryKeyList) && !(ckanPrimaryKeyList.isEmpty()) ) {
primaryKeyExists = true;
if (log.isBasic()) logBasic("Primary Key found, upserting data.");
}
else {
if (log.isBasic()) logBasic("Primary Key not found, inserting data.");
}
// Create or update a DataStore resource
newDataStoreResorce(fields);
if (outputResourceId.length() == 0) {
// Unable to upload data, so end early
if (log.isBasic()) logError("No valid resource ID to upload data to.");
setOutputDone();
return false;
}
}
rowCount++;
dataRow = new LinkedHashMap<String, Object>();
for(int i=0; i < fieldLength; i++) {
fieldName = data.outputRowMeta.getFieldNames()[i];
indexOfValue = data.inputRowMeta.indexOfValue(environmentSubstitute(fieldName));
fieldValue = data.inputRowMeta.getString(r, indexOfValue);
dataRow.put(fieldName, fieldValue);
}
records.add(dataRow);
// Upload data in batches every 5000 rows
if (rowCount % ckanBatchSize == 0) {
uploadDataStore(records);
records = new ArrayList<LinkedHashMap<String, Object>>();
}
// Copy row to possible alternate rowset(s)
putRow(data.outputRowMeta, r);
// Some basic logging every 5000 rows
if (checkFeedback(getLinesRead())) {
if (log.isBasic()) logBasic("Lines read: " + getLinesRead());
}
return true;
}
public boolean init(StepMetaInterface smi, StepDataInterface sdi) {
meta = (ckanMeta)smi;
data = (ckanData)sdi;
// Get user parameters
if (meta.getDomain() != null) {
ckanDomain = meta.getDomain();
}
if (meta.getApiKey() != null) {
ckanApiKey = meta.getApiKey();
}
if (meta.getPackageId() != null) {
ckanPackageId = meta.getPackageId();
}
if (meta.getResourceTitle() != null) {
ckanResourceTitle = meta.getResourceTitle();
}
if (meta.getResourceDescription() != null) {
ckanResourceDescription = meta.getResourceDescription();
}
if (meta.getResourceId() != null) {
ckanResourceId = meta.getResourceId();
}
try {
ckanBatchSize = Integer.parseInt(meta.getBatchSize());
} catch (NumberFormatException e) {
if (log.isBasic()) logBasic("Batch Size [" + meta.getBatchSize() + "] was not valid, set to 5000");
ckanBatchSize = 5000;
}
if (meta.getPrimaryKey() != null) {
ckanPrimaryKeyList = Arrays.asList(meta.getPrimaryKey().split("\\s*;;\\s*"));
}
return super.init(smi, sdi);
}
public void dispose(StepMetaInterface smi, StepDataInterface sdi) {
meta = (ckanMeta)smi;
data = (ckanData)sdi;
super.dispose(smi, sdi);
}
public boolean newDataStoreResorce(List<Field> fields) {
int port;
if (ckanDomain.toLowerCase().startsWith("https://")) {
port = 443;
}
else {
port= 80;
}
Client ckanClient = new Client( new Connection(ckanDomain, port), ckanApiKey);
try {
DataStore ds = new DataStore();
if (ckanResourceId.length() != 0) {
ds.setResource_id(ckanResourceId);
}
else {
Resource rs = new Resource();
rs.setPackage_id(ckanPackageId);
rs.setName(ckanResourceTitle);
rs.setDescription(ckanResourceDescription);
rs.setFormat("CSV");
ds.setResource(rs);
}
ds.setFields(fields);
ds.setForce("True");
if (primaryKeyExists) {
for (int i=0; i<ckanPrimaryKeyList.size();i++){
primaryKeyField.add(ckanPrimaryKeyList.get(i));
}
ds.setPrimary_key(primaryKeyField);
}
DataStore result = ckanClient.createDataStore(ds);
outputResourceId = result.getResource_id();
if (log.isBasic()) logBasic("Uploading to Resource ID: " + outputResourceId);
return true;
} catch ( CKANException cke ) {
logError(cke.toString());
return false;
}
catch ( Exception e ) {
logError(e.toString());
return false;
}
}
public boolean uploadDataStore(List<LinkedHashMap<String, Object>> records) {
int port;
if (ckanDomain.toLowerCase().startsWith("https://")) {
port = 443;
}
else {
port= 80;
}
Client ckanClient = new Client( new Connection(ckanDomain, port), ckanApiKey);
try {
DataStore ds = new DataStore();
ds.setRecords(records);
ds.setResource_id(outputResourceId);
ds.setForce("True");
if (primaryKeyExists) {
ds.setMethod("upsert");
if (log.isBasic()) logBasic("Upserting data...");
}
else {
ds.setMethod("insert");
if (log.isBasic()) logBasic("Inserting data...");
}
DataStore result = ckanClient.upsertDataStore(ds,3);
return true;
} catch ( CKANException cke ) {
logError(cke.toString());
return false;
}
catch ( Exception e ) {
logError(e.toString());
return false;
}
}
}