|
1 | 1 | package sha0w.pub.jsonNifi.processors; |
2 | 2 |
|
| 3 | +import com.jayway.jsonpath.JsonPath; |
| 4 | +import org.apache.commons.io.IOUtils; |
3 | 5 | import org.apache.nifi.annotation.behavior.SideEffectFree; |
4 | 6 | import org.apache.nifi.annotation.documentation.CapabilityDescription; |
5 | 7 | import org.apache.nifi.annotation.documentation.Tags; |
6 | | -import org.apache.nifi.processor.AbstractProcessor; |
7 | | -import org.apache.nifi.processor.ProcessContext; |
8 | | -import org.apache.nifi.processor.ProcessSession; |
| 8 | +import org.apache.nifi.components.PropertyDescriptor; |
| 9 | +import org.apache.nifi.flowfile.FlowFile; |
| 10 | +import org.apache.nifi.processor.*; |
9 | 11 | import org.apache.nifi.processor.exception.ProcessException; |
| 12 | +import org.apache.nifi.processor.io.InputStreamCallback; |
| 13 | +import org.apache.nifi.processor.io.OutputStreamCallback; |
| 14 | +import org.apache.nifi.processor.util.StandardValidators; |
| 15 | + |
| 16 | +import java.io.IOException; |
| 17 | +import java.io.InputStream; |
| 18 | +import java.io.OutputStream; |
| 19 | +import java.util.*; |
| 20 | +import java.util.concurrent.atomic.AtomicReference; |
| 21 | + |
| 22 | +import static java.lang.System.in; |
10 | 23 |
|
11 | 24 | /** |
12 | 25 | * Created by coco1 on 2017/7/18. |
|
15 | 28 | @Tags({"JSON","SHA0W.PUB"}) |
16 | 29 | @CapabilityDescription("Fetch value from json path.") |
17 | 30 | public class JsonProcessor extends AbstractProcessor{ |
| 31 | + |
| 32 | + private List<PropertyDescriptor> properties; |
| 33 | + private Set<Relationship> relationships; |
| 34 | + |
| 35 | + public static final String MATCH_ATTR = "match"; |
| 36 | + |
| 37 | + public static final PropertyDescriptor JSON_PATH = new PropertyDescriptor.Builder() |
| 38 | + .name("Json Path") |
| 39 | + .required(true) |
| 40 | + .addValidator(StandardValidators.NON_EMPTY_VALIDATOR) |
| 41 | + .build(); |
| 42 | + |
| 43 | + public static final Relationship SUCCESS = new Relationship.Builder() |
| 44 | + .name("SUCCESS") |
| 45 | + .description("Succes relationship") |
| 46 | + .build(); |
| 47 | + @Override |
| 48 | + public void init(final ProcessorInitializationContext context ) { |
| 49 | + ArrayList<PropertyDescriptor> properties = new ArrayList<>(); |
| 50 | + properties.add(JSON_PATH); |
| 51 | +// 防止多线程ADD |
| 52 | + this.properties = Collections.unmodifiableList(properties); |
| 53 | + Set<Relationship> relationships = new HashSet<>(); |
| 54 | + relationships.add(SUCCESS); |
| 55 | +// 防止多线程ADD |
| 56 | + this.relationships = Collections.unmodifiableSet(relationships); |
| 57 | + } |
| 58 | + |
18 | 59 | @Override |
19 | 60 | public void onTrigger(ProcessContext processContext, ProcessSession processSession) throws ProcessException { |
20 | | - |
| 61 | + final AtomicReference<String> value = new AtomicReference<>(); |
| 62 | + |
| 63 | + FlowFile flowFile = processSession.get(); |
| 64 | + |
| 65 | + processSession.read(flowFile, in -> { |
| 66 | + try{ |
| 67 | + String json = IOUtils.toString(in); |
| 68 | + String result = JsonPath.read(json, "$.hello"); |
| 69 | + value.set(result); |
| 70 | + }catch(Exception ex){ |
| 71 | + ex.printStackTrace(); |
| 72 | + getLogger().error("Failed to read json string."); |
| 73 | + } |
| 74 | + }); |
| 75 | + |
| 76 | + String results = value.get(); |
| 77 | + if(results != null && !results.isEmpty()){ |
| 78 | + flowFile = processSession.putAttribute(flowFile, "match", results); |
| 79 | + } |
| 80 | + |
| 81 | + // To write the results back out ot flow file |
| 82 | + flowFile = processSession.write(flowFile, out -> out.write(value.get().getBytes())); |
| 83 | + |
| 84 | + processSession.transfer(flowFile, SUCCESS); |
| 85 | + |
| 86 | + } |
| 87 | + |
| 88 | + @Override |
| 89 | + public Set<Relationship> getRelationships(){ |
| 90 | + return relationships; |
| 91 | + } |
| 92 | + |
| 93 | + @Override |
| 94 | + public List<PropertyDescriptor> getSupportedPropertyDescriptors(){ |
| 95 | + return properties; |
21 | 96 | } |
22 | 97 | } |
0 commit comments