我可以在java中遍历以特定字符串开头的HDFS文件吗?
我试图读取多个HDFS.gz文件,但我只想要那些以昨天的日期作为文件名开始的文件。我的文件如下所示:我可以在java中遍历以特定字符串开头的HDFS文件吗?,java,filesystems,hdfs,Java,Filesystems,Hdfs,我试图读取多个HDFS.gz文件,但我只想要那些以昨天的日期作为文件名开始的文件。我的文件如下所示: /notmy-data/openSourceDatasets/Temperatures/2013-06-10T133006.gz /notmy-data/openSourceDatasets/Temperatures/2013-06-11T153006.gz /notmy-data/openSourceDatasets/Temperatures/2013-06-11T173006.gz /not
/notmy-data/openSourceDatasets/Temperatures/2013-06-10T133006.gz
/notmy-data/openSourceDatasets/Temperatures/2013-06-11T153006.gz
/notmy-data/openSourceDatasets/Temperatures/2013-06-11T173006.gz
/notmy-data/openSourceDatasets/Temperatures/2013-06-11T193006.gz
这就是我所拥有的
DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
Calendar cal = Calendar.getInstance();
cal.add(Calendar.DATE, -1);
String yesterdate = dateFormat.format(cal.getTime());
Path tpath = new Path("/notmy-data/openSourceDatasets/Temperatures/" + yesterdate + "*");
FileStatus[] status = fileSystem.listStatus(tpath);
System.out.println("["+new Date()+"] Starting tempertaure ingest...");
for (int i=0;i<status.length;i++){
BufferedReader reader =new BufferedReader(new InputStreamReader(new GZIPInputStream(fileSystem.open(status[i].getPath()))));
String line;
while (null != (line = reader.readLine())){
System.out.println(line);
}
DateFormat DateFormat=新的SimpleDateFormat(“yyyy-MM-dd”);
Calendar cal=Calendar.getInstance();
cal.add(Calendar.DATE,-1);
字符串yesterdate=dateFormat.format(cal.getTime());
路径tpath=新路径(“/notmy data/openSourceDatasets/Temperatures/”+yesterdate+“*”);
FileStatus[]status=fileSystem.listStatus(tpath);
System.out.println(“[”+新日期()+“]开始温度Taure摄入…”);
对于(int i=0;i使用:
//为您做环球旅行!
最终目录流dirstream
=Files.newDirectoryStream(path.get(“yourBaseDir”)、yesterdate+'*');
for(最终路径:dirstream)
//使用“路径”进行操作
然而,真正的答案必须等到你说出FileStatus
是什么之后
此外,要在路径
对象上打开一个新的BufferedReader
,它比您所做的要简单得多:使用。这可能不是最好的方法,但它很有效
Path tpath = new Path("/notmy-data/openSourceDatasets/Temperatures/"); ****changed****
FileStatus[] status = fileSystem.listStatus(tpath);
System.out.println("["+new Date()+"] Starting tempertaure ingest...");
for (int i=0;i<status.length;i++){
****added****
String[] fileNameBits = status[i].getPath().toString().split("/");
String fileDate = fileNameBits[fileNameBits.length - 1].split("T")[0];
String yesterString = yesterdate.toString();
if (!fileDate.equals(yesterString)){
continue;
}
****to here****
BufferedReader reader =new BufferedReader(new InputStreamReader(new GZIPInputStream(fileSystem.open(status[i].getPath()))));
String line;
while (null != (line = reader.readLine())){
System.out.println(line);
}
Path tpath=新路径(“/notmy data/openSourceDatasets/Temperatures/”);****已更改****
FileStatus[]status=fileSystem.listStatus(tpath);
System.out.println(“[”+新日期()+“]开始温度Taure摄入…”);
对于(int i=0;i什么是FileStatus
?它不能到达FileStatus
,因为tpath不是一个存在的文件。请看我的答案;Path
不会为您自动全局化。啊……那么,Path
是特定于您的库的吗?Eclipse找不到DirectoryStream。我添加了import java.nio.
,因为它无法自动完成类…嗯,那么,您的Java是否太旧了?:pI添加了导入,但它不会编译,因为DirectoryStream是一个未定义的类。DirectoryStream
在包Java.nio.file
中,而不是Java.nio
Path tpath = new Path("/notmy-data/openSourceDatasets/Temperatures/"); ****changed****
FileStatus[] status = fileSystem.listStatus(tpath);
System.out.println("["+new Date()+"] Starting tempertaure ingest...");
for (int i=0;i<status.length;i++){
****added****
String[] fileNameBits = status[i].getPath().toString().split("/");
String fileDate = fileNameBits[fileNameBits.length - 1].split("T")[0];
String yesterString = yesterdate.toString();
if (!fileDate.equals(yesterString)){
continue;
}
****to here****
BufferedReader reader =new BufferedReader(new InputStreamReader(new GZIPInputStream(fileSystem.open(status[i].getPath()))));
String line;
while (null != (line = reader.readLine())){
System.out.println(line);
}