Created
March 26, 2018 14:46
-
-
Save DavidRdgz/680a9b094e7ab9954c1975aeb592c731 to your computer and use it in GitHub Desktop.
A mapper to append and tag new information on a field: mmap a file using LMDB.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
A | |
B | |
C | |
D | |
E | |
F | |
G | |
H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* gradle clean | |
* gradle build | |
* <p> | |
* hadoop jar build/libs/mapper-lmdb-1.0-SNAPSHOT.jar com.dvidr.MapperLMDB src/main/resources/keys.txt src/main/resources/output | |
*/ | |
package com.dvidr; | |
import org.apache.hadoop.conf.Configuration; | |
import org.apache.hadoop.conf.Configured; | |
import org.apache.hadoop.fs.Path; | |
import org.apache.hadoop.io.LongWritable; | |
import org.apache.hadoop.io.Text; | |
import org.apache.hadoop.mapreduce.Job; | |
import org.apache.hadoop.mapreduce.Mapper; | |
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat; | |
import org.apache.hadoop.mapreduce.lib.input.TextInputFormat; | |
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat; | |
import org.apache.hadoop.mapreduce.lib.output.TextOutputFormat; | |
import org.apache.hadoop.util.Tool; | |
import org.apache.hadoop.util.ToolRunner; | |
import org.fusesource.lmdbjni.Database; | |
import org.fusesource.lmdbjni.Env; | |
import java.io.IOException; | |
import java.nio.charset.Charset; | |
public class MapperLMDB extends Configured implements Tool { | |
public static class AMap extends Mapper<LongWritable, Text, Text, Text> { | |
private static String TMP_DB_PATH = "/tmp/myLmdbTests"; | |
private Text word = new Text(); | |
private Text word2 = new Text(); | |
private Env env; | |
private Database db; | |
private void setupLMDB() { | |
env = new Env(); | |
env.open(TMP_DB_PATH); | |
db = env.openDatabase(); | |
} | |
@Override | |
protected void setup(Context context) throws IOException, InterruptedException { | |
Configuration conf = context.getConfiguration(); | |
setupLMDB(); | |
} | |
@Override | |
public void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException { | |
// file schema: hostname country | |
String[] parts = value.toString().split("\\s"); | |
word.set(parts[0]); | |
byte[] lmdbKey = parts[0].getBytes(Charset.forName("UTF-8")); | |
byte[] lmdbValue = db.get(lmdbKey); | |
if (lmdbValue == null) return; | |
String s = new String(lmdbValue); | |
word2.set(s); | |
context.write(word, word2); | |
} | |
} | |
public int run(String[] args) throws Exception { | |
Configuration conf = getConf(); | |
Job job = Job.getInstance(conf); | |
job.setJarByClass(MapperLMDB.class); | |
job.setOutputKeyClass(Text.class); | |
job.setOutputValueClass(Text.class); | |
job.setMapperClass(AMap.class); | |
job.setInputFormatClass(TextInputFormat.class); | |
job.setOutputFormatClass(TextOutputFormat.class); | |
FileInputFormat.setInputPaths(job, new Path(args[0])); | |
FileOutputFormat.setOutputPath(job, new Path(args[1])); | |
return (job.waitForCompletion(true) ? 0 : 1); | |
} | |
public static void main(String[] args) throws Exception { | |
int exitCode = ToolRunner.run(new Configuration(), new MapperLMDB(), args); | |
System.exit(exitCode); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment