package com.peidw;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.io.*;
import java.io.IOException;
import java.net.URI;
/**
* mapfile 例子
* Created by peidw on 2018-05-15.
*/
public class MapFileWriteDemo {
private static final String[] DATA = {
"One, two, buckle my shoe",
"Three, four, shut the door",
"Five, six, pick up sticks",
"Seven, eight, lay them straight",
"Nine, ten, a big fat hen"
};
public static void main(String[] args) throws IOException {
String uri="hdfs://localhost:9000/mapfile";
Configuration conf = new Configuration();
FileSystem fs = FileSystem.get(URI.create(uri), conf);
IntWritable key=new IntWritable();
Text value=new Text();
MapFile.Writer write=null;
try{
write =new MapFile.Writer( conf,fs,uri,key.getClass(),value.getClass());
for(int i=0;i<1024;i++){
key.set(i+1);
value.set(DATA[i%DATA.length]);
write.append(key,value);
}
}catch (Exception e){
}finally {
IOUtils.closeStream(write);
}
}
}
运行成功后,查看hdfs的记录
