
com.tinkerpop.gremlin.giraph.process.computer.GiraphMap Maven / Gradle / Ivy
package com.tinkerpop.gremlin.giraph.process.computer;
import com.tinkerpop.gremlin.giraph.Constants;
import com.tinkerpop.gremlin.giraph.process.computer.util.ConfUtil;
import com.tinkerpop.gremlin.giraph.process.computer.util.KryoWritable;
import com.tinkerpop.gremlin.giraph.structure.util.GiraphInternalVertex;
import com.tinkerpop.gremlin.process.computer.MapReduce;
import org.apache.hadoop.io.NullWritable;
import org.apache.hadoop.mapreduce.Mapper;
import java.io.IOException;
import java.lang.reflect.Constructor;
/**
* @author Marko A. Rodriguez (http://markorodriguez.com)
*/
public class GiraphMap extends Mapper {
private MapReduce mapReduce;
public GiraphMap() {
}
@Override
public void setup(final Mapper.Context context) {
try {
final Class extends MapReduce> mapReduceClass = context.getConfiguration().getClass(Constants.MAP_REDUCE_CLASS, MapReduce.class, MapReduce.class);
final Constructor extends MapReduce> constructor = mapReduceClass.getDeclaredConstructor();
constructor.setAccessible(true);
this.mapReduce = constructor.newInstance();
this.mapReduce.loadState(ConfUtil.makeApacheConfiguration(context.getConfiguration()));
} catch (Exception e) {
throw new IllegalStateException(e.getMessage(), e);
}
}
@Override
public void map(final NullWritable key, final GiraphInternalVertex value, final Mapper.Context context) throws IOException, InterruptedException {
this.mapReduce.map(value.getTinkerVertex(), new GiraphMapEmitter<>(context));
}
public static class GiraphMapEmitter implements MapReduce.MapEmitter {
final Mapper.Context context;
final KryoWritable keyWritable = new KryoWritable<>();
final KryoWritable valueWritable = new KryoWritable<>();
public GiraphMapEmitter(final Mapper.Context context) {
this.context = context;
}
public void emit(final K key, final V value) {
this.keyWritable.set(key);
this.valueWritable.set(value);
try {
this.context.write(this.keyWritable, this.valueWritable);
} catch (Exception e) {
throw new IllegalStateException(e.getMessage(), e);
}
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy