Example usage for org.apache.hadoop.io DataInputBuffer DataInputBuffer

List of usage examples for org.apache.hadoop.io DataInputBuffer DataInputBuffer

Introduction

In this page you can find the example usage for org.apache.hadoop.io DataInputBuffer DataInputBuffer.

Prototype

public DataInputBuffer() 

Source Link

Document

Constructs a new empty buffer.

Usage

From source file:com.asakusafw.runtime.stage.collector.WritableSlotTest.java

License:Apache License

static <T extends Writable> T read(T writable, byte[] bytes) {
    DataInputBuffer buffer = new DataInputBuffer();
    buffer.reset(bytes, bytes.length);// ww w. j  a  va2  s .c o m
    try {
        writable.readFields(buffer);
        assertThat("Enf of Stream", buffer.read(), is(-1));
    } catch (IOException e) {
        throw new AssertionError(e);
    }
    return writable;
}

From source file:com.asakusafw.runtime.value.DecimalOptionTest.java

License:Apache License

/**
 * heavy read.//from   ww  w.j ava2 s  .  com
 * @throws Exception if failed
 */
@Test
public void stress_read() throws Exception {
    int count = 10000000;
    DecimalOption value = new DecimalOption(new BigDecimal("3.14"));
    byte[] bytes = toBytes(value);
    DecimalOption buf = new DecimalOption();
    DataInputBuffer in = new DataInputBuffer();
    for (int i = 0; i < count; i++) {
        in.reset(bytes, bytes.length);
        buf.readFields(in);
        if (i == 0) {
            assertThat(buf, is(value));
        }
    }
}

From source file:com.asakusafw.runtime.value.DecimalOptionTest.java

License:Apache License

/**
 * heavy read./*from w  w w .ja v  a2 s .  c o  m*/
 * @throws Exception if failed
 */
@Test
public void stress_read_large() throws Exception {
    int count = 10000000;
    DecimalOption value = new DecimalOption(new BigDecimal(Long.MAX_VALUE).multiply(BigDecimal.TEN));
    byte[] bytes = toBytes(value);
    DecimalOption buf = new DecimalOption();
    DataInputBuffer in = new DataInputBuffer();
    for (int i = 0; i < count; i++) {
        in.reset(bytes, bytes.length);
        buf.readFields(in);
        if (i == 0) {
            assertThat(buf, is(value));
        }
    }
}

From source file:com.chinamobile.bcbsp.bspstaff.BSPStaff.java

License:Apache License

/**
 * loadData: load data for the staff./*from w  w w . jav a  2 s .com*/
 * 
 * @param job
 *        BSP job configuration
 * @param workerAgent
 *        Protocol that staff child process uses to contact its parent process
 * @return boolean
 * @throws ClassNotFoundException
 * @throws IOException
 *         e
 * @throws InterruptedException
 *         e
 */
@SuppressWarnings("unchecked")
public boolean loadData(BSPJob job, WorkerAgentProtocol workerAgent, WorkerAgentForStaffInterface aStaffAgent)
        throws ClassNotFoundException, IOException, InterruptedException {
    // rebuild the input split
    RecordReader input = null;
    org.apache.hadoop.mapreduce.InputSplit split = null;
    if (rawSplitClass.equals("no")) {
        input = null;
    } else {

        DataInputBuffer splitBuffer = new DataInputBuffer();
        splitBuffer.reset(rawSplit.getBytes(), 0, rawSplit.getLength());
        SerializationFactory factory = new SerializationFactory(job.getConf());
        Deserializer<? extends org.apache.hadoop.mapreduce.InputSplit> deserializer = (Deserializer<? extends org.apache.hadoop.mapreduce.InputSplit>) factory
                .getDeserializer(job.getConf().getClassByName(rawSplitClass));
        deserializer.open(splitBuffer);
        split = deserializer.deserialize(null);

        // rebuild the InputFormat class according to the user configuration
        InputFormat inputformat = (InputFormat) ReflectionUtils.newInstance(
                job.getConf().getClass(Constants.USER_BC_BSP_JOB_INPUT_FORMAT_CLASS, InputFormat.class),
                job.getConf());
        inputformat.initialize(job.getConf());
        input = inputformat.createRecordReader(split, job);
        input.initialize(split, job.getConf());
    }
    SuperStepReportContainer ssrc = new SuperStepReportContainer();
    ssrc.setPartitionId(this.getPartition());
    this.numCopy = (int) (1 / (job.getConf().getFloat(Constants.USER_BC_BSP_JOB_BALANCE_FACTOR,
            Constants.USER_BC_BSP_JOB_BALANCE_FACTOR_DEFAULT)));
    ssrc.setNumCopy(numCopy);
    ssrc.setCheckNum(this.staffNum);
    StaffSSControllerInterface lsssc = new StaffSSController(this.getJobId(), this.getSid(), workerAgent);
    long start = System.currentTimeMillis();
    LOG.info("in BCBSP with PartitionType is: Hash" + " start time:" + start);
    if (this.staffNum == 1 || job.getConf().getBoolean(Constants.USER_BC_BSP_JOB_ISDIVIDE, false)) {

        this.partitioner = (Partitioner<Text>) ReflectionUtils.newInstance(
                job.getConf().getClass(Constants.USER_BC_BSP_JOB_PARTITIONER_CLASS, HashPartitioner.class),
                job.getConf());
        this.partitioner.setNumPartition(this.staffNum);
        this.partitioner.intialize(job, split);

        WritePartition writePartition = new NotDivideWritePartition();
        /*
         * RecordParse recordParse = (RecordParse) ReflectionUtils .newInstance(
         * job.getConf() .getClass( Constants.USER_BC_BSP_JOB_RECORDPARSE_CLASS,
         * RecordParseDefault.class), job .getConf()); recordParse.init(job);
         * //add by chen for null bug this.recordParse = recordParse;
         * //this.recordParse.init(job);
         */
        writePartition.setRecordParse(this.recordParse);
        writePartition.setStaff(this);
        writePartition.write(input);

        ssrc.setDirFlag(new String[] { "1" });
        ssrc.setCheckNum(this.staffNum);
        lsssc.loadDataBarrier(ssrc, Constants.PARTITION_TYPE.HASH);

        LOG.info("The number of verteices from other staff" + " that cound not be parsed:" + this.lost);
        LOG.info("in BCBSP with PartitionType is:HASH" + " the number of HeadNode in this partition is:"
                + graphData.sizeForAll());

        graphData.finishAdd();
        ssrc.setCheckNum(this.staffNum * 2);
        ssrc.setDirFlag(new String[] { "2" });
        lsssc.loadDataBarrier(ssrc, Constants.PARTITION_TYPE.HASH);

    } else {
        this.partitioner = (Partitioner<Text>) ReflectionUtils.newInstance(
                job.getConf().getClass(Constants.USER_BC_BSP_JOB_PARTITIONER_CLASS, HashPartitioner.class),
                job.getConf());

        WritePartition writePartition = (WritePartition) ReflectionUtils.newInstance(job.getConf().getClass(
                Constants.USER_BC_BSP_JOB_WRITEPARTITION_CLASS, HashWritePartition.class), job.getConf());
        int multiple = 1;
        if (writePartition instanceof HashWithBalancerWritePartition) {
            this.partitioner.setNumPartition(this.staffNum * numCopy);
            multiple = 2;
        } else {

            this.partitioner.setNumPartition(this.staffNum);
            multiple = 1;
            if (writePartition instanceof RangeWritePartition) {
                multiple = 2;
            }
        }
        this.partitioner.intialize(job, split);
        /*
         * RecordParse recordParse = (RecordParse) ReflectionUtils .newInstance(
         * job.getConf() .getClass( Constants.USER_BC_BSP_JOB_RECORDPARSE_CLASS,
         * RecordParseDefault.class), job .getConf()); recordParse.init(job); //
         * this.recordParse = (RecordParse) ReflectionUtils.newInstance( //
         * job.getConf().getClass( // Constants.USER_BC_BSP_JOB_RECORDPARSE_CLASS,
         * // RecordParseDefault.class), job.getConf()); //
         * this.recordParse.init(job); this.recordParse = recordParse;
         */
        writePartition.setPartitioner(partitioner);
        writePartition.setRecordParse(this.recordParse);
        writePartition.setStaff(this);
        writePartition.setWorkerAgent(aStaffAgent);
        writePartition.setSsrc(ssrc);
        writePartition.setSssc(lsssc);

        writePartition.setTotalCatchSize(job.getConf().getInt(Constants.USER_BC_BSP_JOB_TOTALCACHE_SIZE,
                Constants.USER_BC_BSP_JOB_TOTALCACHE_SIZE_DEFAULT));

        int threadNum = job.getConf().getInt(Constants.USER_BC_BSP_JOB_SENDTHREADNUMBER,
                Constants.USER_BC_BSP_JOB_SENDTHREADNUMBER_DEFAULT);
        if (threadNum > this.staffNum) {
            threadNum = this.staffNum - 1;
        }
        writePartition.setSendThreadNum(threadNum);
        writePartition.write(input);

        ssrc.setDirFlag(new String[] { "1" });
        ssrc.setCheckNum(this.staffNum * multiple);
        lsssc.loadDataBarrier(ssrc, Constants.PARTITION_TYPE.HASH);

        LOG.info("The number of verteices from other staff that" + " cound not be parsed:" + this.lost);
        LOG.info("in BCBSP with PartitionType is:HASH" + " the number of HeadNode in this partition is:"
                + graphData.sizeForAll());

        graphData.finishAdd();

        ssrc.setCheckNum(this.staffNum * (multiple + 1));
        ssrc.setDirFlag(new String[] { "2" });
        lsssc.loadDataBarrier(ssrc, Constants.PARTITION_TYPE.HASH);
    }

    long end = System.currentTimeMillis();
    LOG.info("in BCBSP with PartitionType is:HASH" + " end time:" + end);
    LOG.info(
            "in BCBSP with PartitionType is:HASH" + " using time:" + (float) (end - start) / 1000 + " seconds");

    return true;
}

From source file:com.chinamobile.bcbsp.bspstaff.BSPStaff.java

License:Apache License

/**
 * Rebuid the partition and read data from checkpoint for intializing.
 * /*  w w  w.  ja  v a 2  s  .c om*/
 * @param job
 * @param writePartition
 * @throws ClassNotFoundException
 * @throws IOException
 */
private void intializePartitionForRecovery(BSPJob job, WritePartition writePartition)
        throws ClassNotFoundException, IOException {
    this.currentSuperStepCounter = ssc.getNextSuperStepNum();
    LOG.info("Now, this super step count is " + this.currentSuperStepCounter);
    this.partitioner = (Partitioner<Text>) ReflectionUtils.newInstance(
            job.getConf().getClass(Constants.USER_BC_BSP_JOB_PARTITIONER_CLASS, HashPartitioner.class),
            job.getConf());

    if (writePartition instanceof HashWithBalancerWritePartition) {
        this.partitioner.setNumPartition(this.staffNum * numCopy);
    } else {
        this.partitioner.setNumPartition(this.staffNum);
    }
    org.apache.hadoop.mapreduce.InputSplit split = null;
    if (rawSplitClass.equals("no")) {

    } else {

        DataInputBuffer splitBuffer = new DataInputBuffer();
        splitBuffer.reset(rawSplit.getBytes(), 0, rawSplit.getLength());
        SerializationFactory factory = new SerializationFactory(job.getConf());
        Deserializer<? extends org.apache.hadoop.mapreduce.InputSplit> deserializer = (Deserializer<? extends org.apache.hadoop.mapreduce.InputSplit>) factory
                .getDeserializer(job.getConf().getClassByName(rawSplitClass));
        deserializer.open(splitBuffer);
        split = deserializer.deserialize(null);
    }
    this.partitioner.intialize(job, split);
    displayFirstRoute();
}

From source file:com.chinamobile.bcbsp.bspstaff.BSPStaff.java

License:Apache License

/**
 * loadData: load data for the staff in SGA-Graph.
 * /*ww w  .java  2 s  . c om*/
 * @param job
 *        BSP job configuration
 * @param workerAgent
 *        Protocol that staff child process uses to contact its parent process
 * @return boolean
 * @throws ClassNotFoundException
 * @throws IOException
 *         e
 * @throws InterruptedException
 *         e
 */
@SuppressWarnings("unchecked")
public boolean loadDataSGAGraph(BSPJob job, WorkerAgentProtocol workerAgent,
        WorkerAgentForStaffInterface aStaffAgent)
        throws ClassNotFoundException, IOException, InterruptedException {
    // rebuild the input split
    RecordReader input = null;
    org.apache.hadoop.mapreduce.InputSplit split = null;
    if (rawSplitClass.equals("no")) {
        input = null;
    } else {
        DataInputBuffer splitBuffer = new DataInputBuffer();
        splitBuffer.reset(rawSplit.getBytes(), 0, rawSplit.getLength());
        SerializationFactory factory = new SerializationFactory(job.getConf());
        Deserializer<? extends org.apache.hadoop.mapreduce.InputSplit> deserializer = (Deserializer<? extends org.apache.hadoop.mapreduce.InputSplit>) factory
                .getDeserializer(job.getConf().getClassByName(rawSplitClass));
        deserializer.open(splitBuffer);
        split = deserializer.deserialize(null);

        // rebuild the InputFormat class according to the user configuration
        InputFormat inputformat = (InputFormat) ReflectionUtils.newInstance(
                job.getConf().getClass(Constants.USER_BC_BSP_JOB_INPUT_FORMAT_CLASS, InputFormat.class),
                job.getConf());
        inputformat.initialize(job.getConf());
        input = inputformat.createRecordReader(split, job);
        input.initialize(split, job.getConf());
    }
    SuperStepReportContainer ssrc = new SuperStepReportContainer();
    ssrc.setPartitionId(this.getPartition());
    this.numCopy = (int) (1 / (job.getConf().getFloat(Constants.USER_BC_BSP_JOB_BALANCE_FACTOR,
            Constants.USER_BC_BSP_JOB_BALANCE_FACTOR_DEFAULT)));
    ssrc.setNumCopy(numCopy);
    ssrc.setCheckNum(this.staffNum);
    StaffSSControllerInterface lsssc = new StaffSSController(this.getJobId(), this.getSid(), workerAgent);
    long start = System.currentTimeMillis();
    LOG.info("in BCBSP with PartitionType is: Hash" + " start time:" + start);
    if (this.staffNum == 1 || job.getConf().getBoolean(Constants.USER_BC_BSP_JOB_ISDIVIDE, false)) {

        this.partitioner = (Partitioner<Text>) ReflectionUtils.newInstance(
                job.getConf().getClass(Constants.USER_BC_BSP_JOB_PARTITIONER_CLASS, HashPartitioner.class),
                job.getConf());
        this.partitioner.setNumPartition(this.staffNum);
        this.partitioner.intialize(job, split);

        WritePartition writePartition = new NotDivideWritePartition();
        /*
         * RecordParse recordParse = (RecordParse) ReflectionUtils .newInstance(
         * job.getConf() .getClass( Constants.USER_BC_BSP_JOB_RECORDPARSE_CLASS,
         * RecordParseDefault.class), job .getConf()); recordParse.init(job);
         * //add by chen for null bug this.recordParse = recordParse;
         * //this.recordParse.init(job);
         */
        writePartition.setRecordParse(this.recordParse);
        writePartition.setStaff(this);
        writePartition.write(input);

        ssrc.setDirFlag(new String[] { "1" });
        ssrc.setCheckNum(this.staffNum);
        lsssc.loadDataBarrier(ssrc, Constants.PARTITION_TYPE.HASH);

        LOG.info("The number of verteices from other staff" + " that cound not be parsed:" + this.lost);
        LOG.info("in BCBSP with PartitionType is:HASH" + " the number of HeadNode in this partition is:"
                + graphData.sizeForAll());

        graphData.finishAdd();
        ssrc.setCheckNum(this.staffNum * 2);
        ssrc.setDirFlag(new String[] { "2" });
        lsssc.loadDataBarrier(ssrc, Constants.PARTITION_TYPE.HASH);

    } else {
        this.partitioner = (Partitioner<Text>) ReflectionUtils.newInstance(
                job.getConf().getClass(Constants.USER_BC_BSP_JOB_PARTITIONER_CLASS, HashPartitioner.class),
                job.getConf());

        WritePartition writePartition = (WritePartition) ReflectionUtils.newInstance(job.getConf().getClass(
                Constants.USER_BC_BSP_JOB_WRITEPARTITION_CLASS, HashWritePartition.class), job.getConf());
        int multiple = 1;
        if (writePartition instanceof HashWithBalancerWritePartition) {
            this.partitioner.setNumPartition(this.staffNum * numCopy);
            multiple = 2;
        } else {

            this.partitioner.setNumPartition(this.staffNum);
            multiple = 1;
            if (writePartition instanceof RangeWritePartition) {
                multiple = 2;
            }
        }
        this.partitioner.intialize(job, split);
        /*
         * RecordParse recordParse = (RecordParse) ReflectionUtils .newInstance(
         * job.getConf() .getClass( Constants.USER_BC_BSP_JOB_RECORDPARSE_CLASS,
         * RecordParseDefault.class), job .getConf()); recordParse.init(job); //
         * this.recordParse = (RecordParse) ReflectionUtils.newInstance( //
         * job.getConf().getClass( // Constants.USER_BC_BSP_JOB_RECORDPARSE_CLASS,
         * // RecordParseDefault.class), job.getConf()); //
         * this.recordParse.init(job); this.recordParse = recordParse;
         */
        writePartition.setPartitioner(partitioner);
        writePartition.setRecordParse(this.recordParse);
        writePartition.setStaff(this);
        writePartition.setWorkerAgent(aStaffAgent);
        writePartition.setSsrc(ssrc);
        writePartition.setSssc(lsssc);

        writePartition.setTotalCatchSize(job.getConf().getInt(Constants.USER_BC_BSP_JOB_TOTALCACHE_SIZE,
                Constants.USER_BC_BSP_JOB_TOTALCACHE_SIZE_DEFAULT));

        int threadNum = job.getConf().getInt(Constants.USER_BC_BSP_JOB_SENDTHREADNUMBER,
                Constants.USER_BC_BSP_JOB_SENDTHREADNUMBER_DEFAULT);
        if (threadNum > this.staffNum) {
            threadNum = this.staffNum - 1;
        }
        writePartition.setSendThreadNum(threadNum);
        writePartition.write(input);

        ssrc.setDirFlag(new String[] { "1" });
        ssrc.setCheckNum(this.staffNum * multiple);
        lsssc.loadDataBarrier(ssrc, Constants.PARTITION_TYPE.HASH);

        LOG.info("The number of verteices from other staff that" + " cound not be parsed:" + this.lost);
        LOG.info("in BCBSP with PartitionType is:HASH" + " the number of HeadNode in this partition is:"
                + graphData.sizeForAll());

        graphData.finishAdd();

        ssrc.setCheckNum(this.staffNum * (multiple + 1));
        ssrc.setDirFlag(new String[] { "2" });
        lsssc.loadDataBarrier(ssrc, Constants.PARTITION_TYPE.HASH);
        // for input graph evaluate.
        // this.evaluateflag = lsssc.graphEvaluateBarrier(0, this.staffNum,
        // this.graphData.getGlobalFactor());
    }

    long end = System.currentTimeMillis();
    LOG.info("in BCBSP with PartitionType is:HASH" + " end time:" + end);
    LOG.info(
            "in BCBSP with PartitionType is:HASH" + " using time:" + (float) (end - start) / 1000 + " seconds");

    return true;
}

From source file:com.dasasian.chok.testutil.AbstractWritableTest.java

License:Apache License

protected Writable readWritable(DataOutputBuffer out, Writable writable) throws IOException {
    DataInputBuffer inputBuffer = new DataInputBuffer();
    inputBuffer.reset(out.getData(), out.getData().length);
    writable.readFields(inputBuffer);//from w  ww.j  a  v  a  2s .  co m
    return writable;
}

From source file:com.datasalt.pangool.BaseTest.java

License:Apache License

protected static void assertSerializable(HadoopSerialization ser, ITuple tuple, boolean debug)
        throws IOException {
    DataInputBuffer input = new DataInputBuffer();
    DataOutputBuffer output = new DataOutputBuffer();
    DatumWrapper<ITuple> wrapper = new DatumWrapper<ITuple>(tuple);
    ser.ser(wrapper, output);//w  ww  .j  a va  2s. co m

    input.reset(output.getData(), 0, output.getLength());
    DatumWrapper<ITuple> wrapper2 = new DatumWrapper<ITuple>();

    wrapper2 = ser.deser(wrapper2, input);
    if (debug) {
        System.out.println("D:" + wrapper2.datum());
    }
    assertEquals(tuple, wrapper2.datum());
}

From source file:com.datasalt.pangool.BaseTest.java

License:Apache License

protected static void assertSerializable(TupleSerializer ser, TupleDeserializer deser,
        DatumWrapper<ITuple> tuple, boolean debug) throws IOException {
    DataOutputBuffer output = new DataOutputBuffer();
    ser.open(output);//from   w w w .j av  a2  s  . c  o  m
    ser.serialize(tuple);
    ser.close();

    DataInputBuffer input = new DataInputBuffer();
    input.reset(output.getData(), 0, output.getLength());
    DatumWrapper<ITuple> deserializedTuple = new DatumWrapper<ITuple>();

    deser.open(input);
    deserializedTuple = deser.deserialize(deserializedTuple);
    deser.close();

    if (debug) {
        System.out.println("D:" + deserializedTuple.datum());
    }
    assertEquals(tuple.datum(), deserializedTuple.datum());
}

From source file:com.datasalt.pangool.BaseTest.java

License:Apache License

protected void assertSerializable(Tuple tuple, Tuple toReuse, boolean debug) throws IOException {
    HadoopSerialization hadoopSer = new HadoopSerialization(getConf());
    SimpleTupleSerializer ser = new SimpleTupleSerializer(hadoopSer);
    SimpleTupleDeserializer deser = new SimpleTupleDeserializer(tuple.getSchema(), hadoopSer, getConf());

    DataOutputBuffer output = new DataOutputBuffer();
    ser.open(output);//from  www. jav  a 2  s.c o m
    ser.serialize(tuple);
    ser.close();

    DataInputBuffer input = new DataInputBuffer();
    input.reset(output.getData(), 0, output.getLength());
    ITuple deserializedTuple = null;

    deser.open(input);
    deserializedTuple = deser.deserialize(null);
    deser.close();

    if (debug) {
        System.out.println("D:" + tuple);
    }
    assertEquals(tuple, deserializedTuple);
}