Read from Java beans

Updated: May 30, 2022

This example shows you how to read records from an object using bean introspection and reflection by making use of JavaBeanReader class which extends XmlReader class.

This demo code read records from Java bean classes Employee and Department via JavaBeanReader and then writes the records to the console via StreamWriter class.

Java Code Listing

package com.northconcepts.datapipeline.examples.cookbook;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.apache.log4j.Logger;

import com.northconcepts.datapipeline.core.DataEndpoint;
import com.northconcepts.datapipeline.core.StreamWriter;
import com.northconcepts.datapipeline.javabean.JavaBeanReader;
import com.northconcepts.datapipeline.job.Job;

public class ReadFromJavaBeans {

    public static final Logger log = DataEndpoint.log;

    public static void main(String[] args) {
        Department department = new Department().setEmployees(new Employee(), new Manager());

        JavaBeanReader reader = new JavaBeanReader("department", department);

        reader.addField("id", "//id/text()");
        reader.addField("firstName", "//firstName/text()");
        reader.addField("lastName", "//lastName/text()");
        reader.addField("budget", "//budget/text()");

        reader.addRecordBreak("//Employee");
        reader.addRecordBreak("//Manager");

        Job.run(reader, new StreamWriter(System.out));
    }

    // ============================================
    // Employee Bean
    // ============================================
    public static class Employee {
        public static long nextId = 111;

        private long id = nextId++;
        private String firstName = "FirstName-" + getClass().getSimpleName();
        private String lastName = "LastName-" + getClass().getSimpleName();

        public long getId() {
            return id;
        }

        public Employee setId(long id) {
            this.id = id;
            return this;
        }

        public String getFirstName() {
            return firstName;
        }

        public Employee setFirstName(String firstName) {
            this.firstName = firstName;
            return this;
        }

        public String getLastName() {
            return lastName;
        }

        public Employee setLastName(String lastName) {
            this.lastName = lastName;
            return this;
        }

    }

    // ============================================
    // Manager Bean
    // ============================================
    public static class Manager extends Employee {

        private double budget = 900000.00;

        public double getBudget() {
            return budget;
        }

        public Manager setBudget(double budget) {
            this.budget = budget;
            return this;
        }

    }

    // ============================================
    // Department Bean
    // ============================================
    public static class Department {
        public static long nextDeptId = 1001;

        private long deptId = nextDeptId++;
        private Employee[] employees;
        private final List employeeList = new ArrayList();
        private final Set employeeSet = new LinkedHashSet();
        private final Map employeeMap = new LinkedHashMap();
        private int[] deptFloors = { 5, 9, 17 };

        public long getDeptId() {
            return deptId;
        }

        public Department setDeptId(long deptId) {
            this.deptId = deptId;
            return this;
        }

        public Employee[] getEmployees() {
            return employees;
        }

        public Department setEmployees(Employee... employees) {
            this.employees = employees;
            employeeList.addAll(Arrays.asList(employees));
            employeeSet.addAll(Arrays.asList(employees));
            for (Employee employee : employees) {
                employeeMap.put(employee.getId(), employee);
            }
            return this;
        }

        public List getEmployeeList() {
            return employeeList;
        }

        public Set getEmployeeSet() {
            return employeeSet;
        }

        public Map getEmployeeMap() {
            return employeeMap;
        }

        public int[] getDeptFloors() {
            return deptFloors;
        }

        public Department setDeptFloors(int[] deptFloors) {
            this.deptFloors = deptFloors;
            return this;
        }

    }

}

Code Walkthrough

  1. First, a Department class which extends Employee class is created.
  2. JavaBeanReader object is created by passing name and JavaBean object.
  3. The fields to be read from the JavaBean object are added via the JavaBeanReader.addField method which is inherited from XmlReader.
  4. Records are then printed to the console via StreamWriter class which takes System.out as a parameter to its constructor. Alternatively a static method StreamWriter.newSystemOutWriter() can also be passed to the constructor instead of new StreamWriter(System.out) .

JavaBeanReader

JavaBeanReader class is an input reader that can read records from JavaBeans classes using bean introspection and reflection. It can be created by passing name of an instance and JavaBean object. The main method in this class is the JavaBeanReader.addField method which is used to add the fields to be read from the JavaBean object. This method uses the field name and a subset of the XPath 1.0 location paths notation to identify field values. You can selectively add whichever fields you wish to be read from the input JavaBean object. Another important method is the JavaBeanReader.addRecordBreak, which tells the reader to return a new record using whatever fields have been assigned. This method is basically used to demarcate records.

Console output

01:14:45,239 DEBUG [main] datapipeline:37 - DataPipeline v7.2.0-SNAPSHOT by North Concepts Inc.
01:14:47,100 DEBUG [main] datapipeline:615 - Job[1,job-1,Mon May 23 01:14:46 EAT 2022]::Start
-----------------------------------------------
0 - Record (MODIFIED) {
    0:[id]:LONG=[111]:Long
    1:[firstName]:STRING=[FirstName-Employee]:String
    2:[lastName]:STRING=[LastName-Employee]:String
    3:[budget]:STRING=[null]
}

-----------------------------------------------
1 - Record (MODIFIED) {
    0:[id]:LONG=[112]:Long
    1:[firstName]:STRING=[FirstName-Manager]:String
    2:[lastName]:STRING=[LastName-Manager]:String
    3:[budget]:DOUBLE=[900000.0]:Double
}

-----------------------------------------------
2 - Record (MODIFIED) {
    0:[id]:LONG=[111]:Long
    1:[firstName]:STRING=[FirstName-Employee]:String
    2:[lastName]:STRING=[LastName-Employee]:String
    3:[budget]:DOUBLE=[900000.0]:Double
}

-----------------------------------------------
3 - Record (MODIFIED) {
    0:[id]:LONG=[112]:Long
    1:[firstName]:STRING=[FirstName-Manager]:String
    2:[lastName]:STRING=[LastName-Manager]:String
    3:[budget]:DOUBLE=[900000.0]:Double
}

-----------------------------------------------
4 - Record (MODIFIED) {
    0:[id]:LONG=[111]:Long
    1:[firstName]:STRING=[FirstName-Employee]:String
    2:[lastName]:STRING=[LastName-Employee]:String
    3:[budget]:STRING=[null]
}

-----------------------------------------------
5 - Record (MODIFIED) {
    0:[id]:LONG=[112]:Long
    1:[firstName]:STRING=[FirstName-Manager]:String
    2:[lastName]:STRING=[LastName-Manager]:String
    3:[budget]:DOUBLE=[900000.0]:Double
}

-----------------------------------------------
6 records
01:14:49,962 DEBUG [main] datapipeline:661 - job::Success
Mobile Analytics