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 });
}
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();
}
}
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;
}
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;
}
}
}
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);
}
Aggregations