java操作hdfs文件系统

maven做如下配置

groupId:org.apache.hadoop
artifactId:hadoop-client

version:${hadoop.version}

代码如下:

获取文件系统

	private static FileSystem getFileSystem() throws Exception{
		Configuration conf = new Configuration();
		FileSystem fileSystem = FileSystem.get(conf);
		return fileSystem;
	}

写文件

	private static void write(String toFolder) throws Exception{
		FileSystem fileSystem = getFileSystem();
		fileSystem.delete(new Path(toFolder), true);
		FileInputStream in = null;
		FSDataOutputStream out = null;
		try {
			in = new FileInputStream(new File("E:\\1804211523.txt"));
			out = fileSystem.create(new Path(toFolder));
			IOUtils.copyBytes(in, out, 4096, false);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			IOUtils.closeStream(in);
			IOUtils.closeStream(out);
		}
		
	}

读取文件:

	private static void read(String filePath) throws Exception{
		//预处理,删除文件加
		FileSystem fileSystem = getFileSystem();
		Path path = new Path(filePath);
		FSDataInputStream inStream = null;
		//用文件系统操作路径
		try{
			inStream = fileSystem.open(path);
			IOUtils.copyBytes(inStream, System.out, 4096, false);
		}catch(Exception e){
			e.printStackTrace();
		}finally{
			IOUtils.closeStream(inStream);
		}
	}

猜你喜欢

转载自blog.csdn.net/fanghailiang2016/article/details/80302456