1.读取单个文件
- Date date = DateUtil.getSpecifiedDayBefore();
- String yesterday = DateUtil.dateToStr(date, "yyyy-MM-dd");
- String path = "hdfs://ip:9000/output_log/output_log_click" + yesterday;
- Configuration conf = new Configuration();
- FileSystem fs = FileSystem.get(URI.create(path), conf);
- FSDataInputStream hdfsInStream = fs.open(new Path(path));
- InputStreamReader isr = new InputStreamReader(hdfsInStream, "utf-8");
- BufferedReader br = new BufferedReader(isr);
- String line;
- // int k = 0;
- while ((line = br.readLine()) != null) {
- System.out.println(line);
- }
2.读取文件夹
- Date date = DateUtil.getSpecifiedDayBefore();
- String yesterday = DateUtil.dateToStr(date, "yyyy-MM-dd");
- String path = "hdfs://ip:9000/output_log/output_log_click" + yesterday;
- Configuration conf = new Configuration();
- FileSystem fs = FileSystem.get(URI.create(path), conf);
- FileStatus[] status = fs.listStatus(new Path(path));
- for (FileStatus file : status) {
- if (!file.getPath().getName().startsWith("newsMap")) {
- continue;
- }
- FSDataInputStream hdfsInStream = fs.open(file.getPath());
- InputStreamReader isr = new InputStreamReader(hdfsInStream, "utf-8");
- BufferedReader br = new BufferedReader(isr);
- String line;
- // int k = 0;
- while ((line = br.readLine()) != null) {
- System.out.println(line);
- }
- }
本文提供了一个使用Java从HDFS中读取单个文件和文件夹的具体示例。通过示例代码展示了如何配置Hadoop环境并利用FileSystem API进行文件读取操作。
739

被折叠的 条评论
为什么被折叠?



