Search in sources :

Example 1 with BinaryInputPartitioner

use of edu.iu.dsc.tws.data.api.formatters.BinaryInputPartitioner 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", "/tmp/2000.bin");
    builder.put("RECORD_LENGTH", 1000 * Short.BYTES);
    Config txtFileConf = builder.build();
    Path path = new Path("/tmp/2000.bin");
    InputPartitioner binaryInputPartitioner = new BinaryInputPartitioner(path, 1000 * Short.BYTES);
    binaryInputPartitioner.configure(txtFileConf);
    int count = 0;
    int minSplits = 4;
    double expectedSum = 1.6375350724E1;
    double newSum = 0.0;
    Buffer buffer;
    try {
        InputSplit[] inputSplits = binaryInputPartitioner.createInputSplits(minSplits);
        InputSplitAssigner inputSplitAssigner = binaryInputPartitioner.getInputSplitAssigner(inputSplits);
        InputSplit currentSplit;
        byte[] line = new byte[2000];
        ByteBuffer byteBuffer = ByteBuffer.allocate(2000);
        byteBuffer.order(ByteOrder.BIG_ENDIAN);
        while ((currentSplit = inputSplitAssigner.getNextInputSplit("localhost", 0)) != null) {
            currentSplit.open(txtFileConf);
            while (currentSplit.nextRecord(line) != null) {
                byteBuffer.clear();
                byteBuffer.put(line);
                byteBuffer.flip();
                buffer = byteBuffer.asShortBuffer();
                short[] shortArray = new short[1000];
                ((ShortBuffer) buffer).get(shortArray);
                for (short i : shortArray) {
                    newSum += i;
                    count++;
                }
            }
        }
        LOG.info("Sum and count values are:" + newSum + "\t" + count);
    } catch (Exception e) {
        e.printStackTrace();
    }
}
Also used : Path(edu.iu.dsc.tws.api.data.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.api.config.Config) BinaryInputPartitioner(edu.iu.dsc.tws.data.api.formatters.BinaryInputPartitioner) ByteBuffer(java.nio.ByteBuffer) InputSplit(edu.iu.dsc.tws.data.fs.io.InputSplit) ShortBuffer(java.nio.ShortBuffer) InputPartitioner(edu.iu.dsc.tws.data.api.InputPartitioner) BinaryInputPartitioner(edu.iu.dsc.tws.data.api.formatters.BinaryInputPartitioner)

Aggregations

Config (edu.iu.dsc.tws.api.config.Config)1 Path (edu.iu.dsc.tws.api.data.Path)1 InputPartitioner (edu.iu.dsc.tws.data.api.InputPartitioner)1 BinaryInputPartitioner (edu.iu.dsc.tws.data.api.formatters.BinaryInputPartitioner)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