HDFS文件的读写和关闭流

一、读取HDFS文件

public class ReadHdfsFileUtil {

	//添加日志输出能力
	Logger log = Logger.getLogger(ReadHdfsFileUtil.class);
	//加载配置文件到内存对象
	static Configuration conf = new Configuration();
	//读取HDFS文件内容
	public static void readHdfsFile(String hdfsPath) throws Exception{
		//文件路径的空判断
		if (hdfsPath == null || hdfsPath.trim().length() == 0) {
		throw new Exception("所要读取的源文件" + hdfsPath + ",不存在,请检查!");
		}
		//获取 conf 对应的 hdfs 集群的对象引用
		FileSystem fs = FileSystem.get(conf);
		//将给定的 hdfsPath 构建成一个 hdfs 的路径对象 Path
		Path path = new Path(hdfsPath);
		//字节转字符
		FSDataInputStream fsdis = fs.open(path);
		InputStreamReader isr = new InputStreamReader(fsdis);
		BufferedReader br = new BufferedReader(isr);
		String temp = null;
		while((temp = br.readLine()) != null){
			System.out.println(temp);
		}
		br.close();
	}
}

二、写入HDFS文件

1)、字节流

public static void writeToHdfsFile(String toHdfsPath,String localPath) throws Exception{
		FileSystem fs = FileSystem.get(conf);
		Path hdfsPath = new Path(toHdfsPath);
		FSDataOutputStream fsdos = fs.create(hdfsPath);
        //读取本地文件并写入HDFS文件
		String content = readLocalFile(localPath);
		fsdos.write(content.getBytes("utf-8"));
		fsdos.close();
	}

2)、字节转字符

这个时候要注意关闭流,即bw.close。在写文件的操作中如果不关闭是写不进去的,因为数据并没有落地到文件中,而是在内存中,程序执行完成后就被回收了。而且在读写文件操作的时候,不关闭流可能会占用过多的内存资源,导致内存溢出。

public static void writeToHdfsFile(String toHdfsPath,String localPath) throws Exception{
		FileSystem fs = FileSystem.get(conf);
		Path hdfsPath = new Path(toHdfsPath);
		FSDataOutputStream fsdos = fs.create(hdfsPath);
		OutputStreamWriter osw = new OutputStreamWriter(fsdos);
		BufferedWriter bw = new BufferedWriter(osw);
		List<String> list = readLocalFile(localPath);
		for(String str : list){
			bw.write(str);
			bw.write('\n');
		}
		bw.close();
	}

 

HDFS(Hadoop Distributed File System)是一个分布式文件系统,它的设计就是为了能够在通用硬件上运行,同时提供高吞吐量的数据访问。下面是HDFS文件读写的一些Java代码示例: 1. 读取文件 ```java // 获取Hadoop配置信息 Configuration conf = new Configuration(); // 构造一个HDFS文件系统对象 FileSystem fs = FileSystem.get(URI.create("hdfs://localhost:9000"), conf); // 构造一个输入,用于读取文件内容 Path inFile = new Path("/user/hadoop/input/test.txt"); FSDataInputStream in = fs.open(inFile); // 读取文件内容 byte[] buffer = new byte[1024]; int bytesRead = in.read(buffer); while (bytesRead > 0) { System.out.println(new String(buffer, 0, bytesRead)); bytesRead = in.read(buffer); } // 关闭输入 in.close(); ``` 2. 写入文件 ```java // 获取Hadoop配置信息 Configuration conf = new Configuration(); // 构造一个HDFS文件系统对象 FileSystem fs = FileSystem.get(URI.create("hdfs://localhost:9000"), conf); // 构造一个输出,用于写入文件内容 Path outFile = new Path("/user/hadoop/output/test.txt"); FSDataOutputStream out = fs.create(outFile); // 写入文件内容 out.write("Hello, World!".getBytes()); // 关闭输出 out.close(); ``` 3. 检查文件是否存在 ```java // 获取Hadoop配置信息 Configuration conf = new Configuration(); // 构造一个HDFS文件系统对象 FileSystem fs = FileSystem.get(URI.create("hdfs://localhost:9000"), conf); // 检查文件是否存在 Path path = new Path("/user/hadoop/input/test.txt"); boolean exists = fs.exists(path); System.out.println("File exists: " + exists); ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值