javacsvesper

Reading Events from CSV in Esper


I'm trying to read a couple of events from a CSV using the CSVInputAdapter but I can't achieve it, here is what I'm trying:

First I've followed the first steps of the documentation http://esper.espertech.com/release-7.1.0/esper-reference/html_single/index.html#gettingstarted

I've defined the Person event like this:

public class PersonEvent {
    private String name;
    private int age;

    public PersonEvent() {
        this.name = "NONE";
        this.age = -1;
    }

    public PersonEvent(String name, int age) {
        this.name = name;
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public int getAge() {
        return age;
    }
}

Then instantiate the service, add the event type and create a statement and a listener so everytime a PersonEvent is received it prints the name and the age of the person:

EPServiceProvider engine = EPServiceProviderManager.getDefaultProvider();
engine.getEPAdministrator().getConfiguration().addEventType(PersonEvent.class);
String epl = "select name, age from PersonEvent";
EPStatement statement = engine.getEPAdministrator().createEPL(epl);

statement.addListener((newData, oldData) -> {
            String name = (String) newData[0].get("name");
            int age = (int) newData[0].get("age");
            System.out.println(String.format("Name: %s, Age: %d", name, age));
        });

I tested it sending a simple event and it works

engine.getEPRuntime().sendEvent(new PersonEvent("Peter", 10));

This is the output

Name: Peter, Age: 10

The problem comes when I try to read the events from a CSV file, my CSV looks like this:

name,age
John,3
Anne,4

This is how I read the file:

    AdapterInputSource source = new AdapterInputSource(new File("/path/input.csv"));
    (new CSVInputAdapter(engine, source, "PersonEvent")).start();

And this is the output I receive:

Name: Peter, Age: 10
Name: NONE, Age: -1
Name: NONE, Age: -1

As you can see, I receive the information of Peter, processed by sending a simple event, and two lines of the CSV, which is OK, but without data. Could you help me if there is something that I am doing wrong? Thank you very much.


Solution

  • It might be because the class doesn't have setters? It might be because the JVM does not have any defined order for methods on Java classes. The first property may actually be 'age' and not 'name. Try providing a field order too. Debug level logging may also help you.