Example usage for org.apache.hadoop.io Text readFields

List of usage examples for org.apache.hadoop.io Text readFields

Introduction

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

Prototype

@Override
public void readFields(DataInput in) throws IOException 

Source Link

Document

deserialize

Usage

From source file:com.chinamobile.bcbsp.bspcontroller.BSPController.java

License:Apache License

/**
 * kill staffs of the job in schedule process whose staffs haven't
 * schedule over/*w  ww . ja va  2  s .c  o  m*/
 * @param queueManager
 *        queueManager that handle the jobs in the waitQueues
 * @throws IOException
 *         exceptions haappened during handle hdfs file.
 */
public void killStaffInScheduleing(QueueManager queueManager) throws IOException {
    // String WAIT_QUEUE = "waitQueue";
    // Queue<JobInProgress> waitQueue = queueManager.findQueue(WAIT_QUEUE);
    Collection<JobInProgress> jobsInWaitQueue = queueManager.getJobs();
    if (this.haLogOperator.isExist(conf.get(Constants.BC_BSP_HA_LOG_DIR) + Constants.BC_BSP_HA_SCHEDULE_LOG)) {
        // FSDataInputStream in = this.haLogOperator.readFile(conf
        // .get(Constants.BC_BSP_HA_LOG_DIR)
        // + Constants.BC_BSP_HA_SCHEDULE_LOG);
        BSPFSDataInputStream bspin = new BSPFSDataInputStreamImpl(haLogOperator,
                conf.get(Constants.BC_BSP_HA_LOG_DIR) + Constants.BC_BSP_HA_SCHEDULE_LOG);
        if (bspin != null) {
            String jobid = bspin.readUTF();
            bspin = null;
            for (JobInProgress jip : jobsInWaitQueue) {
                if (jip.getJobID().equals(new BSPJobID().forName(jobid))) {
                    ArrayList<WorkerManagerStatus> wmsl = new ArrayList<WorkerManagerStatus>();
                    // in = this.haLogOperator.readFile(conf
                    // .get(Constants.BC_BSP_HA_LOG_DIR)
                    // + jip.getJobID().toString());
                    BSPFSDataInputStream bspIn = new BSPFSDataInputStreamImpl(haLogOperator,
                            conf.get(Constants.BC_BSP_HA_LOG_DIR) + jip.getJobID().toString());
                    Text loaFactor = new Text();
                    loaFactor.readFields(bspIn.getIn());
                    while (bspIn != null) {
                        try {
                            WorkerManagerStatus wmStatus = new WorkerManagerStatus();
                            wmStatus.readFields(bspIn.getIn());
                            wmsl.add(wmStatus);
                        } catch (EOFException e) {
                            bspIn = null;
                        }
                    }
                    // recovery the jobInprogress state
                    StaffInProgress[] staffs = jip.getStaffInProgress();
                    for (int i = 0; i < staffs.length; i++) {
                        if (!staffs[i].isRunning() && !staffs[i].isComplete()) {
                            Staff t = jip.obtainNewStaff(wmsl, i, Double.parseDouble(loaFactor.toString()));
                            WorkerManagerStatus wmss = staffs[i].getWorkerManagerStatus();
                            jip.updateStaffStatus(staffs[i],
                                    new StaffStatus(jip.getJobID(), staffs[i].getStaffID(), 0,
                                            StaffStatus.State.UNASSIGNED, "running",
                                            wmss.getWorkerManagerName(), StaffStatus.Phase.STARTING));
                            // update the WorkerManagerStatus Cache
                            wmss.setRunningStaffsCount(wmss.getRunningStaffsCount() + 1);
                            LOG.info("debug: kill staffs of the job in schedule process"
                                    + "whose staffs haven't schedule over");
                            this.updateWhiteWorkerManagersKey(wmss, wmss);
                            LOG.info(t.getStaffAttemptId() + " is divided to the "
                                    + wmss.getWorkerManagerName());
                        }
                    }
                    this.killJob(jip);
                }
            }
        }
    }
}

From source file:com.chinamobile.bcbsp.bspcontroller.BSPController.java

License:Apache License

/**
 * start all of the job that have already in running queue
 * @param queueManager//from  ww w . j ava2s.com
 *        queueManager that handle the processing queue.
 * @throws IOException
 *         exceptions during handle hdfs log.
 */
public void startAllRunningJob(QueueManager queueManager) throws IOException {
    String PROCESSING_QUEUE = "processingQueue";
    Queue<JobInProgress> processingQueue = queueManager.findQueue(PROCESSING_QUEUE);
    Collection<JobInProgress> jobs = processingQueue.getJobs();
    for (JobInProgress jip : jobs) {
        Collection<WorkerManagerStatus> wmlist = null;
        ArrayList<WorkerManagerStatus> wmsl = new ArrayList<WorkerManagerStatus>();
        // FSDataInputStream in = this.haLogOperator.readFile(conf
        // .get(Constants.BC_BSP_HA_LOG_DIR)
        // + jip.getJobID().toString());
        BSPFSDataInputStream bspin = new BSPFSDataInputStreamImpl(haLogOperator,
                conf.get(Constants.BC_BSP_HA_LOG_DIR) + jip.getJobID().toString());
        Text loaFactor = new Text();
        loaFactor.readFields(bspin.getIn());
        while (bspin != null) {
            try {
                WorkerManagerStatus wmStatus = new WorkerManagerStatus();
                wmStatus.readFields(bspin.getIn());
                wmsl.add(wmStatus);
            } catch (EOFException e) {
                bspin = null;
            }
        }
        wmlist = wmsl;
        // LOG.info("wmlist size=" + wmsl.size());
        // recovery the jobInprogress state
        StaffInProgress[] staffs = jip.getStaffInProgress();
        for (int i = 0; i < staffs.length; i++) {
            if (!staffs[i].isRunning() && !staffs[i].isComplete()) {
                Staff t = jip.obtainNewStaff(wmlist, i, Double.parseDouble(loaFactor.toString()));
                WorkerManagerStatus wmss = staffs[i].getWorkerManagerStatus();
                jip.updateStaffStatus(staffs[i],
                        new StaffStatus(jip.getJobID(), staffs[i].getStaffID(), 0, StaffStatus.State.UNASSIGNED,
                                "running", wmss.getWorkerManagerName(), StaffStatus.Phase.STARTING));
                // update the WorkerManagerStatus Cache
                wmss.setRunningStaffsCount(wmss.getRunningStaffsCount() + 1);
                // LOG.info("debug: start all the running job");
                this.updateWhiteWorkerManagersKey(wmss, wmss);
                LOG.info(t.getStaffAttemptId() + " is divided to the " + wmss.getWorkerManagerName());
            }
        }
        jip.getGssc().setCurrentSuperStep();
        // LOG.info("before jip.getGssc().start(); ");
        jip.getGssc().setCheckNumBase();
        jip.getGssc().start();
    }
}

From source file:com.chinamobile.bcbsp.partition.HashWithBalancerWritePartition.java

License:Apache License

/**
 * This method is used to partition graph vertexes. Writing Each vertex to the
 * corresponding partition. In this method calls recordParse method to create
 * an HeadNode object. The last call partitioner's getPartitionId method to
 * calculate the HeadNode belongs to partition's id. If the HeadNode belongs
 * local partition then written to the local partition or send it to the
 * appropriate partition.//from   ww w  .java  2 s.c om
 * @param recordReader The recordreader of the split.
 * @throws IOException The io exception
 * @throws InterruptedException The Interrupted Exception
 */
@Override
public void write(RecordReader recordReader) throws IOException, InterruptedException {
    int headNodeNum = 0;
    int local = 0;
    int send = 0;
    int lost = 0;
    ThreadPool tpool = new ThreadPool(this.sendThreadNum);
    int staffNum = this.staff.getStaffNum();
    BytesWritable kbytes = new BytesWritable();
    int ksize = 0;
    BytesWritable vbytes = new BytesWritable();
    int vsize = 0;
    DataOutputBuffer bb = new DataOutputBuffer();
    int bufferSize = (int) ((this.TotalCacheSize * CONTAINERNUMBER * CONTAINERNUMBER) * PART);
    int dataBufferSize = (this.TotalCacheSize * CONTAINERNUMBER * CONTAINERNUMBER)
            / (this.staff.getStaffNum() + this.sendThreadNum);
    byte[] buffer = new byte[bufferSize];
    int bufindex = 0;
    SerializationFactory sFactory = new SerializationFactory(new Configuration());
    Serializer<IntWritable> psserializer = sFactory.getSerializer(IntWritable.class);
    byte[] pidandsize = new byte[TIME * CONTAINERNUMBER * CONTAINERNUMBER];
    int psindex = 0;
    BytesWritable pidbytes = new BytesWritable();
    int psize = 0;
    BytesWritable sizebytes = new BytesWritable();
    int ssize = 0;
    try {
        this.keyserializer.open(bb);
        this.valueserializer.open(bb);
        psserializer.open(bb);
    } catch (IOException e) {
        throw e;
    }
    String path = "/tmp/bcbsp/" + this.staff.getJobID() + "/" + this.staff.getStaffID();
    File dir = new File("/tmp/bcbsp/" + this.staff.getJobID());
    dir.mkdir();
    dir = new File("/tmp/bcbsp/" + this.staff.getJobID() + "/" + this.staff.getStaffID());
    dir.mkdir();
    ArrayList<File> files = new ArrayList<File>();
    try {
        File file = new File(path + "/" + "data" + ".txt");
        files.add(file);
        DataOutputStream dataWriter = new DataOutputStream(
                new BufferedOutputStream(new FileOutputStream(path + "/" + "data" + ".txt", true)));
        DataInputStream dataReader = new DataInputStream(
                new BufferedInputStream(new FileInputStream(path + "/" + "data" + ".txt")));
        File filet = new File(path + "/" + "pidandsize" + ".txt");
        files.add(filet);
        DataOutputStream psWriter = new DataOutputStream(
                new BufferedOutputStream(new FileOutputStream(path + "/" + "pidandsize" + ".txt", true)));
        DataInputStream psReader = new DataInputStream(
                new BufferedInputStream(new FileInputStream(path + "/" + "pidandsize" + ".txt")));
        while (recordReader != null && recordReader.nextKeyValue()) {
            headNodeNum++;
            Text key = new Text(recordReader.getCurrentKey().toString());
            Text value = new Text(recordReader.getCurrentValue().toString());
            int pid = -1;
            Text vertexID = this.recordParse.getVertexID(key);
            if (vertexID != null) {
                pid = this.partitioner.getPartitionID(vertexID);
            } else {
                lost++;
                continue;
            }
            if (this.counter.containsKey(pid)) {
                this.counter.put(pid, (this.counter.get(pid) + 1));
            } else {
                this.counter.put(pid, 1);
            }
            bb.reset();
            this.keyserializer.serialize(key);
            kbytes.set(bb.getData(), 0, bb.getLength());
            ksize = kbytes.getLength();
            bb.reset();
            this.valueserializer.serialize(value);
            vbytes.set(bb.getData(), 0, bb.getLength());
            vsize = vbytes.getLength();
            bb.reset();
            psserializer.serialize(new IntWritable(ksize + vsize));
            sizebytes.set(bb.getData(), 0, bb.getLength());
            ssize = sizebytes.getLength();
            bb.reset();
            psserializer.serialize(new IntWritable(pid));
            pidbytes.set(bb.getData(), 0, bb.getLength());
            psize = pidbytes.getLength();
            if ((pidandsize.length - psindex) > (ssize + psize)) {
                System.arraycopy(sizebytes.getBytes(), 0, pidandsize, psindex, ssize);
                psindex += ssize;
                System.arraycopy(pidbytes.getBytes(), 0, pidandsize, psindex, psize);
                psindex += psize;
            } else {
                psWriter.write(pidandsize, 0, psindex);
                psindex = 0;
                System.arraycopy(sizebytes.getBytes(), 0, pidandsize, psindex, ssize);
                psindex += ssize;
                System.arraycopy(pidbytes.getBytes(), 0, pidandsize, psindex, psize);
                psindex += psize;
            }
            if ((buffer.length - bufindex) > (ksize + vsize)) {
                System.arraycopy(kbytes.getBytes(), 0, buffer, bufindex, ksize);
                bufindex += ksize;
                System.arraycopy(vbytes.getBytes(), 0, buffer, bufindex, vsize);
                bufindex += vsize;
            } else if (buffer.length < (ksize + vsize)) {
                dataWriter.write(buffer, 0, bufindex);
                bufindex = 0;
                LOG.info("This is a super record");
                dataWriter.write(kbytes.getBytes(), 0, ksize);
                dataWriter.write(vbytes.getBytes(), 0, vsize);
            } else {
                dataWriter.write(buffer, 0, bufindex);
                bufindex = 0;
                System.arraycopy(kbytes.getBytes(), 0, buffer, bufindex, ksize);
                bufindex += ksize;
                System.arraycopy(vbytes.getBytes(), 0, buffer, bufindex, vsize);
                bufindex += vsize;
            }
        }
        if (psindex != 0) {
            psWriter.write(pidandsize, 0, psindex);
        }
        if (bufindex != 0) {
            dataWriter.write(buffer, 0, bufindex);
            bufindex = 0;
        }
        dataWriter.close();
        dataWriter = null;
        psWriter.close();
        psWriter = null;
        buffer = null;
        pidandsize = null;
        this.ssrc.setDirFlag(new String[] { "3" });
        this.ssrc.setCounter(this.counter);
        HashMap<Integer, Integer> hashBucketToPartition = this.sssc.loadDataInBalancerBarrier(ssrc,
                Constants.PARTITION_TYPE.HASH);
        this.staff.setHashBucketToPartition(hashBucketToPartition);
        byte[][] databuf = new byte[staffNum][dataBufferSize];
        int[] databufindex = new int[staffNum];
        try {
            IntWritable pid = new IntWritable();
            IntWritable size = new IntWritable();
            int belongPid = 0;
            while (true) {
                size.readFields(psReader);
                pid.readFields(psReader);
                belongPid = hashBucketToPartition.get(pid.get());
                if (belongPid != this.staff.getPartition()) {
                    send++;
                } else {
                    local++;
                }
                if ((databuf[belongPid].length - databufindex[belongPid]) > size.get()) {
                    dataReader.read(databuf[belongPid], databufindex[belongPid], size.get());
                    databufindex[belongPid] += size.get();
                } else if (databuf[belongPid].length < size.get()) {
                    LOG.info("This is a super record");
                    byte[] tmp = new byte[size.get()];
                    dataReader.read(tmp, 0, size.get());
                    if (belongPid == this.staff.getPartition()) {
                        DataInputStream reader = new DataInputStream(
                                new BufferedInputStream(new ByteArrayInputStream(tmp)));
                        try {
                            boolean stop = true;
                            while (stop) {
                                Text key = new Text();
                                key.readFields(reader);
                                Text value = new Text();
                                value.readFields(reader);
                                if (key.getLength() > 0 && value.getLength() > 0) {
                                    Vertex vertex = this.recordParse.recordParse(key.toString(),
                                            value.toString());
                                    if (vertex == null) {
                                        lost++;
                                        continue;
                                    }
                                    this.staff.getGraphData().addForAll(vertex);
                                } else {
                                    stop = false;
                                }
                            }
                        } catch (IOException e) {
                            LOG.info("IO exception: " + e.getStackTrace());
                        }
                    } else {
                        ThreadSignle t = tpool.getThread();
                        while (t == null) {
                            t = tpool.getThread();
                        }
                        t.setWorker(
                                this.workerAgent.getWorker(staff.getJobID(), staff.getStaffID(), belongPid));
                        t.setJobId(staff.getJobID());
                        t.setTaskId(staff.getStaffID());
                        t.setBelongPartition(belongPid);
                        BytesWritable data = new BytesWritable();
                        data.set(tmp, 0, size.get());
                        t.setData(data);
                        LOG.info("Using Thread is: " + t.getThreadNumber());
                        t.setStatus(true);
                    }
                    tmp = null;
                } else {
                    if (belongPid == this.staff.getPartition()) {
                        DataInputStream reader = new DataInputStream(new BufferedInputStream(
                                new ByteArrayInputStream(databuf[belongPid], 0, databufindex[belongPid])));
                        try {
                            boolean stop = true;
                            while (stop) {
                                Text key = new Text();
                                key.readFields(reader);
                                Text value = new Text();
                                value.readFields(reader);
                                if (key.getLength() > 0 && value.getLength() > 0) {
                                    Vertex vertex = this.recordParse.recordParse(key.toString(),
                                            value.toString());
                                    if (vertex == null) {
                                        lost++;
                                        continue;
                                    }
                                    this.staff.getGraphData().addForAll(vertex);
                                } else {
                                    stop = false;
                                }
                            }
                        } catch (IOException e) {
                            LOG.info("IO exception: " + e.getStackTrace());
                        }
                    } else {
                        ThreadSignle t = tpool.getThread();
                        while (t == null) {
                            t = tpool.getThread();
                        }
                        t.setWorker(
                                this.workerAgent.getWorker(staff.getJobID(), staff.getStaffID(), belongPid));
                        t.setJobId(staff.getJobID());
                        t.setTaskId(staff.getStaffID());
                        t.setBelongPartition(belongPid);
                        BytesWritable data = new BytesWritable();
                        data.set(databuf[belongPid], 0, databufindex[belongPid]);
                        t.setData(data);
                        LOG.info("Using Thread is: " + t.getThreadNumber());
                        t.setStatus(true);
                    }
                    databufindex[belongPid] = 0;
                    dataReader.read(databuf[belongPid], databufindex[belongPid], size.get());
                    databufindex[belongPid] += size.get();
                }
            }
        } catch (EOFException ex) {
            LOG.error("[write]", ex);
        }
        for (int i = 0; i < staffNum; i++) {
            if (databufindex[i] != 0) {
                if (i == this.staff.getPartition()) {
                    DataInputStream reader = new DataInputStream(
                            new BufferedInputStream(new ByteArrayInputStream(databuf[i], 0, databufindex[i])));
                    try {
                        boolean stop = true;
                        while (stop) {
                            Text key = new Text();
                            key.readFields(reader);
                            Text value = new Text();
                            value.readFields(reader);
                            if (key.getLength() > 0 && value.getLength() > 0) {
                                Vertex vertex = this.recordParse.recordParse(key.toString(), value.toString());
                                if (vertex == null) {
                                    lost++;
                                    continue;
                                }
                                this.staff.getGraphData().addForAll(vertex);
                            } else {
                                stop = false;
                            }
                        }
                    } catch (IOException e) {
                        LOG.info("IO exception: " + e.getStackTrace());
                    }
                } else {
                    ThreadSignle t = tpool.getThread();
                    while (t == null) {
                        t = tpool.getThread();
                    }
                    t.setWorker(this.workerAgent.getWorker(staff.getJobID(), staff.getStaffID(), i));
                    t.setJobId(staff.getJobID());
                    t.setTaskId(staff.getStaffID());
                    t.setBelongPartition(i);
                    BytesWritable data = new BytesWritable();
                    data.set(databuf[i], 0, databufindex[i]);
                    t.setData(data);
                    LOG.info("Using Thread is: " + t.getThreadNumber());
                    t.setStatus(true);
                }
            }
        }
        dataReader.close();
        dataReader = null;
        psReader.close();
        psReader = null;
        for (File f : files) {
            f.delete();
        }
        dir.delete();
        dir = new File(path.substring(0, path.lastIndexOf('/')));
        dir.delete();
        tpool.cleanup();
        tpool = null;
        databuf = null;
        databufindex = null;
        this.counter = null;
        LOG.info("The number of vertices that were read from the input file: " + headNodeNum);
        LOG.info("The number of vertices that were put into the partition: " + local);
        LOG.info("The number of vertices that were sent to other partitions: " + send);
        LOG.info("The number of verteices in the partition that cound not be " + "parsed:" + lost);
    } catch (IOException e) {
        throw e;
    } catch (InterruptedException e) {
        throw e;
    } finally {
        for (File f : files) {
            f.delete();
        }
        dir.delete();
        dir = new File(path.substring(0, path.lastIndexOf('/')));
        dir.delete();
    }
}

From source file:com.cloudera.crunch.type.writable.TextMapWritable.java

License:Open Source License

@Override
public void readFields(DataInput in) throws IOException {
    instance.clear();/*from w  ww .  j  a  va2s.  c o  m*/
    try {
        this.valueClazz = (Class<T>) Class.forName(Text.readString(in));
    } catch (ClassNotFoundException e) {
        throw (IOException) new IOException("Failed map init").initCause(e);
    }
    int entries = WritableUtils.readVInt(in);
    try {
        for (int i = 0; i < entries; i++) {
            Text txt = new Text();
            txt.readFields(in);
            T value = valueClazz.newInstance();
            value.readFields(in);
            instance.put(txt, value);
        }
    } catch (IllegalAccessException e) {
        throw (IOException) new IOException("Failed map init").initCause(e);
    } catch (InstantiationException e) {
        throw (IOException) new IOException("Failed map init").initCause(e);
    }
}

From source file:com.cloudera.science.matching.graph.VertexState.java

License:Open Source License

@Override
public void readFields(DataInput in) throws IOException {
    bidder = in.readBoolean();/*www.  j  av  a  2s.c o  m*/
    if (!bidder) {
        price = new BigDecimal(in.readUTF());
    }
    matchId.readFields(in);
    this.priceIndex = Maps.newHashMap();
    int sz = WritableUtils.readVInt(in);
    for (int i = 0; i < sz; i++) {
        Text vertexId = new Text();
        vertexId.readFields(in);
        String price = in.readUTF();
        priceIndex.put(vertexId, new BigDecimal(price));
    }
}

From source file:com.ikanow.aleph2.analytics.hadoop.assets.ObjectNodeWritableComparable.java

License:Apache License

@Override
public void readFields(DataInput in) throws IOException {
    final Text text = new Text();
    text.readFields(in);

    _object_node = (ObjectNode) _mapper.readTree(text.toString()); //(object node by construction)
}

From source file:com.inmobi.messaging.consumer.util.MessageUtil.java

License:Apache License

public static Text getTextMessage(byte[] line) throws IOException {
    Text text = new Text();
    ByteArrayInputStream bais = new ByteArrayInputStream(line);
    text.readFields(new DataInputStream(bais));
    return text;//w  ww . ja  va2  s . c o  m
}

From source file:com.intel.hadoop.graphbuilder.types.StringType.java

License:Open Source License

@Override
public void readFields(DataInput arg0) throws IOException {
    Text text = new Text();
    text.readFields(arg0);
    this.str = text.toString();
}

From source file:com.marcolotz.MRComponents.SerializerConverter.java

License:Creative Commons License

/***
 * This is a refactored manner to deserialize a String. Basically it creates
 * a new Text, reads the field and then return the new String;
 * /*from  www. j  av  a2s. co  m*/
 * @param dataInput
 * @return the string that was readen
 * @throws IOException
 */
public static String readString(DataInput dataInput) throws IOException {
    Text readenString = new Text();
    readenString.readFields(dataInput);
    return readenString.toString();
}

From source file:com.marklogic.contentpump.RDFWritable.java

License:Apache License

@SuppressWarnings("unchecked")
@Override/*from w ww. j  ava 2 s.c  om*/
public void readFields(DataInput in) throws IOException {
    byte hasCollection = in.readByte();
    if (hasCollection != 0) {
        Text t = new Text();
        t.readFields(in);
        graphUri = t.toString();
    }
    byte valueType = in.readByte();
    switch (valueType) {
    case 0:
        value = (VALUE) new Text();
        ((Text) value).readFields(in);
        break;
    case 1:
        value = (VALUE) new MarkLogicNode();
        ((MarkLogicNode) value).readFields(in);
        break;
    case 2:
        value = (VALUE) new BytesWritable();
        ((BytesWritable) value).readFields(in);
        break;
    default:
        throw new IOException("incorrect type");
    }
    type = valueType;
    byte hasPerms = in.readByte();
    if (hasPerms != 0) {
        int length = hasPerms;
        permissions = new ContentPermission[length];
        for (int i = 0; i < length; i++) {
            Text t = new Text();
            t.readFields(in);
            String role = t.toString();
            t.readFields(in);
            String perm = t.toString();
            ContentCapability capability = null;
            if (perm.equalsIgnoreCase(ContentCapability.READ.toString())) {
                capability = ContentCapability.READ;
            } else if (perm.equalsIgnoreCase(ContentCapability.EXECUTE.toString())) {
                capability = ContentCapability.EXECUTE;
            } else if (perm.equalsIgnoreCase(ContentCapability.INSERT.toString())) {
                capability = ContentCapability.INSERT;
            } else if (perm.equalsIgnoreCase(ContentCapability.UPDATE.toString())) {
                capability = ContentCapability.UPDATE;
            } else {
                LOG.error("Illegal permission: " + perm);
            }
            permissions[i] = new ContentPermission(capability, role);
        }

    }
}