@Override public Vertex<LongWritable, LongWritable, LongWritable> getCurrentVertex() throws IOException, InterruptedException { Vertex<LongWritable, LongWritable, LongWritable> vertex = conf.createVertex(); String tokens[] = saperator.split(getRecordReader().getCurrentValue().toString()); List<Edge<LongWritable, LongWritable>> edges = Lists.newArrayListWithCapacity(tokens.length - 1); long weight = ((long) 1.0) / (tokens.length - 1); LongWritable VertexId = new LongWritable(Long.parseLong(tokens[0])); LongWritable VertexValue = new LongWritable(Long.parseLong(tokens[1])); System.out.println(VertexValue); String edgeId[] = tokens[2].split(","); /* for(int n=2;n<tokens.length-1;n++) { edges.add(EdgeFactory.create(new LongWritable(Long.parseLong(tokens[n])),new LongWritable(weight))); } */ for (String edge : edgeId) { edges.add( EdgeFactory.create(new LongWritable(Long.parseLong(edge)), new LongWritable(weight))); } // vertex.initialize(VertexId, (LongWritable) edges); vertex.initialize(VertexId, VertexValue, edges); return vertex; }
@Override public void compute( Vertex<IntWritable, IntWritable, NullWritable> vertex, Iterable<IntWritable> messages) throws IOException { if (0 == getSuperstep()) { vertex.setValue(new IntWritable(Integer.MAX_VALUE)); } Integer minDist = isSource(vertex) ? 0 : Integer.MAX_VALUE; for (IntWritable msg : messages) { if (msg.get() < minDist) { minDist = msg.get(); } } if (minDist < vertex.getValue().get()) { vertex.setValue(new IntWritable(minDist)); for (Edge<IntWritable, NullWritable> edge : vertex.getEdges()) { final IntWritable neighbor = edge.getTargetVertexId(); final Integer distance = minDist + 1; sendMessage(neighbor, new IntWritable(distance)); } } vertex.voteToHalt(); }
@Override public boolean shouldDebugVertex( Vertex<LongWritable, IntWritable, NullWritable> vertex, long superstepNo) { return (vertex.getId().get() == 74780L) || (vertex.getId().get() == 75686) || (vertex.getId().get() == 75685); }
/** * Append some vertices to an out-of-core partition. * * @param partitionId Id of the destination partition * @param vertices Vertices to be added * @throws IOException */ private void appendVertices(Integer partitionId, Collection<Vertex<I, V, E, M>> vertices) throws IOException { File file = new File(getPartitionPath(partitionId)); DataOutputStream outputStream = new DataOutputStream(new BufferedOutputStream(new FileOutputStream(file, true))); for (Vertex<I, V, E, M> vertex : vertices) { vertex.write(outputStream); } outputStream.close(); }
/** * Write a partition to disk. * * @param partition The partition object to write * @throws java.io.IOException */ private void writePartition(Partition<I, V, E, M> partition) throws IOException { File file = new File(getPartitionPath(partition.getId())); file.getParentFile().mkdirs(); file.createNewFile(); DataOutputStream outputStream = new DataOutputStream(new BufferedOutputStream(new FileOutputStream(file))); for (Vertex<I, V, E, M> vertex : partition.getVertices()) { vertex.write(outputStream); } outputStream.close(); }
@Override public void compute( Vertex<IntWritable, IntWritable, NullWritable> vertex, Iterable<IntWritable> messages) throws IOException { int currentLength = vertex.getValue().get(); // First superstep is special, because we can simply look at the neighbors if (getSuperstep() == 0) { if (isSource(vertex)) { vertex.setValue(new IntWritable(0)); sendMessageToAllEdges(vertex, vertex.getValue()); } else { vertex.setValue(new IntWritable(Integer.MAX_VALUE)); } vertex.voteToHalt(); return; } boolean changed = false; // did we get a smaller length ? for (IntWritable message : messages) { int candidateLength = message.get() + 1; if (candidateLength < currentLength) { currentLength = candidateLength; changed = true; } } // propagate new length to the neighbors if (changed) { vertex.setValue(new IntWritable(currentLength)); sendMessageToAllEdges(vertex, vertex.getValue()); } vertex.voteToHalt(); }
/** * Read a partition from disk. * * @param partitionId Id of the partition to read * @return The partition object * @throws IOException */ private Partition<I, V, E, M> readPartition(Integer partitionId) throws IOException { Partition<I, V, E, M> partition = new Partition<I, V, E, M>(conf, partitionId); File file = new File(getPartitionPath(partitionId)); DataInputStream inputStream = new DataInputStream(new BufferedInputStream(new FileInputStream(file))); int numVertices = onDiskPartitions.get(partitionId); for (int i = 0; i < numVertices; ++i) { Vertex<I, V, E, M> vertex = conf.createVertex(); vertex.readFields(inputStream); partition.putVertex(vertex); } inputStream.close(); file.delete(); return partition; }
@Override public void compute( Vertex<LongWritable, DoubleWritable, FloatWritable> vertex, Iterable<LongWritable> messages) throws IOException { Assert.assertNotNull(vertex); vertex.voteToHalt(); }
/** * 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 java.io.IOException */ @Override public void compute( Vertex<LongWritable, LongWritable, NullWritable> vertex, Iterable<LongWritable> messages) throws IOException { long currentComponent = vertex.getValue().get(); // First superstep is special, because we can simply look at the neighbors if (getSuperstep() == 0) { for (Edge<LongWritable, NullWritable> edge : vertex.getEdges()) { long neighbor = edge.getTargetVertexId().get(); if (neighbor < currentComponent) { currentComponent = neighbor; } } // Only need to send value if it is not the own id if (currentComponent != vertex.getValue().get()) { vertex.setValue(new LongWritable(currentComponent)); for (Edge<LongWritable, NullWritable> edge : vertex.getEdges()) { LongWritable neighbor = edge.getTargetVertexId(); if (neighbor.get() > currentComponent) { sendMessage(neighbor, vertex.getValue()); } } } vertex.voteToHalt(); return; } boolean changed = false; // did we get a smaller id ? for (LongWritable message : messages) { long candidateComponent = message.get(); if (candidateComponent < currentComponent) { currentComponent = candidateComponent; changed = true; } } // propagate new component id to the neighbors if (changed) { vertex.setValue(new LongWritable(currentComponent)); sendMessageToAllEdges(vertex, vertex.getValue()); } vertex.voteToHalt(); }
@Override public void compute( Vertex<IntWritable, IntWritable, NullWritable> vertex, Iterable<IntWritable> messages) throws IOException { if (getSuperstep() == 0) { // send list of this vertex's neighbors to all neighbors for (Edge<IntWritable, NullWritable> edge : vertex.getEdges()) { sendMessageToAllEdges(vertex, edge.getTargetVertexId()); } } else { for (IntWritable message : messages) { // INTENTIONAL BUG: the original algorithm has these two lines, which // avoids the // NullPointerException, which the current code throws. // final int current = (closeMap.get(message) == null) ? // 0 : closeMap.get(message) + 1; final int current = closeMap.get(message); closeMap.put(message, current); } // make sure the result values are sorted and // packaged in an IntArrayListWritable for output Set<Pair> sortedResults = Sets.<Pair>newTreeSet(); for (Map.Entry<IntWritable, Integer> entry : closeMap.entrySet()) { sortedResults.add(new Pair(entry.getKey(), entry.getValue())); } IntArrayListWritable outputList = new IntArrayListWritable(); for (Pair pair : sortedResults) { if (pair.value > 0) { outputList.add(pair.key); } else { break; } } if (outputList.isEmpty()) { vertex.setValue(new IntWritable(-1)); } else { vertex.setValue(outputList.get(0)); } } vertex.voteToHalt(); }
@Override public Vertex<LongWritable, DoubleWritable, FloatWritable> getCurrentVertex() throws IOException, InterruptedException { Vertex<LongWritable, DoubleWritable, FloatWritable> vertex = conf.createVertex(); String[] tokens = neighborSeparator.split(getRecordReader().getCurrentValue().toString()); List<Edge<LongWritable, FloatWritable>> edges = Lists.newArrayListWithCapacity(tokens.length - 1); for (int n = 1; n < tokens.length; n++) { String[] parts = weightSeparator.split(tokens[n]); edges.add( EdgeFactory.create( new LongWritable(Long.parseLong(parts[0])), new FloatWritable(Float.parseFloat(parts[1])))); } LongWritable vertexId = new LongWritable(Long.parseLong(tokens[0])); vertex.initialize(vertexId, new DoubleWritable(), edges); return vertex; }
@Override public Vertex<LongWritable, VertexDataStructure, FloatWritable> getCurrentVertex() throws IOException, InterruptedException { Text line = getRecordReader().getCurrentValue(); Vertex<LongWritable, VertexDataStructure, FloatWritable> vertex = getConf().createVertex(); try { JSONArray jsonVertex = new JSONArray(line.toString()); vertex.initialize( new LongWritable(jsonVertex.getLong(0)), new VertexDataStructure(jsonVertex.getLong(1), new HashSet<Long>())); JSONArray jsonEdgeArray = jsonVertex.getJSONArray(2); for (int i = 0; i < jsonEdgeArray.length(); ++i) { JSONArray jsonEdge = jsonEdgeArray.getJSONArray(i); Edge<LongWritable, FloatWritable> edge = EdgeFactory.create( new LongWritable(jsonEdge.getLong(0)), new FloatWritable((float) jsonEdge.getDouble(1))); vertex.addEdge(edge); } } catch (JSONException e) { throw new IllegalArgumentException("next: Couldn't get vertex from line " + line, e); } return vertex; }
public IncidentAndAdjacentIterator(Vertex<IntWritable, DoubleWritable, Directions> vertex) { this(vertex.getEdges().iterator()); }
/** * Is this vertex the source id? * * @param vertex Vertex * @return True if the source id */ private boolean isSource(Vertex<IntWritable, ?, ?> vertex) { return vertex.getId().get() == SOURCE_ID.get(getConf()); }
// public static double level=0; @Override public void compute( Vertex<LongWritable, DoubleWritable, FloatWritable> v, Iterable<DoubleWritable> msg) throws IOException { if (getSuperstep() == 0) { if (isSource(v) == true) { v.setValue(new DoubleWritable(0)); // level++; sendMessageToAllEdges(v, new DoubleWritable(v.getId().get())); } else v.setValue(new DoubleWritable(-1)); } else { for (DoubleWritable messages : msg) { if (v.getValue().get() == -1) { v.setValue(messages); for (Edge<LongWritable, FloatWritable> edge : v.getEdges()) { if (edge.getTargetVertexId().get() != v.getValue().get()) { sendMessage(edge.getTargetVertexId(), new DoubleWritable(v.getId().get())); } } } else { if (v.getValue().get() != messages.get()) { removeEdgesRequest(v.getId(), new LongWritable((long) messages.get())); removeEdgesRequest(new LongWritable((long) messages.get()), v.getId()); } } /* if(v.getValue().get() > messages.get()) { v.setValue(new DoubleWritable(getSuperstep())); // changed=true; }*/ } if (v.getValue().get() == -1) { for (Edge<LongWritable, FloatWritable> edge : v.getEdges()) { if (edge.getTargetVertexId().get() != v.getValue().get()) sendMessage(edge.getTargetVertexId(), new DoubleWritable(v.getId().get())); } } /* if(changed) { level++; sendMessageToAllEdges(v,new DoubleWritable(level)); changed=false; }*/ } v.voteToHalt(); }
private boolean isSource(Vertex<LongWritable, DoubleWritable, FloatWritable> v) { return v.getId().get() == SOURCE_ID.get(getConf()); }