Stream Record Fields

This example reads the fields in a Record using the Java Streams API.  This functional programming paradigm allows you to stack complex filtering and mapping logic without a lot of boilerplate code.  You can use this to clean data, transform values, perform complex logic, and more.

Java Stream API can be applied to other DataPipeline-specific data entities such as Dataset, RecordList, FieldList, and ArrayValue. You can reference Java Streams and Iterators for more detailed information.

 

Java Code Listing

package com.northconcepts.datapipeline.examples.cookbook;

import com.northconcepts.datapipeline.core.Record;

public class StreamRecordFields {

    public static void main(String[] args) {
        Record record = new Record();

        record.setField("stageName", "John Wayne");
        record.setField("realName", "Marion Robert Morrison");
        record.setField("gender", "male");
        record.setField("city", "Winterset");
        record.setField("balance", 156.35);

        System.out.println("================================Using forEach================================");
        record.forEach(field -> System.out.println(field.getValueAsString()));

        System.out.println("\n\n================================Using stream================================");
        record.stream()
                .map(field -> field.getValueAsString().toUpperCase())
                .forEach(System.out::println);

        System.out.println("\n\n================================Using parallelStream================================");
        record.parallelStream()
                .map(field -> field.getValueAsString().toUpperCase())
                .forEachOrdered(System.out::println);

    }
}

 

Code Walkthrough

  1. A record instance is created with five fields/values.
  2. Next, each value is printed by calling forEach() method of Record.
  3. Then, both sequential and parallel Java Streams are used to process record values. Each field is mapped to uppercase and printed on the console.

 

Console Output

================================Using forEach================================
John Wayne
Marion Robert Morrison
male
Winterset
156.35


================================Using stream================================
JOHN WAYNE
MARION ROBERT MORRISON
MALE
WINTERSET
156.35


================================Using parallelStream================================
JOHN WAYNE
MARION ROBERT MORRISON
MALE
WINTERSET
156.35

 

Mobile Analytics