首页 > 解决方案 > 使用 PySpark/Python 一次从 HDFS 分区顺序读取一条记录

问题描述

我想从 HDFS 分区中按顺序一次读取一条记录。我找到了一个处理此逻辑的示例 Java 片段。有没有办法使用 PySpark/Python 来实现这一点?

下面的示例 Java 片段(注意 while 循环):

FileSystem fileSystem = FileSystem.get(conf);
Path path = new Path("/path/file1.txt");
if (!fileSystem.exists(path)) {
System.out.println("File does not exists");
return;
}
FSDataInputStream in = fileSystem.open(path);
int numBytes = 0;
while ((numBytes = in.read(b))> 0) {
System.out.prinln((char)numBytes));// code to manipulate the data which is read
}
in.close();
out.close();
fileSystem.close();

标签: apache-sparkpysparkdatasethdfs

解决方案


推荐阅读