java读取hdfs时报错:
Wrong FS: hdfs://centos1:9000/user/hadoop/input, expected: file:///
首先确认 是用 FileSystem.get(conf) 而不是 FileSystem.getLocal(conf) 初始化的 FileSystem 对象.
如果还有问题: 把集群里的 core-site.xml mapped-site.xml hdfs-site.xml slaves 等文件拷贝到eclipse 配置的 Hadoop 目录里.
//hdfs://centos1:9000//user/hadoop/input hdfs://centos1:9000//user/hadoop/output public static void main(String[] args) throws Exception { Configuration conf = new Configuration(); Path inputPath = new Path("hdfs://centos1:9000/user/hadoop/input/"); //FileSystem file = FileSystem.getLocal(conf); FileSystem file = FileSystem.get(conf); FileStatus[] fileStatus = file.listStatus(inputPath); byte[] temp = new byte[1024]; if(fileStatus!=null && fileStatus.length>0){ for (FileStatus status : fileStatus) { System.out.println(status.getPath().getName() + "--"+ status.getLen()/1024/1024.0); FSDataInputStream in = file.open(status.getPath()); int i = 0; while((in.read(temp))!=-1){ System.out.println(new String(temp, 0, i)); } in.close(); } } file.close(); System.out.println("end."); }