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.comm.RequestTest.java

License:Apache License

@Test
public void sendWorkerMessagesRequest() throws IOException {
    // Data to send
    PairList<Integer, VertexIdMessages<IntWritable, IntWritable>> dataToSend = new PairList<>();
    dataToSend.initialize();/*from w w  w.  j av a2  s.  c  om*/
    int partitionId = 0;
    ByteArrayVertexIdMessages<IntWritable, IntWritable> vertexIdMessages = new ByteArrayVertexIdMessages<>(
            new TestMessageValueFactory<>(IntWritable.class));
    vertexIdMessages.setConf(conf);
    vertexIdMessages.initialize();
    dataToSend.add(partitionId, vertexIdMessages);
    for (int i = 1; i < 7; ++i) {
        IntWritable vertexId = new IntWritable(i);
        for (int j = 0; j < i; ++j) {
            vertexIdMessages.add(vertexId, new IntWritable(j));
        }
    }

    // Send the request
    SendWorkerMessagesRequest<IntWritable, IntWritable> request = new SendWorkerMessagesRequest<>(dataToSend);
    request.setConf(conf);

    client.sendWritableRequest(workerInfo.getTaskId(), request);
    client.waitAllRequests();

    // Stop the service
    client.stop();
    server.stop();

    // Check the output
    Iterable<IntWritable> vertices = serverData.getIncomingMessageStore().getPartitionDestinationVertices(0);
    int keySum = 0;
    int messageSum = 0;
    for (IntWritable vertexId : vertices) {
        keySum += vertexId.get();
        Iterable<IntWritable> messages = serverData.<IntWritable>getIncomingMessageStore()
                .getVertexMessages(vertexId);
        synchronized (messages) {
            for (IntWritable message : messages) {
                messageSum += message.get();
            }
        }
    }
    assertEquals(21, keySum);
    assertEquals(35, messageSum);
}

From source file:org.apache.giraph.comm.RequestTest.java

License:Apache License

@Test
public void sendWorkerIndividualMessagesRequest() throws IOException {
    // Data to send
    ByteArrayOneMessageToManyIds<IntWritable, IntWritable> dataToSend = new ByteArrayOneMessageToManyIds<>(
            new TestMessageValueFactory<>(IntWritable.class));
    dataToSend.setConf(conf);/*from   w w w  .java 2 s.co  m*/
    dataToSend.initialize();
    ExtendedDataOutput output = conf.createExtendedDataOutput();
    for (int i = 1; i <= 7; ++i) {
        IntWritable vertexId = new IntWritable(i);
        vertexId.write(output);
    }
    dataToSend.add(output.getByteArray(), output.getPos(), 7, new IntWritable(1));

    // Send the request
    SendWorkerOneMessageToManyRequest<IntWritable, IntWritable> request = new SendWorkerOneMessageToManyRequest<>(
            dataToSend, conf);
    client.sendWritableRequest(workerInfo.getTaskId(), request);
    client.waitAllRequests();

    // Stop the service
    client.stop();
    server.stop();

    // Check the output
    Iterable<IntWritable> vertices = serverData.getIncomingMessageStore().getPartitionDestinationVertices(0);
    int keySum = 0;
    int messageSum = 0;
    for (IntWritable vertexId : vertices) {
        keySum += vertexId.get();
        Iterable<IntWritable> messages = serverData.<IntWritable>getIncomingMessageStore()
                .getVertexMessages(vertexId);
        synchronized (messages) {
            for (IntWritable message : messages) {
                messageSum += message.get();
            }
        }
    }
    assertEquals(28, keySum);
    assertEquals(7, messageSum);
}

From source file:org.apache.giraph.debugger.examples.instrumented.BuggyConnectedComponentsDebugComputationModified.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 vertex//from   w w  w .  jav  a  2s .c  om
 *          Vertex
 * @param messages
 *          Iterator of messages from the previous superstep.
 * @throws IOException
 */
@Override
public void compute(Vertex<IntWritable, IntWritable, NullWritable> vertex, Iterable<IntWritable> messages)
        throws IOException {
    int currentComponent = vertex.getValue().get();

    if (getSuperstep() == 0) {
        vertex.setValue(new IntWritable(currentComponent));
        for (Edge<IntWritable, NullWritable> edge : vertex.getEdges()) {
            sendMessage(edge.getTargetVertexId(), vertex.getValue());
        }
        vertex.voteToHalt();
        return;
    }

    boolean changed = false;
    // did we get a smaller id ?
    for (IntWritable message : messages) {
        int candidateComponent = message.get();
        // INTENTIONAL BUG: in the original algorithm the value of the comparison
        // sign should be <.
        if (candidateComponent > currentComponent) {
            System.out.print("changing value in superstep: " + getSuperstep() + " vertex.id: " + vertex.getId()
                    + " newComponent: " + candidateComponent + "\n");
            currentComponent = candidateComponent;
            changed = true;
        }
    }

    // propagate new component id to the neighbors
    if (changed) {
        vertex.setValue(new IntWritable(currentComponent));
        for (Edge<IntWritable, NullWritable> edge : vertex.getEdges()) {
            sendMessage(edge.getTargetVertexId(), vertex.getValue());
        }
    }
    vertex.voteToHalt();
}

From source file:org.apache.giraph.debugger.examples.randomwalk.RandomWalkComputation.java

License:Apache License

@Override
public void compute(Vertex<LongWritable, IntWritable, NullWritable> vertex, Iterable<IntWritable> messages)
        throws IOException {
    // Halt after the walk reaches a certain length.
    if (getSuperstep() > lengthOfWalk) {
        vertex.voteToHalt();//from w w  w  . j  ava  2 s.  c  o  m
        return;
    }
    short numWalkersHere = 0;
    if (getSuperstep() == 0) {
        // At the first superstep, start from an initial number of walkers.
        numWalkersHere += initialNumWalkers;
    } else {
        // Otherwise, count the number of walkers arrived at this vertex.
        for (IntWritable messageValue : messages) {
            numWalkersHere += messageValue.get();
        }
    }
    vertex.setValue(new IntWritable(numWalkersHere));
    moveWalkersToNeighbors(numWalkersHere, vertex);
}

From source file:org.apache.giraph.debugger.examples.randomwalk.RandomWalkDebugConfig.java

License:Apache License

@Override
public boolean isVertexValueCorrect(LongWritable vertexId, IntWritable value) {
    return value.get() > 0;
}

From source file:org.apache.giraph.debugger.examples.randomwalk.RandomWalkDebugConfig.java

License:Apache License

@Override
public boolean isMessageCorrect(LongWritable srcId, LongWritable dstId, IntWritable message, long superstepNo) {
    return message.get() > 0;
}

From source file:org.apache.giraph.debugger.examples.randomwalk.RandomWalkVertexValueConstraintDebugConfig.java

License:Apache License

@Override
public boolean isVertexValueCorrect(LongWritable vertexId, IntWritable value) {
    return value.get() >= 0;
}

From source file:org.apache.giraph.edge.IntNullArrayEdges.java

License:Apache License

@Override
public void remove(IntWritable targetVertexId) {
    // Thanks to the constant-time implementation of removeAt(int),
    // we can remove all matching edges in linear time.
    for (int i = neighbors.size() - 1; i >= 0; --i) {
        if (neighbors.getInt(i) == targetVertexId.get()) {
            removeAt(i);// w  w w .j  a  v  a  2 s .c  om
        }
    }
}

From source file:org.apache.giraph.edge.primitives.IntEdgeStore.java

License:Apache License

@Override
protected OutEdges<IntWritable, E> getVertexOutEdges(VertexIdEdgeIterator<IntWritable, E> vertexIdEdgeIterator,
        Map<Integer, OutEdges<IntWritable, E>> partitionEdgesIn) {
    Int2ObjectMap<OutEdges<IntWritable, E>> partitionEdges = (Int2ObjectMap<OutEdges<IntWritable, E>>) partitionEdgesIn;
    IntWritable vertexId = vertexIdEdgeIterator.getCurrentVertexId();
    OutEdges<IntWritable, E> outEdges = partitionEdges.get(vertexId.get());
    if (outEdges == null) {
        synchronized (partitionEdges) {
            outEdges = partitionEdges.get(vertexId.get());
            if (outEdges == null) {
                outEdges = configuration.createAndInitializeInputOutEdges();
                partitionEdges.put(vertexId.get(), outEdges);
            }/*from ww  w .  j av a2  s.com*/
        }
    }
    return outEdges;
}

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

License:Apache License

@Override
public void compute(Vertex<IntWritable, IntWritable, NullWritable> vertex, Iterable<IntWritable> messages)
        throws IOException {
    boolean changed = false;
    for (IntWritable message : messages) {
        if (vertex.getValue().get() < message.get()) {
            vertex.setValue(message);/*  w  w w.j  a  va 2  s .  co  m*/
            changed = true;
        }
    }
    if (getSuperstep() == 0 || changed) {
        sendMessageToAllEdges(vertex, vertex.getValue());
    }
    vertex.voteToHalt();
}