什么是在nifi中解析传入的URL的好方法?

时间:2016-07-08 21:49:50

标签: api rest apache-nifi

使用HandleHttpRequest时,我想设置一个结构,通过同一个处理程序对不同的对象进行操作:

/ API /富/添加/ 1/2 ..

我如何轻松地将其解析为

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

2 个答案:

答案 0 :(得分:0)

这段代码只是一个例子,您可以尝试在nifi的工作台上粘贴一个executeScript处理器。您可以将此作为示例。

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 ) 

答案 1 :(得分:0)

为什么不使用ExpressionLanguage getDelimitedField

从表达语言文档:

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.
相关问题