Example usage for org.apache.hadoop.io IntWritable get

List of usage examples for org.apache.hadoop.io IntWritable get

Introduction

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

Prototype

public int get() 

Source Link

Document

Return the value of this IntWritable.

Usage

From source file:org.apache.giraph.examples.ConnectedComponentsVertex.java

License:Apache License

/**
 * Propagates the smallest vertex id to all neighbors. Will always choose to
 * halt and only reactivate if a smaller id has been sent to it.
 *
 * @param messages Iterator of messages from the previous superstep.
 * @throws IOException//from   w  w w . j a va2 s  . c om
 */
@Override
public void compute(Iterable<IntWritable> messages) throws IOException {
    int currentComponent = getValue().get();

    // First superstep is special, because we can simply look at the neighbors
    if (getSuperstep() == 0) {
        for (Edge<IntWritable, NullWritable> edge : getEdges()) {
            int neighbor = edge.getTargetVertexId().get();
            if (neighbor < currentComponent) {
                currentComponent = neighbor;
            }
        }
        // Only need to send value if it is not the own id
        if (currentComponent != getValue().get()) {
            setValue(new IntWritable(currentComponent));
            for (Edge<IntWritable, NullWritable> edge : getEdges()) {
                IntWritable neighbor = edge.getTargetVertexId();
                if (neighbor.get() > currentComponent) {
                    sendMessage(neighbor, getValue());
                }
            }
        }

        voteToHalt();
        return;
    }

    boolean changed = false;
    // did we get a smaller id ?
    for (IntWritable message : messages) {
        int candidateComponent = message.get();
        if (candidateComponent < currentComponent) {
            currentComponent = candidateComponent;
            changed = true;
        }
    }

    // propagate new component id to the neighbors
    if (changed) {
        setValue(new IntWritable(currentComponent));
        sendMessageToAllEdges(getValue());
    }
    voteToHalt();
}

From source file:org.apache.giraph.examples.MinimumIntCombiner.java

License:Apache License

@Override
public Iterable<IntWritable> combine(IntWritable target, Iterable<IntWritable> messages) throws IOException {
    int minimum = Integer.MAX_VALUE;
    for (IntWritable message : messages) {
        if (message.get() < minimum) {
            minimum = message.get();/*from  w w w  .  j a v  a2 s  .c o  m*/
        }
    }
    List<IntWritable> value = new ArrayList<IntWritable>();
    value.add(new IntWritable(minimum));

    return value;
}

From source file:org.apache.giraph.examples.MinimumIntCombinerTest.java

License:Apache License

@Test
public void testCombiner() throws Exception {
    MessageCombiner<WritableComparable, IntWritable> messageCombiner = new MinimumIntMessageCombiner();

    IntWritable vertexId = new IntWritable(1);
    IntWritable result = messageCombiner.createInitialMessage();
    messageCombiner.combine(vertexId, result, new IntWritable(39947466));
    messageCombiner.combine(vertexId, result, new IntWritable(199));
    messageCombiner.combine(vertexId, result, new IntWritable(42));
    messageCombiner.combine(vertexId, result, new IntWritable(19998888));
    assertEquals(42, result.get());
}

From source file:org.apache.giraph.examples.MinimumSpanningTreeComputation.java

License:Apache License

@Override
public void compute(Vertex<LongWritable, MSTVertexValue, MSTEdgeValue> vertex, Iterable<MSTMessage> messages) {

    if (getSuperstep() == 0) {
        // if we are unconnected, just terminate
        if (vertex.getNumEdges() == 0) {
            vertex.voteToHalt();// w w w . j a  v  a 2 s  .  c o m
            return;
        }

        // need to set up correct number of supervertices on first superstep
        aggregate(SUPERVERTEX_AGG, PLUS_ONE);
    }

    IntWritable phaseInt = getAggregatedValue(PHASE_AGG);
    MSTPhase phase = MSTPhase.VALUES[phaseInt.get()];

    // phase transitions are in master.compute()
    // algorithm termination is in phase4B

    switch (phase) {
    case PHASE_1:
        //LOG.info(vertex.getId() + ": phase 1");
        //for (Edge<LongWritable, MSTEdgeValue> edge : vertex.getEdges()) {
        //  LOG.info("  edges to " +
        //           edge.getTargetVertexId() + " with " + edge.getValue());
        //}

        phase1(vertex);
        // fall through

    case PHASE_2A:
        //LOG.info(vertex.getId() + ": phase 2A");
        phase2A(vertex);
        break;

    case PHASE_2B:
        //LOG.info(vertex.getId() + ": phase 2B");
        phase2B(vertex, messages);
        break;

    case PHASE_3A:
        //LOG.info(vertex.getId() + ": phase 3A");
        phase3A(vertex);
        break;

    case PHASE_3B:
        //LOG.info(vertex.getId() + ": phase 3B");
        phase3B(vertex, messages);
        // fall through

    case PHASE_4A:
        //LOG.info(vertex.getId() + ": phase 4A");
        phase4A(vertex);
        break;

    case PHASE_4B:
        //LOG.info(vertex.getId() + ": phase 4B");
        phase4B(vertex, messages);
        break;

    default:
        LOG.error("Invalid computation phase.");
    }
}

From source file:org.apache.giraph.examples.MinimumSpanningTreeVertex.java

License:Apache License

@Override
public void compute(Iterable<MSTMessage> messages) {
    if (getSuperstep() == 0) {
        // if we are unconnected, just terminate
        if (getNumEdges() == 0) {
            voteToHalt();//from  ww  w  .ja  va  2s.  c o  m
            return;
        }

        // need to set up correct number of supervertices on first superstep
        aggregate(SUPERVERTEX_AGG, new LongWritable(1));
    }

    IntWritable phaseInt = getAggregatedValue(PHASE_AGG);
    MSTPhase phase = MSTPhase.VALUES[phaseInt.get()];

    // phase transitions are in master.compute()
    // algorithm termination is in phase4B

    switch (phase) {
    case PHASE_1:
        //LOG.info(getId() + ": phase 1");
        //for (Edge<LongWritable, MSTEdgeValue> edge : getEdges()) {
        //  LOG.info("  edges to " +
        //           edge.getTargetVertexId() + " with " + edge.getValue());
        //}

        phase1();
        // fall through

    case PHASE_2A:
        //LOG.info(getId() + ": phase 2A");
        phase2A();
        break;

    case PHASE_2B:
        //LOG.info(getId() + ": phase 2B");
        phase2B(messages);
        break;

    case PHASE_3A:
        //LOG.info(getId() + ": phase 3A");
        phase3A();
        break;

    case PHASE_3B:
        //LOG.info(getId() + ": phase 3B");
        phase3B(messages);
        // fall through

    case PHASE_4A:
        //LOG.info(getId() + ": phase 4A");
        phase4A();
        break;

    case PHASE_4B:
        //LOG.info(getId() + ": phase 4B");
        phase4B(messages);
        break;

    default:
        LOG.error("Invalid computation phase.");
    }
}

From source file:org.apache.giraph.examples.MSTAsyncComputation.java

License:Apache License

@Override
public void compute(Vertex<LongWritable, MSTVertexValue, MSTEdgeValue> vertex, Iterable<MSTMessage> messages) {

    if (getSuperstep() == 0) {
        // if we are unconnected, just terminate
        if (vertex.getNumEdges() == 0) {
            vertex.voteToHalt();//from   ww w .java2 s. c  o  m
            return;
        }

        // need to set up correct number of supervertices on first superstep
        aggregate(SUPERVERTEX_AGG, PLUS_ONE);
    }

    IntWritable phaseInt = getAggregatedValue(PHASE_AGG);
    MSTPhase phase = MSTPhase.VALUES[phaseInt.get()];

    // phase transitions are in master.compute()
    // algorithm termination is in phase4B

    switch (phase) {
    case PHASE_1:
        //LOG.info(vertex.getId() + ": phase 1");
        //for (Edge<LongWritable, MSTEdgeValue> edge : vertex.getEdges()) {
        //  LOG.info("  edges to " +
        //           edge.getTargetVertexId() + " with " + edge.getValue());
        //}

        phase1(vertex);
        // fall through

    case PHASE_2A:
        //LOG.info(vertex.getId() + ": phase 2A");
        setMessagesAreForNextPhase(true);
        phase2A(vertex);
        break;

    case PHASE_2B:
        //LOG.info(vertex.getId() + ": phase 2B");
        setMessagesAreForNextPhase(false);
        phase2B(vertex, messages);
        break;

    case PHASE_3A:
        //LOG.info(vertex.getId() + ": phase 3A");
        setMessagesAreForNextPhase(true);
        phase3A(vertex);
        break;

    case PHASE_3B:
        //LOG.info(vertex.getId() + ": phase 3B");
        phase3B(vertex, messages);
        // fall through

    case PHASE_4A:
        //LOG.info(vertex.getId() + ": phase 4A");
        setMessagesAreForNextPhase(true);
        phase4A(vertex);
        break;

    case PHASE_4B:
        //LOG.info(vertex.getId() + ": phase 4B");
        phase4B(vertex, messages);
        break;

    default:
        LOG.error("Invalid computation phase.");
    }
}

From source file:org.apache.giraph.examples.scc.SccPhaseMasterCompute.java

License:Apache License

/**
 * Helper function to convert from internal aggregated value to a Phases
 * enumerator./*from w ww .  j a va 2  s . com*/
 * @param phaseInt
 *          An integer that matches a position in the Phases enumerator.
 * @return A Phases' item for the given position.
 */
public static Phases getPhase(IntWritable phaseInt) {
    return Phases.values()[phaseInt.get()];
}

From source file:org.apache.giraph.examples.SimpleCombinerComputation.java

License:Apache License

@Override
public void compute(Vertex<LongWritable, IntWritable, FloatWritable> vertex, Iterable<IntWritable> messages)
        throws IOException {
    if (vertex.getId().equals(new LongWritable(2))) {
        sendMessage(new LongWritable(1), new IntWritable(101));
        sendMessage(new LongWritable(1), new IntWritable(102));
        sendMessage(new LongWritable(1), new IntWritable(103));
    }//from  ww w .java  2 s . co  m
    if (!vertex.getId().equals(new LongWritable(1))) {
        vertex.voteToHalt();
    } else {
        // Check the messages
        int sum = 0;
        int num = 0;
        for (IntWritable message : messages) {
            sum += message.get();
            num++;
        }
        LOG.info("TestCombinerVertex: Received a sum of " + sum
                + " (should have 306 with a single message value)");

        if (num == 1 && sum == 306) {
            vertex.voteToHalt();
        }
    }
    if (getSuperstep() > 3) {
        throw new IllegalStateException("TestCombinerVertex: Vertex 1 failed to receive " + "messages in time");
    }
}

From source file:org.apache.giraph.examples.SimpleCombinerVertex.java

License:Apache License

@Override
public void compute(Iterable<IntWritable> messages) {
    if (getId().equals(new LongWritable(2))) {
        sendMessage(new LongWritable(1), new IntWritable(101));
        sendMessage(new LongWritable(1), new IntWritable(102));
        sendMessage(new LongWritable(1), new IntWritable(103));
    }//from  w  w  w .j a v  a 2s  .co  m
    if (!getId().equals(new LongWritable(1))) {
        voteToHalt();
    } else {
        // Check the messages
        int sum = 0;
        int num = 0;
        for (IntWritable message : messages) {
            sum += message.get();
            num++;
        }
        LOG.info("TestCombinerVertex: Received a sum of " + sum
                + " (should have 306 with a single message value)");

        if (num == 1 && sum == 306) {
            voteToHalt();
        }
    }
    if (getSuperstep() > 3) {
        throw new IllegalStateException("TestCombinerVertex: Vertex 1 failed to receive " + "messages in time");
    }
}

From source file:org.apache.giraph.examples.SimpleMsgComputation.java

License:Apache License

@Override
public void compute(Vertex<LongWritable, IntWritable, FloatWritable> vertex, Iterable<IntWritable> messages)
        throws IOException {
    if (vertex.getId().equals(new LongWritable(2))) {
        sendMessage(new LongWritable(1), new IntWritable(101));
        sendMessage(new LongWritable(1), new IntWritable(102));
        sendMessage(new LongWritable(1), new IntWritable(103));
    }//from  w  w  w  . java  2s  .  c o  m
    if (!vertex.getId().equals(new LongWritable(1))) {
        vertex.voteToHalt();
    } else {
        /* Check the messages */
        int sum = 0;
        for (IntWritable message : messages) {
            sum += message.get();
        }
        LOG.info("compute: Received a sum of " + sum + " (will stop on 306)");

        if (sum == 306) {
            vertex.voteToHalt();
        }
    }
    if (getSuperstep() > 3) {
        System.err.println("compute: Vertex 1 failed to receive " + "messages in time");
        vertex.voteToHalt();
    }
}