-
Notifications
You must be signed in to change notification settings - Fork 1
/
ReadGTFSSubwayDataProcessor.java
62 lines (47 loc) · 2.12 KB
/
ReadGTFSSubwayDataProcessor.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
Sure, here is the code to read GTFS subway data for NYC MTA with NiFi:
```
import org.apache.nifi.flowfile.FlowFile;
import org.apache.nifi.processor.ProcessContext;
import org.apache.nifi.processor.ProcessSession;
import org.apache.nifi.processor.Relationship;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class ReadGTFSSubwayDataProcessor extends AbstractProcessor {
private static final Logger logger = LoggerFactory.getLogger(ReadGTFSSubwayDataProcessor.class);
private static final String REL_SUCCESS = "success";
private static final String REL_FAILURE = "failure";
@Override
public void onTrigger(ProcessContext context, ProcessSession session) throws ProcessException {
FlowFile flowFile = session.get();
if (flowFile == null) {
return;
}
try {
// Read the GTFS data from the flow file
GTFSData gtfsData = new GTFSData(flowFile);
// Write the GTFS data to a database
gtfsData.writeToDatabase(context);
// Set the relationship to success
session.transfer(flowFile, REL_SUCCESS);
} catch (Exception e) {
logger.error("Error reading GTFS data", e);
// Set the relationship to failure
session.transfer(flowFile, REL_FAILURE);
}
}
@Override
protected void init(final ProcessorInitializationContext context) {
super.init(context);
// Register the relationships
this.addRelationship(REL_SUCCESS);
this.addRelationship(REL_FAILURE);
}
@Override
public Set<Relationship> getRelationships() {
return Collections.unmodifiableSet(new HashSet<>(Arrays.asList(REL_SUCCESS, REL_FAILURE)));
}
}
```
This code will read GTFS subway data for NYC MTA from a flow file and write it to a database. The relationships that are defined in this code are:
* `REL_SUCCESS`: This relationship is used to indicate that the GTFS data was successfully read and written to the database.
* `REL_FAILURE`: This relationship is used to indicate that an error occurred while reading or writing the GTFS data.