Example usage for org.apache.hadoop.io LongWritable compareTo

List of usage examples for org.apache.hadoop.io LongWritable compareTo

Introduction

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

Prototype

@Override
public int compareTo(LongWritable o) 

Source Link

Document

Compares two LongWritables.

Usage

From source file:com.alexholmes.hadooputils.sort.LongArrayWritable.java

License:Apache License

public int compareTo(LongArrayWritable o) {
    Writable[] theseLongs = get();/*from  w w w .j av  a  2  s  .c o  m*/
    Writable[] thoseLongs = o.get();
    int len = Math.min(theseLongs.length, thoseLongs.length);
    for (int i = 0; i < len; i++) {
        LongWritable thisLong = (LongWritable) theseLongs[i];
        LongWritable thatLong = (LongWritable) thoseLongs[i];
        int comp = thisLong.compareTo(thatLong);
        if (comp != 0) {
            return comp;
        }
    }
    return 0;
}

From source file:it.uniroma1.bdc.tesi.piccioli.giraphstandalone.ksimplecycle.KSimpleCyclePlusPlus.java

License:Apache License

@Override
public void compute(Vertex<LongWritable, LongWritable, NullWritable> vertex,
        Iterable<CustomMessageWithPathPlusPlus> messages) throws IOException {

    int k = 3; //circuiti chiusi di lunghezza k
    k += 2; //add supersep aggiuntivi

    Iterable<Edge<LongWritable, NullWritable>> edges = vertex.getEdges();
    if (getSuperstep() == 0) {
        //calcolo degree e invio a vertici vicini
        LongWritable degree = new LongWritable(Iterables.size(edges));
        vertex.setValue(degree);/*from  w ww  .  j a v a 2 s  . c  o  m*/

        for (Edge<LongWritable, NullWritable> edge : edges) {
            this.sendMessage(edge.getTargetVertexId(),
                    new CustomMessageWithPathPlusPlus(vertex.getId(), degree));
        }

    } else if (getSuperstep() == 1) {

        //Ricevo Degree dai nodi vicini, elimino edge che collegano nodi "< degree minori"
        for (CustomMessageWithPathPlusPlus message : messages) {

            LongWritable messageValue = message.getValue();
            LongWritable vertexValue = vertex.getValue();
            LongWritable messageId = message.getId();
            LongWritable vertexId = vertex.getId();

            if ((messageValue.compareTo(vertexValue) < 0)
                    || ((messageValue.compareTo(vertexValue) == 0) && (messageId.compareTo(vertexId) < 0))) {
                this.removeEdgesRequest(messageId, vertexId);
            }
        }
    } else if (getSuperstep() == 2) {

        for (Edge<LongWritable, NullWritable> edge : vertex.getEdges()) {

            CustomMessageWithPathPlusPlus msg = new CustomMessageWithPathPlusPlus();

            msg.getVisitedVertex().add(vertex.getId());
            msg.setId(vertex.getId());

            sendMessage(edge.getTargetVertexId(), msg);
        }
    } else if (getSuperstep() > 2 && getSuperstep() < k) {

        for (CustomMessageWithPathPlusPlus message : messages) {
            if (!message.getVisitedVertex().contains(vertex.getId())) {
                for (Edge<LongWritable, NullWritable> edge : vertex.getEdges()) {
                    message.getVisitedVertex().add(vertex.getId());
                    sendMessage(edge.getTargetVertexId(), message);
                }
            }
        }

    } else if (getSuperstep() == k) {

        Long T = (long) 0;
        for (CustomMessageWithPathPlusPlus message : messages) {
            if (message.getId().compareTo(vertex.getId()) == 0) {
                T++;
            }

        }

        //            T = T / (2 * k);

        vertex.setValue(new LongWritable(T));
        vertex.voteToHalt();
        //            aggregate(SOMMA, new DoubleWritable(T));

    }
}

From source file:it.uniroma1.bdc.tesi.piccioli.giraphstandalone.trianglecount.longwritable.TriangleCountPlusPlus.java

License:Apache License

@Override
public void compute(Vertex<LongWritable, LongWritable, NullWritable> vertex,
        Iterable<MessageLongIdLongValue> messages) throws IOException {

    Iterable<Edge<LongWritable, NullWritable>> edges = vertex.getEdges();

    if (getSuperstep() == 0) {
        //calcolo degree e invio a vertici vicini
        LongWritable degree = new LongWritable(vertex.getNumEdges());
        vertex.setValue(degree);/*from   w w w  . j a  v  a  2 s  . c  om*/

        for (Edge<LongWritable, NullWritable> edge : edges) {
            this.sendMessage(edge.getTargetVertexId(), new MessageLongIdLongValue(vertex.getId(), degree));
        }
        //            this.sendMessageToAllEdges(vertex, new MessageLongIdLongValue(vertex.getId(), degree));

    } else if (getSuperstep() == 1) {

        //Ricevo Degree dai nodi vicini, elimino edge che collegano nodi "< degree minori"

        LongWritable vertexId = vertex.getId();
        LongWritable vertexValue = vertex.getValue();

        LongWritable messageId;
        LongWritable messageValue;

        for (MessageLongIdLongValue message : messages) {

            messageValue = message.getValue();
            messageId = message.getId();

            if ((messageValue.compareTo(vertexValue) < 0)
                    || ((messageValue.compareTo(vertexValue) == 0) && (messageId.compareTo(vertexId) < 0))) {
                this.removeEdgesRequest(messageId, vertexId);
            }
        }
    } else if (getSuperstep() == 2) {
        //triangle count
        for (Edge<LongWritable, NullWritable> edge : edges) {
            this.sendMessageToAllEdges(vertex,
                    new MessageLongIdLongValue(edge.getTargetVertexId(), new LongWritable()));
        }
    } else if (getSuperstep() == 3) {
        Integer T = 0;
        Set<Long> edgeMap = Sets.<Long>newHashSet();

        for (Edge<LongWritable, NullWritable> edge : edges) {
            edgeMap.add(edge.getTargetVertexId().get());
        }

        for (MessageLongIdLongValue message : messages) {
            if (edgeMap.contains(message.getId().get())) {
                T++;
            }
        }
        vertex.setValue(new LongWritable(T));
        aggregate(SOMMA + getSuperstep(), new LongWritable(T));
        vertex.voteToHalt();
    }

}

From source file:it.uniroma1.bdc.tesi.piccioli.giraphstandalone.trianglecount.longwritable.TriangleCountPlusPlusPhase1.java

License:Apache License

/**   Prima fase composta da i primi 2 superstep
 * 1 superstep - calcolo del degree di ogni nodo e invio info a nodi vicino
 * 2 superstep - elimino archi fuori ordinamento 
 * /*from   w  ww . j a va2 s  .  c o  m*/
 * @param vertex
 * @param messages
 * @throws java.io.IOException
 **/
@Override
public void compute(Vertex<LongWritable, LongWritable, NullWritable> vertex,
        Iterable<MessageLongIdLongValue> messages) throws IOException {

    Iterable<Edge<LongWritable, NullWritable>> edges = vertex.getEdges();

    if (getSuperstep() == 0) {
        //calcolo degree e invio a vertici vicini
        LongWritable degree = new LongWritable(vertex.getNumEdges());
        vertex.setValue(degree);

        for (Edge<LongWritable, NullWritable> edge : edges) {
            this.sendMessage(edge.getTargetVertexId(), new MessageLongIdLongValue(vertex.getId(), degree));
        }

    } else if (getSuperstep() == 1) {

        //Ricevo Degree dai nodi vicini, elimino edge che collegano nodi "< degree minori"
        LongWritable vertexId = vertex.getId();
        LongWritable vertexValue = vertex.getValue();

        LongWritable messageId;
        LongWritable messageValue;

        for (MessageLongIdLongValue message : messages) {

            messageValue = message.getValue();
            messageId = message.getId();

            if ((messageValue.compareTo(vertexValue) < 0)
                    || ((messageValue.compareTo(vertexValue) == 0) && (messageId.compareTo(vertexId) < 0))) {
                this.removeEdgesRequest(messageId, vertexId);
            }
        }
    }
}

From source file:org.apache.accumulo.core.data.MapFileTest.java

License:Apache License

public void testMapFileFix() {
    try {//ww  w .  j a  va2s.  co  m
        Configuration conf = CachedConfiguration.getInstance();
        FileSystem fs = FileSystem.get(conf);
        conf.setInt("io.seqfile.compress.blocksize", 4000);

        for (CompressionType compressionType : CompressionType.values()) {
            /*****************************
             * write out the test map file
             */
            MyMapFile.Writer mfw = new MyMapFile.Writer(conf, fs, "/tmp/testMapFileIndexingMap", Text.class,
                    BytesWritable.class, compressionType);
            BytesWritable value;
            Random r = new Random();
            byte[] bytes = new byte[1024];
            for (int i = 0; i < 1000; i++) {
                String keyString = Integer.toString(i + 1000000);
                Text key = new Text(keyString);
                r.nextBytes(bytes);
                value = new BytesWritable(bytes);
                mfw.append(key, value);
            }
            mfw.close();

            /************************************
             * move the index file
             */
            fs.rename(new Path("/tmp/testMapFileIndexingMap/index"),
                    new Path("/tmp/testMapFileIndexingMap/oldIndex"));

            /************************************
             * recreate the index
             */
            MyMapFile.fix(fs, new Path("/tmp/testMapFileIndexingMap"), Text.class, BytesWritable.class, false,
                    conf);

            /************************************
             * compare old and new indices
             */
            MySequenceFile.Reader oldIndexReader = new MySequenceFile.Reader(fs,
                    new Path("/tmp/testMapFileIndexingMap/oldIndex"), conf);
            MySequenceFile.Reader newIndexReader = new MySequenceFile.Reader(fs,
                    new Path("/tmp/testMapFileIndexingMap/index"), conf);

            Text oldKey = new Text();
            Text newKey = new Text();
            LongWritable oldValue = new LongWritable();
            LongWritable newValue = new LongWritable();
            while (true) {
                boolean moreKeys = false;
                // check for the same number of records
                assertTrue((moreKeys = oldIndexReader.next(oldKey, oldValue)) == newIndexReader.next(newKey,
                        newValue));
                if (!moreKeys)
                    break;
                assertTrue(oldKey.compareTo(newKey) == 0);
                assertTrue(oldValue.compareTo(newValue) == 0);
            }
            oldIndexReader.close();
            newIndexReader.close();

            fs.delete(new Path("/tmp/testMapFileIndexingMap"), true);
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}