(6)Java 读写 hdfs文件或者目录
2017-02-09 10:53
429 查看
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);
}
}
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对相对目录的文件读写和调用方法的一些总结
- java遍历D盘(或者其他盘)目录下所有的.txt文件,并输入文件名。
- Java常用面试题18 列出目录或者文件
- HDFS文件系统简单的Java读写操作
- Java 通过调用系统程序打开文件或者目录的方式
- 如何获得java工程或者java web工程中的src目录中的文件和webinf目录路径
- java在指定目录下执行dos命令或者bat文件
- java FTP 文件上传(给出的目录是文件夹,或者文件,都能上传到FTP服务器;如果是文件夹,只用链接一次FTP服务器即可)
- gen目录无法更新,或者gen目录下的R.JAVA文件无法生成
- java读写hdfs文件
- HDFS 读取、写入、遍历目录获取文件全路径、append文件创建或者写入报错问题分析
- HDFS上文件处理、Java文件读写
- java程序中获取项目或者目录或者文件路径的方法
- 在Java中简单的读写log或者txt文件内容
- java读写src目录下的properties文件
- java修改文件或者目录的最后修改时间
- 如何用java压缩文件或者目录
- Java web 项目读取src或者tomcat下class目录下的xml文件或者properties文件
- Java读写HDFS文件
- java获取工程目录下面或者引用jar包中的配置文件方式: