Java Code Examples for org.datavec.arrow.recordreader.ArrowRecordReader#next()

The following examples show how to use org.datavec.arrow.recordreader.ArrowRecordReader#next() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ArrowBinaryInputAdapter.java    From konduit-serving with Apache License 2.0 5 votes vote down vote up
@Override
public ArrowWritableRecordBatch convert(Buffer input, ConverterArgs parameters, Map<String, Object> contextData) {
    ArrowRecordReader arrowRecordReader = new ArrowRecordReader();
    arrowRecordReader.initialize(new InputStreamInputSplit(new ByteArrayInputStream(input.getBytes())));
    arrowRecordReader.next();
    return arrowRecordReader.getCurrentBatch();
}
 
Example 2
Source File: RecordMapperTest.java    From DataVec with Apache License 2.0 5 votes vote down vote up
@Test
public void testCopyFromCsvToArrow() throws Exception {
    val recordsPair = records();

    Path p = Files.createTempFile("csvwritetest", ".csv");
    FileUtils.write(p.toFile(),recordsPair.getFirst());
    p.toFile().deleteOnExit();


    CSVRecordReader recordReader = new CSVRecordReader();
    FileSplit fileSplit = new FileSplit(p.toFile());

    ArrowRecordWriter arrowRecordWriter = new ArrowRecordWriter(recordsPair.getMiddle());
    File outputFile = Files.createTempFile("outputarrow","arrow").toFile();
    FileSplit outputFileSplit = new FileSplit(outputFile);
    RecordMapper mapper = RecordMapper.builder().batchSize(10).inputUrl(fileSplit)
            .outputUrl(outputFileSplit).partitioner(new NumberOfRecordsPartitioner())
            .recordReader(recordReader).recordWriter(arrowRecordWriter)
            .build();
    mapper.copy();

    ArrowRecordReader arrowRecordReader = new ArrowRecordReader();
    arrowRecordReader.initialize(outputFileSplit);
    List<List<Writable>> next = arrowRecordReader.next(10);
    System.out.println(next);
    assertEquals(10,next.size());

}
 
Example 3
Source File: ArrowConverterTest.java    From DataVec with Apache License 2.0 5 votes vote down vote up
@Test
public void testCreateNDArray() throws Exception {
    val recordsToWrite = recordToWrite();
    ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
    ArrowConverter.writeRecordBatchTo(recordsToWrite.getRight(),recordsToWrite.getFirst(),byteArrayOutputStream);

    File tmpFile = new File("tmp-arrow-file-" + UUID.randomUUID().toString() + ".arrorw");
    FileOutputStream outputStream = new FileOutputStream(tmpFile);
    tmpFile.deleteOnExit();
    ArrowConverter.writeRecordBatchTo(recordsToWrite.getRight(),recordsToWrite.getFirst(),outputStream);
    outputStream.flush();
    outputStream.close();

    Pair<Schema, ArrowWritableRecordBatch> schemaArrowWritableRecordBatchPair = ArrowConverter.readFromFile(tmpFile);
    assertEquals(recordsToWrite.getFirst(),schemaArrowWritableRecordBatchPair.getFirst());
    assertEquals(recordsToWrite.getRight(),schemaArrowWritableRecordBatchPair.getRight().toArrayList());

    byte[] arr = byteArrayOutputStream.toByteArray();
    val read = ArrowConverter.readFromBytes(arr);
    assertEquals(recordsToWrite,read);

    //send file
    File tmp =  tmpDataFile(recordsToWrite);
    ArrowRecordReader recordReader = new ArrowRecordReader();

    recordReader.initialize(new FileSplit(tmp));

    recordReader.next();
    ArrowWritableRecordBatch currentBatch = recordReader.getCurrentBatch();
    INDArray arr2 = ArrowConverter.toArray(currentBatch);
    assertEquals(2,arr2.rows());
    assertEquals(2,arr2.columns());
}
 
Example 4
Source File: RecordMapperTest.java    From deeplearning4j with Apache License 2.0 5 votes vote down vote up
@Test
public void testCopyFromCsvToArrow() throws Exception {
    val recordsPair = records();

    Path p = Files.createTempFile("csvwritetest", ".csv");
    FileUtils.write(p.toFile(),recordsPair.getFirst());
    p.toFile().deleteOnExit();


    CSVRecordReader recordReader = new CSVRecordReader();
    FileSplit fileSplit = new FileSplit(p.toFile());

    ArrowRecordWriter arrowRecordWriter = new ArrowRecordWriter(recordsPair.getMiddle());
    File outputFile = Files.createTempFile("outputarrow","arrow").toFile();
    FileSplit outputFileSplit = new FileSplit(outputFile);
    RecordMapper mapper = RecordMapper.builder().batchSize(10).inputUrl(fileSplit)
            .outputUrl(outputFileSplit).partitioner(new NumberOfRecordsPartitioner())
            .recordReader(recordReader).recordWriter(arrowRecordWriter)
            .build();
    mapper.copy();

    ArrowRecordReader arrowRecordReader = new ArrowRecordReader();
    arrowRecordReader.initialize(outputFileSplit);
    List<List<Writable>> next = arrowRecordReader.next(10);
    System.out.println(next);
    assertEquals(10,next.size());

}
 
Example 5
Source File: ArrowConverterTest.java    From deeplearning4j with Apache License 2.0 5 votes vote down vote up
@Test
public void testCreateNDArray() throws Exception {
    val recordsToWrite = recordToWrite();
    ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
    ArrowConverter.writeRecordBatchTo(recordsToWrite.getRight(),recordsToWrite.getFirst(),byteArrayOutputStream);

    File f = testDir.newFolder();

    File tmpFile = new File(f, "tmp-arrow-file-" + UUID.randomUUID().toString() + ".arrorw");
    FileOutputStream outputStream = new FileOutputStream(tmpFile);
    tmpFile.deleteOnExit();
    ArrowConverter.writeRecordBatchTo(recordsToWrite.getRight(),recordsToWrite.getFirst(),outputStream);
    outputStream.flush();
    outputStream.close();

    Pair<Schema, ArrowWritableRecordBatch> schemaArrowWritableRecordBatchPair = ArrowConverter.readFromFile(tmpFile);
    assertEquals(recordsToWrite.getFirst(),schemaArrowWritableRecordBatchPair.getFirst());
    assertEquals(recordsToWrite.getRight(),schemaArrowWritableRecordBatchPair.getRight().toArrayList());

    byte[] arr = byteArrayOutputStream.toByteArray();
    val read = ArrowConverter.readFromBytes(arr);
    assertEquals(recordsToWrite,read);

    //send file
    File tmp =  tmpDataFile(recordsToWrite);
    ArrowRecordReader recordReader = new ArrowRecordReader();

    recordReader.initialize(new FileSplit(tmp));

    recordReader.next();
    ArrowWritableRecordBatch currentBatch = recordReader.getCurrentBatch();
    INDArray arr2 = ArrowConverter.toArray(currentBatch);
    assertEquals(2,arr2.rows());
    assertEquals(2,arr2.columns());
}