Dave
Dave

Reputation: 888

whats a good way to parse the incoming url in nifi?

When using HandleHttpRequest, i want to setup a structure to operate on different objects through the same handler:

/api/foo/add/1/2..

how do i easily parse that out into

object = foo
operation = add
arg1 = [1,2,...]

?

Upvotes: 0

Views: 835

Answers (2)

Andre de Miranda
Andre de Miranda

Reputation: 744

Why not to use ExpressionLanguage getDelimitedField ?

From the Expression Language documentation:

getDelimitedField
Description: Parses the Subject as a delimited line of text and returns just a single field from that delimited text.

Subject Type: String

Arguments:

index : The index of the field to return. A value of 1 will return the first field, a value of 2 will return the second field, and so on.

delimiter : Optional argument that provides the character to use as a field separator. If not specified, a comma will be used. This value must be exactly 1 character.

quoteChar : Optional argument that provides the character that can be used to quote values so that the delimiter can be used within a single field. If not specified, a double-quote (") will be used. This value must be exactly 1 character.

escapeChar : Optional argument that provides the character that can be used to escape the Quote Character or the Delimiter within a field. If not specified, a backslash (\) is used. This value must be exactly 1 character.

stripChars : Optional argument that specifies whether or not quote characters and escape characters should be stripped. For example, if we have a field value "1, 2, 3" and this value is true, we will get the value 1, 2, 3, but if this value is false, we will get the value "1, 2, 3" with the quotes. The default value is false. This value must be either true or false.

Upvotes: 1

ozw1z5rd
ozw1z5rd

Reputation: 3208

This code is just an example you can try sticking a executeScript processor on nifi's workbench. You can use this as example.

from urlparse import parse_qs, urlparse 

def parse ( uri2parse ) : 
        o = urlparse( uri2parse )
        d = parse_qs( o.query )        
        return ( o.path[1:], d['year'][0], d['month'][0], d['day'][0] )    

# get the flow file from the incoming queue

flowfile = session.get() 
if flowfile is not None: 
        source_URI = flowfile.getAttribute( 'source_URI' )
        destination_URI = flowfile.getAttribute(  'destination_URI' ) 
        current_time = flowfile.getAttribute(  'current_time' ) 

        # expand the URI into smaller pieces 
        src_table, src_year, src_month, src_day = parse( source_URI ) 
        dst_table, dst_year, dst_month, dst_day = parse( destination_URI ) 

        flowfile = session.putAllAttributes( flowfile, { 'src_table' : src_table, 'src_year': src_year, 'src_month' :src_month, 'src_day': src_day })
        flowfile = session.putAllAttributes( flowfile, { 'dst_table' : dst_table, 'dst_year': dst_year, 'dst_month' :dst_month, 'dst_day': dst_day })

        session.transfer( flowfile, REL_SUCCESS )
else: 
        flowfile = session.create() 
        session.transer( flowfile, REL_FAILURE ) 

Upvotes: 0

Related Questions