Hadoop中写入Hdfs中文件的两种方式:
1. 以文件为单位
String localSrc = args[0]; String dst = args[1]; InputStream in = new BufferednputStream(new FileInputStream(localSrc)); Configuration conf = new Configuration(); FileSysetm fs = FileSystem.get(URI.create(dst), conf); OutputStream out = fs.create(new Path(dst)); IOUtils.copyBytes(int, out, 4096, true);//参数3:缓冲区大小;参数4:文件操作结束后是否关闭数据流 /** java.lang.Object--->java.net.URI */
2. 以行为单位
String localSrc = args[0];
String dst = args[1];
BufferedReader in = new BufferedReader(new InputStreamReader(new FileInputStream(localSrc)));
Configuration conf = new Configuration();
FileSysetm fs = FileSystem.get(URI.create(dst), conf);
(Fs)DataOutputStream out = fs.create(new Path(dst));
while ((line = in.readLine()) != null ) {
out.writeBytes(line);
out.writeChar('\n');
out.flush();
}
3. Hadoop常用命令
hadoop fs -ls
hadoop fs -cat
hadoop fs -rmr
hadoop fs -mkdir directoryName
4. Hadoop编程中main函数的典型写法
Job job = new Job();
job.setJarByClass(Program.class);
FileInputFormat.addInputPath(job, new Path(args[0]));
FileOutputFormat.setOutputPath(job, new Path(args[1]));
job.setMapperClass(Program.class);
job.setReducerClass(Program.class);
job.setOutputKeyClass(Text.class);
job.setOutputValueClass(IntWritable.class);
System.exit(job.waitForCompletion(true) ? 0 : 1);
5. Hadoop中Path的两种构造方法
Path(String pathString);Resolve a path from a String
Path(String parent, String child);Resolve a child path against a parent path
6. Hadoop的MapReduce编程是一种函数式编程风格:在已知输入的情况下,得到已知的输出
7. map任务是由MapRunner负责运行的,它是默认的MapRunnable的实现,并且顺利地为每一条记录执行一次mapper的map()任务。
默认的partitioner是HashPartitioner,它将每条记录的键进行哈希处理来决定某条记录应该放到哪个分区上。每个分区上会对应一个reducer,所以分区的个数和reducer的个数是相同的。
默认的reducer是IdentityReducer,同样,它也是一个泛型。
8. 一个输入分片(input split)就是能够被单个map操作处理的输入块。每一个map操作只处理一个输入分片,并且 一个一个地处理每条记录,即一个键值对。
9. jobtracker与tasktracker
10. InputFormat : getRecordReader(), RecordReader
FileInputFormat : public static void addInputPath(Job job, new Path(String ));
FileInputFormat只会分割大文件,即大于HDFS块大小的文件(默认为64M).
11. TextInputFormat是默认的InputFormat,每一行数据都是一条记录。它的键是LongWritable类型的,存储该行在整个文件中的偏移量(以字节为单位)。而值是行中的数据内容,不包括任何行终止符,是Text类型的。
12. Reduce操作的输入键一定是有序的,但输出时的键不一定是有序的。
13. Mapper中map的实现:
protected void map(KEYIN key, VALUEIN value, Context context)
throws IOException, InterruptedException {
context.write((KEYOUT) key, (VALUEOUT) value);
}
Reducer中reduce的实现:
protected void reduce(KEYIN key, Iterable<VALUEIN> values, Context context )
throws IOException, InterruptedException {
for(VALUEIN value: values) {
context.write((KEYOUT) key, (VALUEOUT) value);
}
}
与0.20版本以前的IndentityMapper、IndentityReducer相同。
14. 在Hadoop中Mapper对应的输出格式的設定为setMapOutputKeyClass()与setMapOutputValueClass()
而Reducer对应的输出格式的設定为setOutputKeyClass()与setOutputValueClass()。
如果不設定,则Mapper的输出格式以Reducer的输出格式为准。但此时若程序中map()中設定的key与value的格式不是Reducer中的输出格式,则虽不会报错,但极有可能最終什么也不会输出。即在Mapper与Reducer的输出格式不同时,要单独对Mapper的输出格式进行設定。
15. 若想对前一个MapReduce输出的文件进行读取,务必保证前一个MapReduce中設定的FileOutputFormatClass与后一个MapReduce中設定的FileInputFormatClass中設定的输入输出格式一致。否则,极有可能什么也读不出来。
16. 在map与reduce函数中,可以对类的全局变量进行操作,与普通成员方法中对全局变量的操作无异。
文章来源:http://hi.baidu.com/shdren09/item/b9c80944269a95e4bcf45138