Our Community is getting an upgrade! To get everything ready for the relaunch, we’ll be placing the site in read-only mode starting September 21st.
We really appreciate your understanding while we get things set up behind the scenes. Catch up on all the exciting details about the move here.
Need help or have questions? Drop us a line at [email protected]
Created 03-05-2018 09:56 AM
@Tags({"json mapping with entitymaker"})
@CapabilityDescription("Provide a description")
public class MyProcessor extends AbstractProcessor {
public static final Relationship SUCCESS = new Relationship.Builder()
.name("SUCCESS")
.description("Successfully converted incoming json file to EntitymakerJSON")
.build();
public static final Relationship FALIURE = new Relationship.Builder()
.name("FALIURE")
.description("Successfully converted incoming json file to EntitymakerJSON")
.build();
private Set<Relationship> relationships;
@Override
protected void init(final ProcessorInitializationContext context) {
final Set<Relationship> relationships = new HashSet<Relationship>();
relationships.add(SUCCESS);
relationships.add(FALIURE);
this.relationships = Collections.unmodifiableSet(relationships);
}
@Override
public Set<Relationship> getRelationships() {
return relationships;
}
/* public final List<PropertyDescriptor> getSupportedPropertyDescriptors() {
return null ;
}
@OnScheduled
public void onScheduled(final ProcessContext context) {
}*/
@Override
public void onTrigger(final ProcessContext context, final ProcessSession session)
throws ProcessException,FlowFileHandlingException {
final AtomicReference<String> value = new AtomicReference<>();
FlowFile flowFile = session.get();
if ( flowFile == null ) {
return;
}
session.read(flowFile, new InputStreamCallback() {
@Override
public void process(InputStream in) throws IOException {
try{
JSONParser parser = new JSONParser();
JSONArray inputdata;
String json = IOUtils.toString(in);
inputdata = (JSONArray) parser.parse(json);
Jsonmapping jsm = new Jsonmapping();
for (Object o : inputdata)
{
JSONObject person = (JSONObject) o;
JSONObject jo = jsm.finaljson(person);
value.set(jo.toString());
}
}catch(Exception ex){
ex.printStackTrace();
getLogger().error("Failed to read json string.");
}
}
});
flowFile = session.write(flowFile, new OutputStreamCallback() {
@Override
public void process(OutputStream out) throws IOException {
out.write(value.get().getBytes());
}
});
session.transfer(flowFile, SUCCESS);
}
}
Created 03-06-2018 06:16 AM
I think I have not used Failure Relationship, that's why is showing Error. Because when I create another processor and take only SUCCESS now it is solved.
Created 03-05-2018 02:30 PM
Please format the code so that it is readable.
Created 03-06-2018 04:45 AM
Hi @Bryan Bende, I have formatted the code please check.
Created 03-06-2018 06:16 AM
I think I have not used Failure Relationship, that's why is showing Error. Because when I create another processor and take only SUCCESS now it is solved.
Created 06-23-2019 10:09 PM
It's because you have a typo in your code: .name("FALIURE")