Search in sources :

Example 1 with ListRecordSet

use of org.apache.nifi.serialization.record.ListRecordSet in project nifi by apache.

the class SolrUtils method solrDocumentsToRecordSet.

/**
 * Writes each SolrDocument to a record.
 */
public static RecordSet solrDocumentsToRecordSet(final List<SolrDocument> docs, final RecordSchema schema) {
    final List<Record> lr = new ArrayList<Record>();
    for (SolrDocument doc : docs) {
        final Map<String, Object> recordValues = new LinkedHashMap<>();
        for (RecordField field : schema.getFields()) {
            final Object fieldValue = doc.getFieldValue(field.getFieldName());
            if (fieldValue != null) {
                if (field.getDataType().getFieldType().equals(RecordFieldType.ARRAY)) {
                    recordValues.put(field.getFieldName(), ((List<Object>) fieldValue).toArray());
                } else {
                    recordValues.put(field.getFieldName(), fieldValue);
                }
            }
        }
        lr.add(new MapRecord(schema, recordValues));
    }
    return new ListRecordSet(schema, lr);
}
Also used : MapRecord(org.apache.nifi.serialization.record.MapRecord) SolrDocument(org.apache.solr.common.SolrDocument) RecordField(org.apache.nifi.serialization.record.RecordField) ListRecordSet(org.apache.nifi.serialization.record.ListRecordSet) ArrayList(java.util.ArrayList) Record(org.apache.nifi.serialization.record.Record) MapRecord(org.apache.nifi.serialization.record.MapRecord) LinkedHashMap(java.util.LinkedHashMap)

Aggregations

ArrayList (java.util.ArrayList)1 LinkedHashMap (java.util.LinkedHashMap)1 ListRecordSet (org.apache.nifi.serialization.record.ListRecordSet)1 MapRecord (org.apache.nifi.serialization.record.MapRecord)1 Record (org.apache.nifi.serialization.record.Record)1 RecordField (org.apache.nifi.serialization.record.RecordField)1 SolrDocument (org.apache.solr.common.SolrDocument)1