List of usage examples for org.apache.hadoop.io LongWritable get
public long get()
From source file:org.apache.giraph.examples.scc.SccAsyncComputation.java
License:Apache License
/** * Creates list of parents based on the received ids and halts the vertices * that don't have any parent or outgoing edge, hence, they can't be * part of an SCC.//from ww w . ja v a2 s . c o m * @param vertex Current vertex. * @param messages Received ids from the Transpose phase. */ private void trim(Vertex<LongWritable, SccVertexValue, NullWritable> vertex, Iterable<LongWritable> messages) { SccVertexValue vertexValue = vertex.getValue(); // Keep the ids of the parent nodes to allow for backwards traversal for (LongWritable parent : messages) { vertexValue.addParent(parent.get()); } // If this node doesn't have any parents or outgoing edges, // it can't be part of an SCC vertexValue.set(vertex.getId().get()); if (vertex.getNumEdges() == 0 || vertexValue.getParents() == null) { vertexValue.deactivate(); } else { messageValue.set(vertexValue.get()); sendMessageToAllEdges(vertex, messageValue); } }
From source file:org.apache.giraph.examples.scc.SccAsyncComputation.java
License:Apache License
/** * Traverse the transposed graph and keep the maximum vertex value. * @param vertex Current vertex.// ww w.j av a2 s . co m * @param messages Received values from children vertices. */ private void backwardTraversalRest(Vertex<LongWritable, SccVertexValue, NullWritable> vertex, Iterable<LongWritable> messages) { SccVertexValue vertexValue = vertex.getValue(); for (LongWritable m : messages) { if (vertexValue.get() == m.get()) { sendMessageToAllParents(vertex, m); aggregate(CONVERGED, new BooleanWritable(true)); vertexValue.deactivate(); vertex.voteToHalt(); break; } } }
From source file:org.apache.giraph.examples.scc.SccAsyncComputation.java
License:Apache License
/** * Compares the messages values with the current vertex value and finds * the maximum./*from w w w . jav a2 s .c o m*/ * If the maximum value is different from the vertex value, makes it the * new vertex value and returns true, otherwise, returns false. * @param vertexValue Current vertex value. * @param messages Messages containing neighbors' vertex values. * @return True if a new maximum was found, otherwise, returns false. */ private boolean setMaxValue(SccVertexValue vertexValue, Iterable<LongWritable> messages) { boolean changed = false; for (LongWritable m : messages) { if (vertexValue.get() < m.get()) { vertexValue.set(m.get()); changed = true; } } return changed; }
From source file:org.apache.giraph.examples.SubgraphTriangleCount.java
License:Apache License
@Override public void compute(Iterable<SubgraphMessage<LongWritable, BytesWritable>> subgraphMessages) throws IOException { // Convert adjacency list to adjacency set Subgraph<LongWritable, LongWritable, LongWritable, NullWritable, TriangleCountSubgraphValue, LongWritable> subgraph = getSubgraph(); if (getSuperstep() == 0) { TriangleCountSubgraphValue triangleCountSubgraphValue = new TriangleCountSubgraphValue(); triangleCountSubgraphValue.adjSet = new HashMap<Long, Set<Long>>(); subgraph.getSubgraphVertices().setSubgraphValue(triangleCountSubgraphValue); for (SubgraphVertex<LongWritable, LongWritable, LongWritable, NullWritable, LongWritable> vertex : subgraph .getSubgraphVertices().getLocalVertices().values()) { Set<Long> adjVertices = new HashSet<Long>(); for (SubgraphEdge<LongWritable, NullWritable, LongWritable> edge : vertex.getOutEdges()) { adjVertices.add(edge.getSinkVertexId().get()); }/*from www. j a v a 2s . com*/ triangleCountSubgraphValue.adjSet.put(vertex.getId().get(), adjVertices); } return; } else if (getSuperstep() == 1) { TriangleCountSubgraphValue triangleCountSubgraphValue = subgraph.getSubgraphVertices() .getSubgraphValue(); long triangleCount = triangleCountSubgraphValue.triangleCount; Map<SubgraphId<LongWritable>, ExtendedByteArrayDataOutput> msg = new HashMap<>(); for (SubgraphVertex<LongWritable, LongWritable, LongWritable, NullWritable, LongWritable> vertex : subgraph .getSubgraphVertices().getLocalVertices().values()) { for (SubgraphEdge<LongWritable, NullWritable, LongWritable> edge : vertex.getOutEdges()) { SubgraphVertex<LongWritable, LongWritable, LongWritable, NullWritable, LongWritable> adjVertex = subgraph .getSubgraphVertices().getVertexById(edge.getSinkVertexId()); // Preparing messages to be sent to remote adjacent vertices. if (adjVertex.isRemote() && adjVertex.getId().get() > vertex.getId().get()) { SubgraphId<LongWritable> remoteSubgraphId = ((RemoteSubgraphVertex<LongWritable, LongWritable, LongWritable, NullWritable, LongWritable>) adjVertex) .getSubgraphId(); ExtendedByteArrayDataOutput vertexIds = msg.get(remoteSubgraphId); if (vertexIds == null) { vertexIds = new ExtendedByteArrayDataOutput(); msg.put(remoteSubgraphId, vertexIds); } vertexIds.writeLong(adjVertex.getId().get()); vertexIds.writeLong(vertex.getId().get()); vertexIds.writeLong(vertex.getId().get()); } else if (adjVertex.isRemote() || vertex.getId().get() > adjVertex.getId().get()) continue; if (adjVertex.isRemote()) { continue; //as it has no outedges } // Counting triangles which have at least two vertices in the same // subgraph. for (SubgraphEdge<LongWritable, NullWritable, LongWritable> edgeAdjVertex : adjVertex .getOutEdges()) { SubgraphVertex<LongWritable, LongWritable, LongWritable, NullWritable, LongWritable> adjAdjVertex = subgraph .getSubgraphVertices().getVertexById(edgeAdjVertex.getSinkVertexId()); if (adjAdjVertex.isRemote() || adjAdjVertex.getId().get() > adjVertex.getId().get()) { if (triangleCountSubgraphValue.adjSet.get(vertex.getId().get()) .contains(adjAdjVertex.getId().get())) { triangleCount++; //trianglesList.append(vertex.getVertexID().get() + " " + adjVertex.getVertexID().get() // + " " + adjAdjVertex.getVertexID().get() + "\n"); } } } } } triangleCountSubgraphValue.triangleCount = triangleCount; sendPackedMessages(msg); } else if (getSuperstep() == 2) { Map<Long, List<Pair<Long, Long>>> ids = new HashMap<Long, List<Pair<Long, Long>>>(); unpackMessages(subgraphMessages, ids); Map<SubgraphId<LongWritable>, ExtendedByteArrayDataOutput> msg = new HashMap<>(); for (Map.Entry<Long, List<Pair<Long, Long>>> entry : ids.entrySet()) { SubgraphVertex<LongWritable, LongWritable, LongWritable, NullWritable, LongWritable> vertex = subgraph .getSubgraphVertices().getVertexById(new LongWritable(entry.getKey())); List<Pair<Long, Long>> idPairs = entry.getValue(); for (SubgraphEdge<LongWritable, NullWritable, LongWritable> edge : vertex.getOutEdges()) { SubgraphVertex<LongWritable, LongWritable, LongWritable, NullWritable, LongWritable> adjVertex = subgraph .getSubgraphVertices().getVertexById(edge.getSinkVertexId()); if (adjVertex.isRemote() && adjVertex.getId().get() > vertex.getId().get()) { SubgraphId<LongWritable> remoteSubgraphId = ((RemoteSubgraphVertex<LongWritable, LongWritable, LongWritable, NullWritable, LongWritable>) adjVertex) .getSubgraphId(); ExtendedByteArrayDataOutput vertexIds = msg.get(remoteSubgraphId); if (vertexIds == null) { vertexIds = new ExtendedByteArrayDataOutput(); msg.put(remoteSubgraphId, vertexIds); } for (Pair<Long, Long> id : idPairs) { LongWritable firstId = new LongWritable(id.first); RemoteSubgraphVertex<LongWritable, LongWritable, LongWritable, NullWritable, LongWritable> sinkSubgraphID = (RemoteSubgraphVertex<LongWritable, LongWritable, LongWritable, NullWritable, LongWritable>) subgraph .getSubgraphVertices().getVertexById(firstId); if (sinkSubgraphID.getSubgraphId() != remoteSubgraphId) { vertexIds.writeLong(adjVertex.getId().get()); vertexIds.writeLong(firstId.get()); vertexIds.writeLong(vertex.getId().get()); } } } } } sendPackedMessages(msg); } else { long triangleCount = subgraph.getSubgraphVertices().getSubgraphValue().triangleCount; Map<Long, List<Pair<Long, Long>>> ids = new HashMap<Long, List<Pair<Long, Long>>>(); unpackMessages(subgraphMessages, ids); for (Map.Entry<Long, List<Pair<Long, Long>>> entry : ids.entrySet()) { SubgraphVertex<LongWritable, LongWritable, LongWritable, NullWritable, LongWritable> vertex = subgraph .getSubgraphVertices().getLocalVertices().get(new LongWritable(entry.getKey())); for (Pair<Long, Long> p : entry.getValue()) { for (SubgraphEdge<LongWritable, NullWritable, LongWritable> edge : vertex.getOutEdges()) { if (edge.getSinkVertexId().get() == p.first) { triangleCount++; } } } } subgraph.getSubgraphVertices().getSubgraphValue().triangleCount = triangleCount; } voteToHalt(); }
From source file:org.apache.giraph.examples.TrianglesVertex.java
License:Apache License
@Override public void compute(Iterator<LongArrayWritable> msgIterator) { if (getSuperstep() == 0) { // Each vertex this is connected to List<LongWritable> verticesl = new ArrayList<LongWritable>(); // This is so we know which vertex the messages came from verticesl.add(getVertexId());/*from ww w .j a va2s .c om*/ // Find all connected vertices with ID less than current vertex for (LongWritable targetVertexId : this) { if (targetVertexId.get() < getVertexId().get()) { verticesl.add(targetVertexId); } } // Need to send list to other vertices, must convert to arraywritable LongWritable[] verticesa = verticesl.toArray(new LongWritable[0]); LongArrayWritable vertices = new LongArrayWritable(verticesa); // Sends list of smaller ID vertices to bigger ID vertices for (LongWritable targetVertexId : this) { if (targetVertexId.get() > getVertexId().get()) { sendMsg(targetVertexId, vertices); } } } else if (getSuperstep() == 1) { while (msgIterator.hasNext()) { LongArrayWritable law = msgIterator.next(); Writable[] vertices = law.get(); LongWritable source = (LongWritable) vertices[0]; for (int i = 1; i < vertices.length; i++) { if (hasEdge((LongWritable) vertices[i])) { double num = getVertexValue().get(); setVertexValue(new DoubleWritable(1.0 + num)); LongWritable[] one = new LongWritable[] { new LongWritable(1) }; LongArrayWritable inc = new LongArrayWritable(one); sendMsg(source, inc); sendMsg(((LongWritable) vertices[i]), inc); triangles.add(source.toString()); triangles.add(vertices[i].toString()); } } } try { for (LongWritable edge : this) { removeEdgeRequest(getVertexId(), edge); } } catch (IOException e) { e.printStackTrace(); } } else if (getSuperstep() == 2) { try { /*for (LongWritable edge : this) { removeEdgeRequest(getVertexId(), edge); }*/ for (int i = 0; i < triangles.size(); i += 2) { LongWritable one = new LongWritable(Long.parseLong(triangles.get(i))); LongWritable two = new LongWritable(Long.parseLong(triangles.get(i + 1))); addEdgeRequest(getVertexId(), new Edge(one, new FloatWritable(1))); addEdgeRequest(one, new Edge(getVertexId(), new FloatWritable(1))); addEdgeRequest(getVertexId(), new Edge(two, new FloatWritable(1))); addEdgeRequest(two, new Edge(getVertexId(), new FloatWritable(1))); addEdgeRequest(one, new Edge(two, new FloatWritable(1))); addEdgeRequest(two, new Edge(one, new FloatWritable(1))); } } catch (IOException e) { e.printStackTrace(); } LongWritable a = new LongWritable(1); LongWritable[] b = new LongWritable[1]; b[0] = a; LongArrayWritable c = new LongArrayWritable(b); sendMsg(getVertexId(), c); } else { voteToHalt(); } }
From source file:org.apache.giraph.examples.utils.BrachaTouegDeadlockVertexValue.java
License:Apache License
/** * remove the expected request from the edge on which the message arrived * * @param tag tag of the edge// w ww . j ava 2s . c o m * @param targetId target Id to which the edge points */ public void removeRequest(LongWritable tag, LongWritable targetId) { Long l = Long.valueOf(tag.get()); ArrayList<Long> targets = this.requests.get(l); if (targets.contains(targetId.get())) { targets.remove(Long.valueOf(targetId.get())); } }
From source file:org.apache.giraph.examples.utils.BrachaTouegDeadlockVertexValue.java
License:Apache License
/** * This function retrieves the number of pending requests for the specified * tag. Because of the N-out-of-M semantic, each time a GRANT is received * on an edge, the number of requests is reduced for the tag which the edge * is part of./*from w ww. j a v a2 s . c o m*/ * * @param tag tag related to the requests to be verified * @return number of requests pending for the tag provided */ public int getNumOfRequests(LongWritable tag) { Long l = Long.valueOf(tag.get()); ArrayList<Long> targets = this.requests.get(l); return targets.size(); }
From source file:org.apache.giraph.graph.LongDoubleFloatDoubleVertex.java
License:Apache License
@Override public void initialize(LongWritable vertexIdW, DoubleWritable vertexValueW, Map<LongWritable, FloatWritable> edgesW, Iterable<DoubleWritable> messagesW) { if (vertexIdW != null) { vertexId = vertexIdW.get(); }//from w ww . j a va 2s .c o m if (vertexValueW != null) { vertexValue = vertexValueW.get(); } if (edgesW != null) { for (Map.Entry<LongWritable, FloatWritable> entry : edgesW.entrySet()) { verticesWithEdgeValues.put(entry.getKey().get(), entry.getValue().get()); } } if (messagesW != null) { for (DoubleWritable m : messagesW) { messageList.add(m.get()); } } }
From source file:org.apache.giraph.graph.LongDoubleFloatDoubleVertex.java
License:Apache License
@Override public final boolean addEdge(LongWritable targetId, FloatWritable edgeValue) { if (verticesWithEdgeValues.put(targetId.get(), edgeValue.get())) { if (LOG.isDebugEnabled()) { LOG.debug("addEdge: Vertex=" + vertexId + ": already added an edge value for dest vertex id " + targetId.get());//from ww w .ja v a 2 s . c o m } return false; } else { return true; } }
From source file:org.apache.giraph.graph.LongDoubleFloatDoubleVertex.java
License:Apache License
@Override public FloatWritable removeEdge(LongWritable targetVertexId) { long target = targetVertexId.get(); if (verticesWithEdgeValues.containsKey(target)) { float value = verticesWithEdgeValues.get(target); verticesWithEdgeValues.removeKey(target); return new FloatWritable(value); } else {/*from ww w. j a v a 2s. c o m*/ return null; } }