Search in sources :

Example 1 with BinaryInputFormatter

use of edu.iu.dsc.tws.data.api.formatters.BinaryInputFormatter in project twister2 by DSC-SPIDAL.

the class TestBinaryFileFormatter method main.

public static void main(String[] args) {
    Config.Builder builder = new Config.Builder();
    builder.put("input.file.path", "/home/pulasthi/git/twister2/twister2/" + "data/src/test/resources/2000.bin");
    Config txtFileConf = builder.build();
    Path path = new Path("/home/pulasthi/git/twister2/twister2/data/src/test/resources/2000.bin");
    InputFormat binaryInputFormatter = new BinaryInputFormatter(path, 2000 * Short.BYTES);
    binaryInputFormatter.configure(txtFileConf);
    int minSplits = 8;
    double expectedSum = 1.97973979E8;
    double newSum = 0.0;
    int count = 0;
    Buffer buffer = null;
    try {
        InputSplit[] inputSplits = binaryInputFormatter.createInputSplits(minSplits);
        InputSplitAssigner inputSplitAssigner = binaryInputFormatter.getInputSplitAssigner(inputSplits);
        InputSplit currentSplit;
        byte[] line = new byte[4000];
        ByteBuffer byteBuffer = ByteBuffer.allocate(4000);
        byteBuffer.order(ByteOrder.LITTLE_ENDIAN);
        while ((currentSplit = inputSplitAssigner.getNextInputSplit(null, 0)) != null) {
            binaryInputFormatter.open(currentSplit);
            while (binaryInputFormatter.nextRecord(line) != null) {
                byteBuffer.clear();
                byteBuffer.put(line);
                byteBuffer.flip();
                buffer = byteBuffer.asShortBuffer();
                short[] shortArray = new short[2000];
                ((ShortBuffer) buffer).get(shortArray);
                for (short i : shortArray) {
                    newSum += i;
                    count++;
                }
            }
        }
        System.out.println(newSum);
        System.out.println(count);
    } catch (Exception e) {
        e.printStackTrace();
    }
}
Also used : Path(edu.iu.dsc.tws.data.fs.Path) Buffer(java.nio.Buffer) ShortBuffer(java.nio.ShortBuffer) ByteBuffer(java.nio.ByteBuffer) InputSplitAssigner(edu.iu.dsc.tws.data.fs.io.InputSplitAssigner) Config(edu.iu.dsc.tws.common.config.Config) ByteBuffer(java.nio.ByteBuffer) BinaryInputFormatter(edu.iu.dsc.tws.data.api.formatters.BinaryInputFormatter) InputFormat(edu.iu.dsc.tws.data.api.InputFormat) InputSplit(edu.iu.dsc.tws.data.fs.io.InputSplit) ShortBuffer(java.nio.ShortBuffer)

Aggregations

Config (edu.iu.dsc.tws.common.config.Config)1 InputFormat (edu.iu.dsc.tws.data.api.InputFormat)1 BinaryInputFormatter (edu.iu.dsc.tws.data.api.formatters.BinaryInputFormatter)1 Path (edu.iu.dsc.tws.data.fs.Path)1 InputSplit (edu.iu.dsc.tws.data.fs.io.InputSplit)1 InputSplitAssigner (edu.iu.dsc.tws.data.fs.io.InputSplitAssigner)1 Buffer (java.nio.Buffer)1 ByteBuffer (java.nio.ByteBuffer)1 ShortBuffer (java.nio.ShortBuffer)1