Search in sources :

Example 1 with DatatypeNameMapper

use of com.airhacks.enhydrator.transform.DatatypeNameMapper in project enhydrator by AdamBien.

the class ConvertToIntAndCopyTest method init.

@Before
public void init() {
    this.input = new VirtualSinkSource();
    this.output = new VirtualSinkSource();
    //map column answer to Integer
    final DatatypeNameMapper datatypeMapper = new DatatypeNameMapper();
    datatypeMapper.addMapping("answer", Datatype.INTEGER);
    //Copy column question to answer and origin
    ColumnCopier columnCopier = new ColumnCopier();
    columnCopier.addMapping("question", "answer", "origin");
    this.pump = new Pump.Engine().startWith(columnCopier).startWith(datatypeMapper).with("answer", (c) -> {
        Integer columnValue = (Integer) c;
        return columnValue * 2;
    }).from(input).to(output).to(new LogSink()).build();
}
Also used : LogSink(com.airhacks.enhydrator.out.LogSink) Memory(com.airhacks.enhydrator.transform.Memory) CoreMatchers.is(org.hamcrest.CoreMatchers.is) ColumnCopier(com.airhacks.enhydrator.transform.ColumnCopier) Column(com.airhacks.enhydrator.in.Column) Test(org.junit.Test) Assert.assertThat(org.junit.Assert.assertThat) VirtualSinkSource(com.airhacks.enhydrator.in.VirtualSinkSource) Datatype(com.airhacks.enhydrator.transform.Datatype) DatatypeNameMapper(com.airhacks.enhydrator.transform.DatatypeNameMapper) Pump(com.airhacks.enhydrator.Pump) Row(com.airhacks.enhydrator.in.Row) Before(org.junit.Before) VirtualSinkSource(com.airhacks.enhydrator.in.VirtualSinkSource) LogSink(com.airhacks.enhydrator.out.LogSink) ColumnCopier(com.airhacks.enhydrator.transform.ColumnCopier) DatatypeNameMapper(com.airhacks.enhydrator.transform.DatatypeNameMapper) Before(org.junit.Before)

Example 2 with DatatypeNameMapper

use of com.airhacks.enhydrator.transform.DatatypeNameMapper in project enhydrator by AdamBien.

the class ReadJDKVersionsTest method readFromCSV.

@Test
public void readFromCSV() {
    Source input = new CSVFileSource("./src/test/files/jdk-dates.csv", ";", "UTF-8", true);
    VirtualSinkSource output = new VirtualSinkSource();
    Pump pump = new Pump.Engine().from(input).startWith(new SkipFirstRow()).startWith(new DatatypeNameMapper().addMapping("Year", Datatype.INTEGER)).filter("$ROW.getColumnValue('Year') > 2000").to(new LogSink()).to(output).build();
    Memory memory = pump.start();
    Assert.assertFalse(memory.areErrorsOccured());
    List<Row> rows = output.getRows();
    assertFalse(rows.isEmpty());
    Row first = rows.get(1);
    Object year = first.getColumnValue("Year");
    System.out.println("The year is: " + year);
}
Also used : VirtualSinkSource(com.airhacks.enhydrator.in.VirtualSinkSource) LogSink(com.airhacks.enhydrator.out.LogSink) Memory(com.airhacks.enhydrator.transform.Memory) CSVFileSource(com.airhacks.enhydrator.in.CSVFileSource) SkipFirstRow(com.airhacks.enhydrator.transform.SkipFirstRow) SkipFirstRow(com.airhacks.enhydrator.transform.SkipFirstRow) Row(com.airhacks.enhydrator.in.Row) DatatypeNameMapper(com.airhacks.enhydrator.transform.DatatypeNameMapper) CSVFileSource(com.airhacks.enhydrator.in.CSVFileSource) Source(com.airhacks.enhydrator.in.Source) VirtualSinkSource(com.airhacks.enhydrator.in.VirtualSinkSource) Pump(com.airhacks.enhydrator.Pump) Test(org.junit.Test)

Example 3 with DatatypeNameMapper

use of com.airhacks.enhydrator.transform.DatatypeNameMapper in project enhydrator by AdamBien.

the class FromJsonToCSVTest method filterAndCastFromCSVFileToLog.

@Test
public void filterAndCastFromCSVFileToLog() {
    Source source = new CSVFileSource(INPUT + "/languages.csv", ";", "utf-8", true);
    VirtualSinkSource sink = new VirtualSinkSource();
    Pump pump = new Pump.Engine().from(source).filter("$ROW.getColumnValue('language') === 'java'").startWith(new DatatypeNameMapper().addMapping("rank", Datatype.INTEGER)).to(sink).to(new LogSink()).build();
    Memory memory = pump.start();
    assertFalse(memory.areErrorsOccured());
    assertThat(memory.getProcessedRowCount(), is(5l));
    //expecting only "java" language
    assertThat(sink.getNumberOfRows(), is(1));
    String languageValue = (String) sink.getRow(0).getColumnValue("language");
    assertThat(languageValue, is("java"));
    //expecting "java" having rank 1 as Integer
    Object rankValue = sink.getRow(0).getColumnValue("rank");
    assertTrue(rankValue instanceof Integer);
    assertThat((Integer) rankValue, is(1));
}
Also used : VirtualSinkSource(com.airhacks.enhydrator.in.VirtualSinkSource) LogSink(com.airhacks.enhydrator.out.LogSink) Memory(com.airhacks.enhydrator.transform.Memory) CSVFileSource(com.airhacks.enhydrator.in.CSVFileSource) DatatypeNameMapper(com.airhacks.enhydrator.transform.DatatypeNameMapper) CSVFileSource(com.airhacks.enhydrator.in.CSVFileSource) Source(com.airhacks.enhydrator.in.Source) ScriptableSource(com.airhacks.enhydrator.in.ScriptableSource) VirtualSinkSource(com.airhacks.enhydrator.in.VirtualSinkSource) Pump(com.airhacks.enhydrator.Pump) Test(org.junit.Test)

Aggregations

Pump (com.airhacks.enhydrator.Pump)3 VirtualSinkSource (com.airhacks.enhydrator.in.VirtualSinkSource)3 LogSink (com.airhacks.enhydrator.out.LogSink)3 DatatypeNameMapper (com.airhacks.enhydrator.transform.DatatypeNameMapper)3 Memory (com.airhacks.enhydrator.transform.Memory)3 Test (org.junit.Test)3 CSVFileSource (com.airhacks.enhydrator.in.CSVFileSource)2 Row (com.airhacks.enhydrator.in.Row)2 Source (com.airhacks.enhydrator.in.Source)2 Column (com.airhacks.enhydrator.in.Column)1 ScriptableSource (com.airhacks.enhydrator.in.ScriptableSource)1 ColumnCopier (com.airhacks.enhydrator.transform.ColumnCopier)1 Datatype (com.airhacks.enhydrator.transform.Datatype)1 SkipFirstRow (com.airhacks.enhydrator.transform.SkipFirstRow)1 CoreMatchers.is (org.hamcrest.CoreMatchers.is)1 Assert.assertThat (org.junit.Assert.assertThat)1 Before (org.junit.Before)1