incubator-giraph-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From jgho...@apache.org
Subject svn commit: r1190137 - in /incubator/giraph/trunk/src: main/java/org/apache/giraph/lib/AdjacencyListTextVertexOutputFormat.java test/java/org/apache/giraph/lib/TestAdjacencyListTextVertexOutputFormat.java
Date Fri, 28 Oct 2011 02:19:04 GMT
Author: jghoman
Date: Fri Oct 28 02:19:04 2011
New Revision: 1190137

URL: http://svn.apache.org/viewvc?rev=1190137&view=rev
Log:
GIRAPH-56, the sequel. The one where I remember to add the files.

Added:
    incubator/giraph/trunk/src/main/java/org/apache/giraph/lib/AdjacencyListTextVertexOutputFormat.java
    incubator/giraph/trunk/src/test/java/org/apache/giraph/lib/TestAdjacencyListTextVertexOutputFormat.java

Added: incubator/giraph/trunk/src/main/java/org/apache/giraph/lib/AdjacencyListTextVertexOutputFormat.java
URL: http://svn.apache.org/viewvc/incubator/giraph/trunk/src/main/java/org/apache/giraph/lib/AdjacencyListTextVertexOutputFormat.java?rev=1190137&view=auto
==============================================================================
--- incubator/giraph/trunk/src/main/java/org/apache/giraph/lib/AdjacencyListTextVertexOutputFormat.java
(added)
+++ incubator/giraph/trunk/src/main/java/org/apache/giraph/lib/AdjacencyListTextVertexOutputFormat.java
Fri Oct 28 02:19:04 2011
@@ -0,0 +1,82 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.giraph.lib;
+
+import org.apache.giraph.graph.BasicVertex;
+import org.apache.giraph.graph.VertexWriter;
+import org.apache.hadoop.io.Text;
+import org.apache.hadoop.io.Writable;
+import org.apache.hadoop.io.WritableComparable;
+import org.apache.hadoop.mapreduce.RecordWriter;
+import org.apache.hadoop.mapreduce.TaskAttemptContext;
+
+import java.io.IOException;
+
+/**
+ * OutputFormat to write out the graph nodes as text, value-separated (by
+ * tabs, by default).  With the default delimiter, a vertex is written out as:
+ *
+ * <VertexId><tab><Vertex Value><tab>[<EdgeId><tab><EdgeValue>]+
+ *
+ * @param <I> Vertex index value
+ * @param <V> Vertex value
+ * @param <E> Edge value
+ */
+public class AdjacencyListTextVertexOutputFormat <I extends WritableComparable,
+    V extends Writable, E extends Writable> extends TextVertexOutputFormat<I, V, E>{
+
+  static class AdjacencyListVertexWriter<I extends WritableComparable, V extends
+      Writable, E extends Writable> extends TextVertexWriter<I, V, E> {
+    public static final String LINE_TOKENIZE_VALUE = "adj.list.output.delimiter";
+    public static final String LINE_TOKENIZE_VALUE_DEFAULT = "\t";
+
+    private String delimiter;
+
+    public AdjacencyListVertexWriter(RecordWriter<Text,Text> recordWriter) {
+      super(recordWriter);
+    }
+
+    @Override
+    public void writeVertex(BasicVertex<I, V, E, ?> vertex) throws IOException,
+        InterruptedException {
+      if(delimiter == null) {
+        delimiter = getContext().getConfiguration()
+           .get(LINE_TOKENIZE_VALUE, LINE_TOKENIZE_VALUE_DEFAULT);
+      }
+
+      StringBuffer sb = new StringBuffer(vertex.getVertexId().toString());
+      sb.append(delimiter);
+      sb.append(vertex.getVertexValue().toString());
+
+      for (I edge : vertex) {
+        sb.append(delimiter).append(edge);
+        sb.append(delimiter).append(vertex.getEdgeValue(edge));
+      }
+
+      getRecordWriter().write(new Text(sb.toString()), null);
+    }
+  }
+
+  @Override
+  public VertexWriter<I, V, E> createVertexWriter(TaskAttemptContext context)
+      throws IOException, InterruptedException {
+    return new AdjacencyListVertexWriter<I, V, E>
+        (textOutputFormat.getRecordWriter(context));
+  }
+
+}

Added: incubator/giraph/trunk/src/test/java/org/apache/giraph/lib/TestAdjacencyListTextVertexOutputFormat.java
URL: http://svn.apache.org/viewvc/incubator/giraph/trunk/src/test/java/org/apache/giraph/lib/TestAdjacencyListTextVertexOutputFormat.java?rev=1190137&view=auto
==============================================================================
--- incubator/giraph/trunk/src/test/java/org/apache/giraph/lib/TestAdjacencyListTextVertexOutputFormat.java
(added)
+++ incubator/giraph/trunk/src/test/java/org/apache/giraph/lib/TestAdjacencyListTextVertexOutputFormat.java
Fri Oct 28 02:19:04 2011
@@ -0,0 +1,125 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.giraph.lib;
+
+import junit.framework.TestCase;
+import org.apache.giraph.graph.BasicVertex;
+import org.apache.hadoop.conf.Configuration;
+import org.apache.hadoop.io.DoubleWritable;
+import org.apache.hadoop.io.Text;
+import org.apache.hadoop.io.WritableComparable;
+import org.apache.hadoop.mapreduce.RecordWriter;
+import org.apache.hadoop.mapreduce.TaskAttemptContext;
+import org.mockito.Matchers;
+
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.Iterator;
+
+import static org.apache.giraph.lib.AdjacencyListTextVertexOutputFormat.AdjacencyListVertexWriter;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.never;
+import static org.mockito.Mockito.times;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.when;
+
+public class TestAdjacencyListTextVertexOutputFormat extends TestCase {
+  public void testVertexWithNoEdges() throws IOException, InterruptedException {
+    Configuration conf = new Configuration();
+    TaskAttemptContext tac = mock(TaskAttemptContext.class);
+    when(tac.getConfiguration()).thenReturn(conf);
+
+    BasicVertex vertex = mock(BasicVertex.class);
+    when(vertex.getVertexId()).thenReturn(new Text("The Beautiful South"));
+    when(vertex.getVertexValue()).thenReturn(new DoubleWritable(32.2d));
+    // Create empty iterator == no edges
+    when(vertex.iterator()).thenReturn(new ArrayList<Text>().iterator());
+
+    RecordWriter<Text,Text> tw = mock(RecordWriter.class);
+    AdjacencyListVertexWriter writer = new AdjacencyListVertexWriter(tw);
+    writer.initialize(tac);
+    writer.writeVertex(vertex);
+
+    Text expected = new Text("The Beautiful South\t32.2");
+    verify(tw).write(expected, null);
+    verify(vertex, times(1)).iterator();
+    verify(vertex, times(0)).getEdgeValue(Matchers.<WritableComparable>any());
+  }
+
+  public void testVertexWithEdges() throws IOException, InterruptedException {
+    Configuration conf = new Configuration();
+    TaskAttemptContext tac = mock(TaskAttemptContext.class);
+    when(tac.getConfiguration()).thenReturn(conf);
+
+    BasicVertex vertex = mock(BasicVertex.class);
+    when(vertex.getVertexId()).thenReturn(new Text("San Francisco"));
+    when(vertex.getVertexValue()).thenReturn(new DoubleWritable(0d));
+    when(vertex.getNumEdges()).thenReturn(2l);
+    ArrayList<Text> cities = new ArrayList<Text>();
+    Collections.addAll(cities, new Text("Los Angeles"), new Text("Phoenix"));
+
+    when(vertex.iterator()).thenReturn(cities.iterator());
+    mockEdgeValue(vertex, "Los Angeles", 347.16);
+    mockEdgeValue(vertex, "Phoenix", 652.48);
+
+    RecordWriter<Text,Text> tw = mock(RecordWriter.class);
+    AdjacencyListVertexWriter writer = new AdjacencyListVertexWriter(tw);
+    writer.initialize(tac);
+    writer.writeVertex(vertex);
+
+    Text expected = new Text("San Francisco\t0.0\tLos Angeles\t347.16\t" +
+            "Phoenix\t652.48");
+    verify(tw).write(expected, null);
+    verify(vertex, times(1)).iterator();
+    verify(vertex, times(2)).getEdgeValue(Matchers.<WritableComparable>any());
+  }
+
+  public void testWithDifferentDelimiter() throws IOException, InterruptedException {
+    Configuration conf = new Configuration();
+    conf.set(AdjacencyListVertexWriter.LINE_TOKENIZE_VALUE, ":::");
+    TaskAttemptContext tac = mock(TaskAttemptContext.class);
+    when(tac.getConfiguration()).thenReturn(conf);
+
+    BasicVertex vertex = mock(BasicVertex.class);
+    when(vertex.getVertexId()).thenReturn(new Text("San Francisco"));
+    when(vertex.getVertexValue()).thenReturn(new DoubleWritable(0d));
+    when(vertex.getNumEdges()).thenReturn(2l);
+    ArrayList<Text> cities = new ArrayList<Text>();
+    Collections.addAll(cities, new Text("Los Angeles"), new Text("Phoenix"));
+
+    when(vertex.iterator()).thenReturn(cities.iterator());
+    mockEdgeValue(vertex, "Los Angeles", 347.16);
+    mockEdgeValue(vertex, "Phoenix", 652.48);
+
+    RecordWriter<Text,Text> tw = mock(RecordWriter.class);
+    AdjacencyListVertexWriter writer = new AdjacencyListVertexWriter(tw);
+    writer.initialize(tac);
+    writer.writeVertex(vertex);
+
+    Text expected = new Text("San Francisco:::0.0:::Los Angeles:::347.16:::" +
+            "Phoenix:::652.48");
+    verify(tw).write(expected, null);
+    verify(vertex, times(1)).iterator();
+    verify(vertex, times(2)).getEdgeValue(Matchers.<WritableComparable>any());
+  }
+
+  private void mockEdgeValue(BasicVertex vertex, String s, double d) {
+    when(vertex.getEdgeValue(new Text(s))).thenReturn(new DoubleWritable(d));
+  }
+}



Mime
View raw message