Search in sources :

Example 1 with RecordPathSegment

use of org.apache.nifi.record.path.paths.RecordPathSegment in project nifi by apache.

the class Concat method evaluate.

@Override
public Stream<FieldValue> evaluate(final RecordPathEvaluationContext context) {
    Stream<FieldValue> concatenated = Stream.empty();
    for (final RecordPathSegment valuePath : valuePaths) {
        final Stream<FieldValue> stream = valuePath.evaluate(context);
        concatenated = Stream.concat(concatenated, stream);
    }
    final StringBuilder sb = new StringBuilder();
    concatenated.forEach(fv -> sb.append(DataTypeUtils.toString(fv.getValue(), (String) null)));
    final RecordField field = new RecordField("concat", RecordFieldType.STRING.getDataType());
    final FieldValue responseValue = new StandardFieldValue(sb.toString(), field, null);
    return Stream.of(responseValue);
}
Also used : RecordPathSegment(org.apache.nifi.record.path.paths.RecordPathSegment) RecordField(org.apache.nifi.serialization.record.RecordField) StandardFieldValue(org.apache.nifi.record.path.StandardFieldValue) FieldValue(org.apache.nifi.record.path.FieldValue) StandardFieldValue(org.apache.nifi.record.path.StandardFieldValue)

Example 2 with RecordPathSegment

use of org.apache.nifi.record.path.paths.RecordPathSegment in project nifi by apache.

the class RecordPath method compile.

/**
 * Compiles a RecordPath from the given text
 *
 * @param path the textual representation of the RecordPath
 * @return the compiled RecordPath
 * @throws RecordPathException if the given text is not a valid RecordPath
 */
public static RecordPath compile(final String path) throws RecordPathException {
    try {
        final CharStream input = new ANTLRStringStream(path);
        final RecordPathLexer lexer = new RecordPathLexer(input);
        final CommonTokenStream lexerTokenStream = new CommonTokenStream(lexer);
        final RecordPathParser parser = new RecordPathParser(lexerTokenStream);
        final Tree tree = (Tree) parser.pathExpression().getTree();
        // We look at the first child, because 'tree' is a PATH_EXPRESSION and we really
        // want the underlying PATH token.
        final Tree firstChild = tree.getChild(0);
        final int childType = firstChild.getType();
        final boolean absolute;
        final RecordPathSegment rootPath;
        if (childType == PATH || childType == CHILD_REFERENCE) {
            rootPath = new RootPath();
            absolute = true;
        } else {
            rootPath = null;
            absolute = false;
        }
        return RecordPathCompiler.compile(firstChild, rootPath, absolute);
    } catch (final RecordPathException e) {
        throw e;
    } catch (final Exception e) {
        throw new RecordPathException(e);
    }
}
Also used : ANTLRStringStream(org.antlr.runtime.ANTLRStringStream) RecordPathSegment(org.apache.nifi.record.path.paths.RecordPathSegment) CommonTokenStream(org.antlr.runtime.CommonTokenStream) RootPath(org.apache.nifi.record.path.paths.RootPath) Tree(org.antlr.runtime.tree.Tree) RecordPathException(org.apache.nifi.record.path.exception.RecordPathException) CharStream(org.antlr.runtime.CharStream) RecordPathException(org.apache.nifi.record.path.exception.RecordPathException)

Aggregations

RecordPathSegment (org.apache.nifi.record.path.paths.RecordPathSegment)2 ANTLRStringStream (org.antlr.runtime.ANTLRStringStream)1 CharStream (org.antlr.runtime.CharStream)1 CommonTokenStream (org.antlr.runtime.CommonTokenStream)1 Tree (org.antlr.runtime.tree.Tree)1 FieldValue (org.apache.nifi.record.path.FieldValue)1 StandardFieldValue (org.apache.nifi.record.path.StandardFieldValue)1 RecordPathException (org.apache.nifi.record.path.exception.RecordPathException)1 RootPath (org.apache.nifi.record.path.paths.RootPath)1 RecordField (org.apache.nifi.serialization.record.RecordField)1