Kui,
The getControllerService() method requires a controller service (CS)
identifier, not the name (because names are not necessarily unique).
To get the CS by name, you have to get the list of all CSs and match
on the name. I have an example in Groovy on my blog [1], but here is a
similar one ported to Nashorn/Javascript:
var ControllerService =
Java.type('org.apache.nifi.controller.ControllerService');
var lookup = context.getControllerServiceLookup();
var dbServiceName = databaseConnectionPoolName.getValue();
var dbcpServiceId = null;
for each(var cs in
lookup.getControllerServiceIdentifiers(ControllerService.class)) {
if(lookup.getControllerServiceName(cs) == dbServiceName) {
dbcpServiceId = cs;
break;
}
}
if(dbcpServiceId != null) {
var conn = lookup.getControllerService(dbcpServiceId).getConnection();
var stmt = conn.createStatement();
var resultSet = stmt.execute("SELECT * FROM users");
// do stuff here
log.info('***** do stuff here *****');
} else {
log.error("Couldn't find Controller Service: " + dbServiceName);
}
Regards,
Matt
[1] http://funnifi.blogspot.com/2016/04/sql-in-nifi-with-executescript.html
On Thu, Dec 21, 2017 at 4:09 AM, oppo <[email protected]> wrote:
> Hi,
>
> I try to use javascript in processor to do some enrichment and
> transformation, but i don't know how to concat the service identifier.
> As it is ExecutorScript processor, I can't add a service reference in
> properties, I can only add service in current Group (I put logic in one
> Process Group), I have name and id of service, I enable the service.
>
> See the following code:
>
> var enrichmentService =
> context.getControllerServiceLookup().getControllerService("DBCPConnectionPool");
> if(enrichmentService == null) {
> log.warn("Can't find DBCPConnectionPool");
> session.transfer(flowFile, REL_FAILURE);
> }
>
> It always jump to null condition.
>
> Anyone can help?
>
> Kui