2016-05-15 4 views
1

特定のケースでNiFiでReplaceTextWithMappingの使用法を明確にする必要があります。私の入力ファイルは、次のようになりますマッピングファイルの複数の列でReplaceTextWithMappingを使用する

{"field1" : "A", 
"field2" : "A", 
"field3": "A" 
} 

マッピングファイルは次のように、代わりに、なります。次のように

Header1;Header2;Header3 
A;some text;2 

私の予想結果は次のようになります。

{"field1" : "some text", 
    "field2": "A", 
    "field3": "A2" 
    } 

正規表現のセット次のように単純になります。

[A-Z0-9]+ 

であり、マッピングファイルのフィールドキーと一致しています(大文字または大文字+桁のいずれかが必要です)。しかし、どの値を(col 2またはcol3から)どのように割り当てるかはわかりませんの入力値。また、私のfield2は変更してはならず、入力値から取得しているのと同じ値を保持する必要があります。現時点では、私はこのようなものになっています:あなたは、あなたのマッピングファイルの異なる列から別の値を使用して、入力ファイル内の同じ値をマッピングされたことができます。

{"field1" : "some text A2", 
    "field2": "some text A2", 
    "field3": "some text A2" 
    } 

を私は私の主な質問があると思いますか?

はあなたに

EDITありがとう:私はReplaceTextWithMapping、ApacheのNiFi(V 0.5.1)ですぐにプロセッサを使用しています。データフロー全体を通して、私はExtronTextを使って解析するのではなく、メモリにロードしたい外部ファイルからのマッピングを適用する必要があるJsonファイルを作成します。

答えて

0

フォワード

あなたがJSON文字列で作業しているJSON構造を解析するなりにくいエッジケースを作成することができますよう、JSONの構文解析エンジンを経由して、このような文字列で動作するように容易になるだろうと思われます正規表現は難しい。それで、私はあなたの理由があると確信しています、そして、私は正規の警察ではありません。あなたがしておこうストリングと置き換えたい部分文字列をキャプチャするために容易になるだろう、このような交換を行うには

説明

(\{"[a-z0-9]+"\s*:\s*")([a-z0-9]+)("[,\r\n]+"[a-z0-9]+"\s*:\s*")([a-z0-9]+)("[,\r\n]+"[a-z0-9]+"\s*:\s*")([a-z0-9]+)("[,\r\n]+\})

と交換してください:$1SomeText$3$4$5A2$7

Regular expression visualization

注:私はこの表現で、以下のフラグを使用することをお勧め:ケース小文字を区別しない、との点は、新しい行を含むすべての文字に一致します。

Exmaples

ライブ電王

この例では、正規表現は、あなたのソーステキストに対して一致する方法を示しています。 https://regex101.com/r/vM1qE2/1

ソーステキスト

{"field1" : "A", 
"field2" : "A", 
"field3": "A" 
} 

は、交換後

{"field1" : "SomeText", 
"field2" : "A", 
"field3": "A2" 
} 

説明

NODE      EXPLANATION 
---------------------------------------------------------------------- 
    (      group and capture to \1: 
---------------------------------------------------------------------- 
    \{      '{' 
---------------------------------------------------------------------- 
    "      '"' 
---------------------------------------------------------------------- 
    [a-z0-9]+    any character of: 'a' to 'z', '0' to '9' 
          (1 or more times (matching the most 
          amount possible)) 
---------------------------------------------------------------------- 
    "      '"' 
---------------------------------------------------------------------- 
    \s*      whitespace (\n, \r, \t, \f, and " ") (0 
          or more times (matching the most amount 
          possible)) 
---------------------------------------------------------------------- 
    :      ':' 
---------------------------------------------------------------------- 
    \s*      whitespace (\n, \r, \t, \f, and " ") (0 
          or more times (matching the most amount 
          possible)) 
---------------------------------------------------------------------- 
    "      '"' 
---------------------------------------------------------------------- 
)      end of \1 
---------------------------------------------------------------------- 
    (      group and capture to \2: 
---------------------------------------------------------------------- 
    [a-z0-9]+    any character of: 'a' to 'z', '0' to '9' 
          (1 or more times (matching the most 
          amount possible)) 
---------------------------------------------------------------------- 
)      end of \2 
---------------------------------------------------------------------- 
    (      group and capture to \3: 
---------------------------------------------------------------------- 
    "      '"' 
---------------------------------------------------------------------- 
    [,\r\n]+     any character of: ',', '\r' (carriage 
          return), '\n' (newline) (1 or more times 
          (matching the most amount possible)) 
---------------------------------------------------------------------- 
    "      '"' 
---------------------------------------------------------------------- 
    [a-z0-9]+    any character of: 'a' to 'z', '0' to '9' 
          (1 or more times (matching the most 
          amount possible)) 
---------------------------------------------------------------------- 
    "      '"' 
---------------------------------------------------------------------- 
    \s*      whitespace (\n, \r, \t, \f, and " ") (0 
          or more times (matching the most amount 
          possible)) 
---------------------------------------------------------------------- 
    :      ':' 
---------------------------------------------------------------------- 
    \s*      whitespace (\n, \r, \t, \f, and " ") (0 
          or more times (matching the most amount 
          possible)) 
---------------------------------------------------------------------- 
    "      '"' 
---------------------------------------------------------------------- 
)      end of \3 
---------------------------------------------------------------------- 
    (      group and capture to \4: 
---------------------------------------------------------------------- 
    [a-z0-9]+    any character of: 'a' to 'z', '0' to '9' 
          (1 or more times (matching the most 
          amount possible)) 
---------------------------------------------------------------------- 
)      end of \4 
---------------------------------------------------------------------- 
    (      group and capture to \5: 
---------------------------------------------------------------------- 
    "      '"' 
---------------------------------------------------------------------- 
    [,\r\n]+     any character of: ',', '\r' (carriage 
          return), '\n' (newline) (1 or more times 
          (matching the most amount possible)) 
---------------------------------------------------------------------- 
    "      '"' 
---------------------------------------------------------------------- 
    [a-z0-9]+    any character of: 'a' to 'z', '0' to '9' 
          (1 or more times (matching the most 
          amount possible)) 
---------------------------------------------------------------------- 
    "      '"' 
---------------------------------------------------------------------- 
    \s*      whitespace (\n, \r, \t, \f, and " ") (0 
          or more times (matching the most amount 
          possible)) 
---------------------------------------------------------------------- 
    :      ':' 
---------------------------------------------------------------------- 
    \s*      whitespace (\n, \r, \t, \f, and " ") (0 
          or more times (matching the most amount 
          possible)) 
---------------------------------------------------------------------- 
    "      '"' 
---------------------------------------------------------------------- 
)      end of \5 
---------------------------------------------------------------------- 
    (      group and capture to \6: 
---------------------------------------------------------------------- 
    [a-z0-9]+    any character of: 'a' to 'z', '0' to '9' 
          (1 or more times (matching the most 
          amount possible)) 
---------------------------------------------------------------------- 
)      end of \6 
---------------------------------------------------------------------- 
    (      group and capture to \7: 
---------------------------------------------------------------------- 
    "      '"' 
---------------------------------------------------------------------- 
    [,\r\n]+     any character of: ',', '\r' (carriage 
          return), '\n' (newline) (1 or more times 
          (matching the most amount possible)) 
---------------------------------------------------------------------- 
    \}      '}' 
---------------------------------------------------------------------- 
)      end of \7 
+0

こんにちは、あなたの非常に詳細な応答のためのおかげで、はるかに高く評価。私は実際にReplaceTextWithMapping(https://nifi.apache.org/docs/nifi-docs/components/org.apache.nifi.processors.standard.ReplaceTextWithMapping/index.html参照)と呼ばれるNiFiプロセッサを使用しています。一致するグループ(一致するグループプロパティ)を使用できるようにするには、一度に1つしか設定できないようです。私はいくつかの試みをしましたが、NiFiではうまくいきません。 – paranza

+0

ReplaceTextWithMappingのドキュメントでは、置換systaxがドル記号のない数字であることを示しています。私は、置換の値にdollarsign + Numberコンボを使用するReplaceTextを見てみることをお勧めします。 https://nifi.apache.org/docs/nifi-docs/components/org.apache.nifi.processors.standard.ReplaceText/index.html –

+0

これは私の最初のステップでしたが、私のデータフローを通してReplaceTextWithMappingに移動しました外部ファイルからのマッピングを適用する必要があります。だから私はReplaceTextWithMappingを使用していると私は何かが不足しているかどうかはわかりませんが、複数の一致するグループで動作するようには思われません。おかげさまで、ありがとうございました。 – paranza

0

は、だから私はあなたのユースケースを解決しようとそれを得るためにReplaceTextWithMappingに鳩が、私はちょうど何を行うのに十分強力であるとは思わないあなた欲しいです。現在のところ、これは目的のためだけに設計されています。シンプルな正規表現とマッチし、空白以外の文字のグループを別の文字グループにマッピングします(空白と後ろの参照を持つことができます)。

ユースケースを純粋なテキストとして見ているときは、別のキャプチャグループの値とマッピングファイルに基づいて、あるキャプチャグループの値を変更します。 JSONの観点から見ると、ユースケースははるかに簡単で、キーとマッピングファイルに基づいてキー/値のペアの値を変更する必要があります。サイド・ノートでは、マッピング・ファイルが必要ないとすれば、新しいJSON-to-JSONプロセッサが0.7.0 [1]で動作すると信じています。

解決策を探す上で、問題を見る方法はどちらも有効です。 ReplaceTextWithMappingは、拡張された機能を使用して高度なユースケースを可能にすることは確かですが、複雑すぎる可能性があります(ただし、機能の不明瞭な範囲により混乱する可能性があります)。 「ReplaceJsonWithMapping」の行に沿った新しいプロセッサも確実に追加することができますが、スコープと目的を明確に定義する必要があります。

さらに迅速な解決策として、常にExecuteScriptプロセッサを使用するオプションがあります。ここで、[2]は、基本的なJSON-to-JSONプロセッサの記述方法を説明するブログ(ExecuteScriptの作成者が作成)へのリンクです。マッピングのためにファイルを読み込む機能を持たせるには、さらに多くのロジックを追加する必要があります。

[1] https://issues.apache.org/jira/browse/NIFI-361 [2] http://funnifi.blogspot.com/2016/02/executescript-json-to-json-conversion.html

関連する問題