如何在NiFi中调用远程REST服务

2023-11-26

是否可以在NIFI处理器中使用xmlHttpRequest来调用远程休息服务?就我而言ExecuteScript处理器(使用Javascript)无法评估XMLHttpRequest;有没有类似的解决方案可以用来获取响应数据?

var OutputStreamCallback = Java.type("org.apache.nifi.processor.io.OutputStreamCallback");
var StandardCharsets = Java.type("java.nio.charset.StandardCharsets");

Date.prototype.isValid = function () {
    return (Object.prototype.toString.call(this) === "[object Date]")
        && !isNaN(this.getTime());
};

var flowFile = session.get();

if (flowFile != null) {
    var fromDate = flowFile.getAttribute('fromDate')
    var uid = flowFile.getAttribute('uid')

    var xmlhttp = null;
    var result = null;

        xmlhttp = new XMLHttpRequest();
        if (typeof xmlhttp.overrideMimeType != 'undefined') {
            xmlhttp.overrideMimeType('application/json');

    } else if (window.ActiveXObject) {
        xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
    }
    xmlhttp.open('GET', "similar url here WorkInfo?dateFrom=?&uid=?", true);
    xmlhttp.send(dateFrom, uid);
    if (xmlhttp.status == 200) {
        result = 'WorkInfoDate'
    }
    flowFile = session.putAttribute(flowFile, 'filename', fromDate + '_' + result);

    flowFile = session.write(flowFile,
        new OutputStreamCallback(function (outputStream) {
            outputStream.write(command.getBytes(StandardCharsets.UTF_8))
        }));

    session.transfer(flowFile, REL_SUCCESS)
}

有一个 InvokeHttp 处理器可用于调用 REST 服务,而无需编写任何代码:

https://nifi.apache.org/docs/nifi-docs/components/org.apache.nifi/nifi-standard-nar/1.3.0/org.apache.nifi.processors.standard.InvokeHTTP/index.html

来自服务的响应将写入流文件的内容。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何在NiFi中调用远程REST服务 的相关文章

随机推荐