Skip to content

Instantly share code, notes, and snippets.

@chathurawidanage
Last active May 19, 2020 23:15
Show Gist options
  • Save chathurawidanage/7957ec1994f031f7e810af3827a3e2ea to your computer and use it in GitHub Desktop.
Save chathurawidanage/7957ec1994f031f7e810af3827a3e2ea to your computer and use it in GitHub Desktop.
import org.apache.arrow.memory.RootAllocator;
import org.apache.arrow.vector.IntVector;
import org.apache.arrow.vector.VectorSchemaRoot;
import org.apache.arrow.vector.ipc.ArrowFileReader;
import org.apache.arrow.vector.ipc.SeekableReadChannel;
import org.apache.arrow.vector.ipc.message.ArrowBlock;
import org.apache.arrow.vector.types.pojo.Schema;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Iterator;
import java.util.List;
public class ArrowTest {
public static void main(String[] args) throws IOException {
File arrowFile = new File("/home/chathura/Downloads/test.arrow");
FileInputStream fileInputStream = new FileInputStream(arrowFile);
SeekableReadChannel seekableReadChannel = new SeekableReadChannel(fileInputStream.getChannel());
ArrowFileReader arrowFileReader = new ArrowFileReader(seekableReadChannel,
new RootAllocator(Integer.MAX_VALUE));
VectorSchemaRoot root = arrowFileReader.getVectorSchemaRoot(); // get root
Schema schema = root.getSchema(); // get schema
List<ArrowBlock> arrowBlocks = arrowFileReader.getRecordBlocks();
System.out.println(root.getFieldVectors().size());
Iterator<Integer> x = new Iterator<Integer>() {
int currentBlock = 0;
int currentCell = 0;
IntVector intVector;
private void loadNextBlock() {
try {
if (currentBlock + 1 < arrowBlocks.size()) {
arrowFileReader.loadRecordBatch(arrowBlocks.get(currentBlock++));
intVector = (IntVector) root.getFieldVectors().get(0);
} else {
intVector = null;
}
currentCell = 0;
} catch (IOException e) {
e.printStackTrace();
}
}
{
// load the block intially
loadNextBlock();
}
@Override
public boolean hasNext() {
return intVector != null && currentCell < intVector.getValueCount();
}
@Override
public Integer next() {
int value = intVector.get(currentCell++);
// progress to next block
if (!hasNext()) {
loadNextBlock();
}
return value;
}
};
int count = 0;
while (x.hasNext()) {
count++;
System.out.println(x.next());
}
System.out.println("Read : " + count);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment