1、将文件从本地上传到HDFS中报错
错误描述:
将本地文件使用代码上传至HDFS中出现错误提示:“Exception in thread "main" java.lang.IllegalArgumentException: Wrong FS: hdfs://192.168.163.131:9000/local, expected: file:///”,如下图:
源代码:
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileStatus;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
public class CopyFile {
public static void main(String[] args) throws Exception{
Configuration conf = new Configuration();
FileSystem hdfs = FileSystem.get(conf);
Path src = new Path("F:/test.txt");
Path dst = new Path("hdfs://192.168.163.131:9000/local");
hdfs.copyFromLocalFile(src, dst);
System.out.println("Upload to" + conf.get("fs.default.name"));
FileStatus files[] = hdfs.listStatus(dst);
for(FileStatus file:files){
System.out.println(file.getPath());
}
}
}
解决方案:
添加conf.set("fs.default.name","hdfs://192.168.163.131:9000");设置,并将dst做相应的更改,正确代码如下:
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileStatus;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
public class CopyFile {
public static void main(String[] args) throws Exception{
Configuration conf = new Configuration();
conf.set("fs.default.name","hdfs://192.168.163.131:9000");
FileSystem hdfs = FileSystem.get(conf);
Path src = new Path("F:/test.txt");
Path dst = new Path("/local");
hdfs.copyFromLocalFile(src, dst);
System.out.println("Upload to" + conf.get("fs.default.name"));
FileStatus files[] = hdfs.listStatus(dst);
for(FileStatus file:files){
System.out.println(file.getPath());
}
}
}
2、本地文件上传至HDFS中文乱码
解决方案:
Step1:将本地文件编码格式设置为UTF-8
Step2:Window --> Preferences --> Workspace --> 将Text file encoding设置为UTF-8
Step3:项目名称右击 --> Properties --> Resource --> 将Text file encoding设置为UTF-8
3、java.net.ConnectException: Call From DESKTOP-7VC79C3/192.168.56.1 to 192.168.163.131:8020 ...
错误描述:
错误提示: Exception in thread "main" java.net.ConnectException: Call From DESKTOP-7VC79C3/192.168.56.1 to 192.168.163.131:8020 failed on connection exception: java.net.ConnectException: Connection refused: no further information; For more details see: http://wiki.apache.org/hadoop/ConnectionRefused
错误截图:
问题原因:
FileSystem.get(conf)得到的是一个LocalFileSystem的instance,下面强制转化成DistributedFileSystem时出现问题,应设置configuration.set("fs.default.name", "hdfs://192.168.163.131"); 在设置的时候,只设置了hadoop集群上的master的ip,ip后面忘记添加port。
解决方案:
将configuration.set("fs.default.name", "hdfs://192.168.163.131");改成configuration.set("fs.default.name", "hdfs://192.168.163.131:9000");
4、map()函数和reduce()函数不被执行
问题叙述:
将map、reduce函数编写好,设置好configuration、job等之后,发现文件读取等都正常,但是map()函数不被执行。
问题原因:
待解决
解决方案:
将在主函数中设置的job.setInputFormatClass(KeyValueTextInputFormat.class);注释掉即可。