NiFi ConvertRecord处理器转Avro为XML时冗余内容去除咨询
It sounds like your XMLRecordWriter is using default settings that add unnecessary wrapper elements (<nifiRecord>, <attribute>) and override your desired encoding. Here are the most effective ways to resolve this:
1. Configure XMLRecordWriter to Match Your Desired Output (Best Practice)
The root cause here is the default behavior of NiFi's XMLRecordWriter, which wraps records in generic elements. Adjust these properties in your XMLRecordWriter controller service:
- Root Element Name: Change this from the default
nifiRecordtoFDX. This will make<FDX>the top-level element instead of<nifiRecord>. - Record Element Name: If your Avro schema directly represents the content inside
<FDX>(not a nested record), set this to an empty string to eliminate the extra<attribute>wrapper. - Character Encoding: Set this to
UTF-16to match your original XML's encoding. - Include Header Line: Ensure this is enabled so the writer outputs the correct
<?xml version="1.0" encoding="UTF-16"?>declaration.
Once these are updated, ConvertRecord should generate XML that matches your original structure without redundant wrappers.
2. Post-Process with ReplaceText (If Writer Configuration Isn't Feasible)
If modifying the XMLRecordWriter isn't an option, use the ReplaceText processor to strip the outer elements:
- Search Value: Use this regex pattern to capture your target XML content (adjust whitespace if your output has different spacing):
^<\?xml version="1.0" encoding="UTF-8"\?> <nifiRecord><attribute> (.*) </attribute></nifiRecord>$ - Replacement Value: Set this to
$1to retain only the captured inner XML (including your original<FDX>structure and correct encoding declaration). - Replacement Strategy: Choose "Replace Entire Text" to overwrite the FlowFile content with the cleaned XML.
3. Validate Schema Alignment
Double-check that your Avro schema exactly matches the structure of the XML you want to produce. If your Avro schema’s top-level record maps directly to the <FDX> element’s content, the XMLRecordWriter will generate the correct structure without unexpected nesting. Mismatched schemas often lead to unwanted wrappers, so alignment prevents future issues.
内容的提问来源于stack exchange,提问作者red_eyes

