Search in sources :

Example 1 with JoltTransform

use of com.bazaarvoice.jolt.JoltTransform in project nifi by apache.

the class JoltTransformJSON method onTrigger.

@Override
public void onTrigger(final ProcessContext context, ProcessSession session) throws ProcessException {
    final FlowFile original = session.get();
    if (original == null) {
        return;
    }
    final ComponentLog logger = getLogger();
    final StopWatch stopWatch = new StopWatch(true);
    final Object inputJson;
    try (final InputStream in = session.read(original)) {
        inputJson = JsonUtils.jsonToObject(in);
    } catch (final Exception e) {
        logger.error("Failed to transform {}; routing to failure", new Object[] { original, e });
        session.transfer(original, REL_FAILURE);
        return;
    }
    final String jsonString;
    final ClassLoader originalContextClassLoader = Thread.currentThread().getContextClassLoader();
    try {
        final JoltTransform transform = getTransform(context, original);
        if (customClassLoader != null) {
            Thread.currentThread().setContextClassLoader(customClassLoader);
        }
        final Object transformedJson = TransformUtils.transform(transform, inputJson);
        jsonString = JsonUtils.toJsonString(transformedJson);
    } catch (final Exception ex) {
        logger.error("Unable to transform {} due to {}", new Object[] { original, ex.toString(), ex });
        session.transfer(original, REL_FAILURE);
        return;
    } finally {
        if (customClassLoader != null && originalContextClassLoader != null) {
            Thread.currentThread().setContextClassLoader(originalContextClassLoader);
        }
    }
    FlowFile transformed = session.write(original, new OutputStreamCallback() {

        @Override
        public void process(OutputStream out) throws IOException {
            out.write(jsonString.getBytes(DEFAULT_CHARSET));
        }
    });
    final String transformType = context.getProperty(JOLT_TRANSFORM).getValue();
    transformed = session.putAttribute(transformed, CoreAttributes.MIME_TYPE.key(), "application/json");
    session.transfer(transformed, REL_SUCCESS);
    session.getProvenanceReporter().modifyContent(transformed, "Modified With " + transformType, stopWatch.getElapsed(TimeUnit.MILLISECONDS));
    logger.info("Transformed {}", new Object[] { original });
}
Also used : FlowFile(org.apache.nifi.flowfile.FlowFile) InputStream(java.io.InputStream) JoltTransform(com.bazaarvoice.jolt.JoltTransform) OutputStream(java.io.OutputStream) IOException(java.io.IOException) ComponentLog(org.apache.nifi.logging.ComponentLog) ProcessException(org.apache.nifi.processor.exception.ProcessException) IOException(java.io.IOException) StopWatch(org.apache.nifi.util.StopWatch) OutputStreamCallback(org.apache.nifi.processor.io.OutputStreamCallback)

Example 2 with JoltTransform

use of com.bazaarvoice.jolt.JoltTransform in project nifi by apache.

the class TransformJSONResource method executeSpec.

@POST
@Produces({ MediaType.APPLICATION_JSON })
@Path("/execute")
public Response executeSpec(JoltSpecificationDTO specificationDTO) {
    try {
        JoltTransform transform = getTransformation(specificationDTO, true);
        Object inputJson = JsonUtils.jsonToObject(specificationDTO.getInput());
        return Response.ok(JsonUtils.toJsonString(TransformUtils.transform(transform, inputJson))).build();
    } catch (final Exception e) {
        logger.error("Execute Specification Failed - " + e.toString());
        return Response.status(Response.Status.INTERNAL_SERVER_ERROR).build();
    }
}
Also used : JoltTransform(com.bazaarvoice.jolt.JoltTransform) Path(javax.ws.rs.Path) POST(javax.ws.rs.POST) Produces(javax.ws.rs.Produces)

Example 3 with JoltTransform

use of com.bazaarvoice.jolt.JoltTransform in project nifi by apache.

the class TransformJSONResource method getTransformation.

protected JoltTransform getTransformation(JoltSpecificationDTO specificationDTO, boolean evaluateAttributes) throws Exception {
    Object specJson = getSpecificationJsonObject(specificationDTO, evaluateAttributes);
    String transformName = specificationDTO.getTransform();
    String modules = specificationDTO.getModules();
    ClassLoader classLoader = null;
    JoltTransform transform;
    if (modules != null && !modules.isEmpty()) {
        classLoader = ClassLoaderUtils.getCustomClassLoader(specificationDTO.getModules(), this.getClass().getClassLoader(), getJarFilenameFilter());
    } else {
        classLoader = this.getClass().getClassLoader();
    }
    if (transformName.equals("jolt-transform-custom")) {
        transform = TransformFactory.getCustomTransform(classLoader, specificationDTO.getCustomClass(), specJson);
    } else {
        transform = TransformFactory.getTransform(classLoader, specificationDTO.getTransform(), specJson);
    }
    return transform;
}
Also used : JoltTransform(com.bazaarvoice.jolt.JoltTransform)

Example 4 with JoltTransform

use of com.bazaarvoice.jolt.JoltTransform in project nifi by apache.

the class TransformFactory method getChainrJoltTransformations.

protected static List<JoltTransform> getChainrJoltTransformations(ClassLoader classLoader, Object specJson) throws Exception {
    if (!(specJson instanceof List)) {
        throw new SpecException("JOLT Chainr expects a JSON array of objects - Malformed spec.");
    } else {
        List operations = (List) specJson;
        if (operations.isEmpty()) {
            throw new SpecException("JOLT Chainr passed an empty JSON array.");
        } else {
            ArrayList<JoltTransform> entries = new ArrayList<JoltTransform>(operations.size());
            for (Object chainrEntryObj : operations) {
                if (!(chainrEntryObj instanceof Map)) {
                    throw new SpecException("JOLT ChainrEntry expects a JSON map - Malformed spec");
                } else {
                    Map chainrEntryMap = (Map) chainrEntryObj;
                    String opString = (String) chainrEntryMap.get("operation");
                    String operationClassName;
                    if (opString == null) {
                        throw new SpecException("JOLT Chainr \'operation\' must implement Transform or ContextualTransform");
                    } else {
                        if (ChainrEntry.STOCK_TRANSFORMS.containsKey(opString)) {
                            operationClassName = ChainrEntry.STOCK_TRANSFORMS.get(opString);
                        } else {
                            operationClassName = opString;
                        }
                        entries.add(getCustomTransform(classLoader, operationClassName, chainrEntryMap.get("spec")));
                    }
                }
            }
            return entries;
        }
    }
}
Also used : SpecException(com.bazaarvoice.jolt.exception.SpecException) JoltTransform(com.bazaarvoice.jolt.JoltTransform) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List) Map(java.util.Map)

Example 5 with JoltTransform

use of com.bazaarvoice.jolt.JoltTransform in project nifi by apache.

the class TestTransformFactory method testGetCardinalityTransform.

@Test
public void testGetCardinalityTransform() throws Exception {
    final String cardrSpec = new String(Files.readAllBytes(Paths.get("src/test/resources/TestTransformFactory/cardrSpec.json")));
    JoltTransform transform = TransformFactory.getTransform(getClass().getClassLoader(), "jolt-transform-card", JsonUtils.jsonToObject(cardrSpec));
    assertTrue(transform instanceof CardinalityTransform);
}
Also used : CardinalityTransform(com.bazaarvoice.jolt.CardinalityTransform) JoltTransform(com.bazaarvoice.jolt.JoltTransform) Test(org.junit.Test)

Aggregations

JoltTransform (com.bazaarvoice.jolt.JoltTransform)15 Test (org.junit.Test)10 Modifier (com.bazaarvoice.jolt.Modifier)3 CardinalityTransform (com.bazaarvoice.jolt.CardinalityTransform)1 Chainr (com.bazaarvoice.jolt.Chainr)1 Defaultr (com.bazaarvoice.jolt.Defaultr)1 Removr (com.bazaarvoice.jolt.Removr)1 Shiftr (com.bazaarvoice.jolt.Shiftr)1 Sortr (com.bazaarvoice.jolt.Sortr)1 SpecException (com.bazaarvoice.jolt.exception.SpecException)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 OutputStream (java.io.OutputStream)1 URL (java.net.URL)1 URLClassLoader (java.net.URLClassLoader)1 Path (java.nio.file.Path)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 Map (java.util.Map)1 POST (javax.ws.rs.POST)1