Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save saltzm/ab7172c57dec927061be to your computer and use it in GitHub Desktop.
Save saltzm/ab7172c57dec927061be to your computer and use it in GitHub Desktop.
Sample input format to go with SampleVertexData
import org.apache.giraph.io.formats.*;
import org.apache.giraph.edge.Edge;
import org.apache.giraph.edge.EdgeFactory;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.NullWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.InputSplit;
import org.apache.hadoop.mapreduce.TaskAttemptContext;
import com.google.common.collect.Lists;
import java.io.IOException;
import java.util.List;
import java.util.regex.Pattern;
/**
* Simple text-based {@link org.apache.giraph.io.VertexInputFormat} for
* unweighted graphs with long ids.
*
* Each line consists of: vertex neighbor1 neighbor2 ...
*/
public class LongSampleVertexDataNullTextInputFormat extends
TextVertexInputFormat<LongWritable, SampleVertexData, NullWritable> {
/** Separator of the vertex and neighbors */
private static final Pattern SEPARATOR = Pattern.compile("[\t ]");
@Override
public TextVertexReader createVertexReader(InputSplit split,
TaskAttemptContext context)
throws IOException {
return new LongSampleVertexDataNullVertexReader();
}
/**
* Vertex reader associated with LongSampleVertexDataNullTextInputFormat}.
*/
public class LongSampleVertexDataNullVertexReader extends
TextVertexReaderFromEachLineProcessed<String[]> {
/** Cached vertex id for the current line */
private LongWritable id;
@Override
protected String[] preprocessLine(Text line) throws IOException {
String[] tokens = SEPARATOR.split(line.toString());
id = new LongWritable(Long.parseLong(tokens[0]));
return tokens;
}
@Override
protected LongWritable getId(String[] tokens) throws IOException {
return id;
}
@Override
protected SampleVertexData getValue(String[] tokens) throws IOException {
return new SampleVertexData();
}
@Override
protected Iterable<Edge<LongWritable, NullWritable>> getEdges(
String[] tokens) throws IOException {
List<Edge<LongWritable, NullWritable>> edges =
Lists.newArrayListWithCapacity(tokens.length - 1);
for (int n = 1; n < tokens.length; n++) {
edges.add(EdgeFactory.create(
new LongWritable(Long.parseLong(tokens[n]))));
}
return edges;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment