Support Questions

Find answers, ask questions, and share your expertise

Transfer relationship not specified and failed to create session in nifi

avatar
Contributor
	@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);
        
        
    }
}
1 ACCEPTED SOLUTION

avatar
Contributor

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.

View solution in original post

4 REPLIES 4

avatar
Master Guru

Please format the code so that it is readable.

avatar
Contributor

Hi @Bryan Bende, I have formatted the code please check.

avatar
Contributor

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.

avatar
Explorer

It's because you have a typo in your code: .name("FALIURE")