Hi folks..
To give the vertex value as (double,double), I created my own class
MyVertexValueWritable.java and to give vertex input format,i also created
MyVertexInputFormat.java...Program is compiling successfully...
But while running, it is showing some strange behaviour....
All three classes are listed below...
1.MyVertexValueWritable.java
2.MyVertexInputFormat.java
3.Sample_program.java
*Below is the class MyVertexValueWritable.java* and its path where it is
stored is in *org.apache.giraph.examples.utils package in giraph-examples*
package org.apache.giraph.examples.utils;
import java.io.*;
import org.apache.hadoop.io.Writable;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.WritableComparator;
public class MyVertexValueWritable implements Writable {
private double vertex_value;
private double vertex_previous_value;
public MyVertexValueWritable() {}
public MyVertexValueWritable(double vertex_value,double
vertex_previous_value) { set(vertex_value,vertex_previous_value); }
public void set(double vertex_value,double vertex_previous_value) {
this.vertex_value = vertex_value;
this.vertex_previous_value=vertex_previous_value;}
public double get_vertex_value() { return vertex_value; }
public double get_vertex_previous_value() { return vertex_previous_value;
}
public void readFields(DataInput in) throws IOException {
vertex_value = in.readDouble();
vertex_previous_value=in.readDouble();
}
public void write(DataOutput out) throws IOException {
out.writeDouble(vertex_value);
out.writeDouble(vertex_previous_value);
}
public String toString() {
return Double.toString(vertex_value) + ", "
+ Double.toString(vertex_previous_value);
}
}
......................***********************************..........................................
Next Class is * MyVertexInputFormat.java *
its path is in *org.apache.giraph.examples.io.formats package in
giraph-examples*
import org.apache.giraph.edge.Edge;
import org.apache.giraph.edge.EdgeFactory;
import org.apache.giraph.examples.utils.BrachaTouegDeadlockVertexValue;
import org.apache.giraph.graph.Vertex;
import org.apache.giraph.io.formats.TextVertexInputFormat;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.FloatWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.InputSplit;
import org.apache.hadoop.mapreduce.TaskAttemptContext;
import org.json.JSONArray;
import org.json.JSONException;
import org.apache.giraph.examples.utils.MyVertexValueWritable;
import com.google.common.collect.Lists;
public class MyVertexInputFormat extends
TextVertexInputFormat<LongWritable, MyVertexValueWritable,
FloatWritable> {
@Override
public TextVertexReader createVertexReader(InputSplit split,
TaskAttemptContext context) {
return new JsonLongMyVertexValueFloatDoubleVertexReader();
}
/**
input is given in this format...
* [1,0,0.0,[[2,0], [3,1], [4,1]]]
*/
class JsonLongMyVertexValueFloatDoubleVertexReader
extends
TextVertexReaderFromEachLineProcessedHandlingExceptions<JSONArray,
JSONException> {
@Override
protected JSONArray preprocessLine(Text line) throws JSONException {
return new JSONArray(line.toString());
}
@Override
protected LongWritable getId(JSONArray jsonVertex) throws JSONException,
IOException {
return new LongWritable(jsonVertex.getLong(0));
}
@Override
protected MyVertexValueWritable getValue(JSONArray jsonVertex)
throws JSONException, IOException {
return new
MyVertexValueWritable(jsonVertex.getDouble(1),jsonVertex.getDouble(2));
}
@Override
protected Iterable<Edge<LongWritable, FloatWritable>> getEdges(
JSONArray jsonVertex) throws JSONException, IOException {
JSONArray jsonEdgeArray = jsonVertex.getJSONArray(3);
List<Edge<LongWritable, FloatWritable>> edges =
Lists.newArrayListWithCapacity(jsonEdgeArray.length());
for (int i = 0; i < jsonEdgeArray.length(); ++i) {
JSONArray jsonEdge = jsonEdgeArray.getJSONArray(i);
edges.add(EdgeFactory.create(new LongWritable(jsonEdge.getLong(0)),
new FloatWritable((float) jsonEdge.getDouble(1))));
}
return edges;
}
@Override
protected Vertex<LongWritable, MyVertexValueWritable,
FloatWritable> handleException(Text line, JSONArray jsonVertex,
JSONException e) {
throw new IllegalArgumentException(
"Couldn't get vertex from line " + line, e);
}
}
}
*And the main program which I am implementing is Sample_program.java*
...........................................************************..........................................
package org.apache.giraph.examples;
import org.apache.giraph.examples.utils.MyVertexValueWritable;
import org.apache.giraph.graph.BasicComputation;
import org.apache.giraph.conf.LongConfOption;
import org.apache.giraph.edge.Edge;
import org.apache.giraph.graph.Vertex;
import org.apache.hadoop.io.DoubleWritable;
import org.apache.hadoop.io.FloatWritable;
import org.apache.hadoop.io.LongWritable;
import org.apache.log4j.Logger;
import java.io.IOException;
public class Sample_program extends BasicComputation<LongWritable,
MyVertexValueWritable, FloatWritable, DoubleWritable>
{
@Override
public void compute(Vertex<LongWritable, MyVertexValueWritable,
FloatWritable> vertex,Iterable<DoubleWritable> messages) throws IOException
{
if(getSuperstep()==0)
{
vertex.setValue(new MyVertexValueWritable(1.0,0.0));
vertex.voteToHalt();
}
}
}
I gave the following command to run
hadoop jar $GIRAPH_HOME/giraph-examples/
target/giraph-examples-1.1.0-SNAPSHOT-for-hadoop-0.20.203.0-jar-with-dependencies.jar
org.apache.giraph.GiraphRunner org.apache.giraph.examples.Sample_program
-vif org.apache.giraph.examples.io.formats.MyVertexInputFormat -vip
/user/hduser/sp_input/linerank_input1.txt -vof
org.apache.giraph.io.formats.IdWithValueTextOutputFormat -op
/user/hduser/sp_output70/linerank -w 1
Any help is really appreciated...
Thanks
Jyoti