如何使用NiFi的PutHbaseJson/PutHbaseCell插入多条JSON数据到HBase?
Hey there! Let's walk through exactly how to get those multiple JSON records into HBase using NiFi's two HBase processors. I'll start with the one you've already experimented with—PutHbaseCell—since you have sample data ready, then cover the more JSON-friendly PutHbaseJson for comparison.
Using PutHbaseCell (Your Current Processor)
PutHbaseCell works best when you need explicit control over HBase columns, but it requires splitting your multi-JSON input into individual records first. Here's the step-by-step workflow:
Split Your Multi-JSON FlowFile into Single Records
- If your input is a comma-separated list of JSON objects (like your sample):
- First, use a ReplaceText processor to wrap the content in an array:
- Set
Search Valueto^(start of content) andReplacement Valueto[ - Add a second ReplaceText processor: set
Search Valueto$(end of content) andReplacement Valueto]
- Set
- Then add a SplitJson processor with
JsonPath Expressionset to$.*—this will split the array into individual FlowFiles, each containing one JSON object.
- First, use a ReplaceText processor to wrap the content in an array:
- If your input is already a valid JSON array (
[{"id":"1"},{"id":"2"}]), skip the ReplaceText steps and go straight to SplitJson.
- If your input is a comma-separated list of JSON objects (like your sample):
Extract JSON Fields as NiFi Attributes
- Add an EvaluateJsonPath processor to pull out the fields you need for HBase:
- Create these user-defined properties:
row_id→$.id(this will be your HBase row key)name_val→$.namepath_val→$.path
- Create these user-defined properties:
- Set
Destinationtoflowfile-attributeso these values are stored as FlowFile attributes.
- Add an EvaluateJsonPath processor to pull out the fields you need for HBase:
Configure PutHbaseCell
- Fill in the core settings:
HBase Configuration Resources: Path to yourhbase-site.xmlfile (so NiFi can connect to your HBase cluster)Table Name: The name of your target HBase table (e.g.,product_categories)Row ID: Use the attribute we extracted:${row_id}
- Under the
Columnssection, add entries for each field you want to insert:- For the
namefield:- Column Family:
info(replace with your actual column family) - Column Qualifier:
name - Value:
${name_val}
- Column Family:
- For the
pathfield:- Column Family:
info - Column Qualifier:
path - Value:
${path_val}
- Column Family:
- For the
- Adjust
Batch Size(default 1000) if you want to tune how many records are inserted in one batch.
- Fill in the core settings:
Using PutHbaseJson (Simpler for JSON Data)
If your JSON structure is consistent, PutHbaseJson is more streamlined—it maps JSON fields directly to HBase columns without needing to extract attributes first. Here's how to set it up:
Split Multi-JSON into Single Records
- Follow the same SplitJson (and ReplaceText if needed) steps as above to get one JSON object per FlowFile.
Configure PutHbaseJson
- Core settings:
HBase Configuration Resources: Same as above, path tohbase-site.xmlTable Name: Your target HBase tableRow ID JsonPath:$.id(tells the processor which JSON field to use as the HBase row key)
- Map JSON fields to HBase columns:
- Option 1 (Single Column Family): Set
Column Familyto your desired family (e.g.,info), and the processor will automatically map every JSON field to a column qualifier with the same name (soname→info:name,path→info:path). - Option 2 (Custom Mappings): Use the
Column Mappingsproperty to specify exact mappings, like:
Format:name:info:product_name, path:info:category_pathjson-field-name:hbase-column-family:hbase-column-qualifier
- Option 1 (Single Column Family): Set
- Core settings:
Key Notes to Avoid Issues
- Permissions: Make sure the NiFi service account has write access to your HBase table.
- Debugging: Use a LogAttribute processor after EvaluateJsonPath/SplitJson to verify that attributes or split records are correct before sending to HBase.
- Batch Performance: Adjust the
Batch Sizein both processors based on your cluster's capacity—larger batches can improve throughput but may increase latency.
内容的提问来源于stack exchange,提问作者Umang

