mapreduce执行遇到的一些问题
有个小需求,写个mapreduce处理下hdfs的文件,只用到了map阶段
遇到的问题:
1. reduce无法输出
解决:确保map和reduce的<key,value>键值对对应
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-nTRxncvR-1637829942870)(C:\Users\ThinkPad\AppData\Roaming\Typora\typora-user-images\image-20211125163543797.png)]
默认继承父类方法,这里去掉。
2. hadoop jar执行参数不正确,将项目代码中参数args[]往后顺延一位
FileInputFormat.addInputPath(job, new Path(args[1]));
//设置统计结果的输出路径
FileOutputFormat.setOutputPath(job, new Path(args[2]));
FileSystem fs=FileSystem.get(conf);
if (fs.exists(new Path(args[2]))){
fs.delete(new Path(args[2]),true);
3. 执行报错:java.lang.StringIndexOutOfBoundsException: String index out of range: 16
是由于字符串的长度小于16位,我再截取字符串的时候str.subString(0,16),由于字符串没有16位所以会报String index out of range: 16,这里解决可以用indexof取截取数字,同时使用这个indexof出现的问题
4.执行报错: java.lang.StringIndexOutOfBoundsException: String index out of range: -1
当字符串的值中没有索引的字符(.)的时候会抛出 String index out of range: -1 这个异常;
所以在使用indexOf()最好做一下判断。