目前,我有以下代码。
try
{
FileSystem fs = FileSystem.get(config);
StringBuilder builder=new StringBuilder();
byte[] buffer=new byte[4096];
int bytesRead;
FSDataInputStream in = fs.open(new Path(fname));
while ((bytesRead = in.read(buffer)) > 0)
builder.append(new String(buffer, 0, bytesRead));
in.close();
return builder.toString();
}
catch (IOException ex)
{
ex.printStackTrace();
return "";
}
如您所见,我已经为缓冲区分配了4096个字节。现在,由于HDFS块为64 MB,分配大小为64 MB的缓冲区会更好吗?鉴于我的HDFS文件大小> 64 MB,什么是最有效的写方法。
最佳答案
您可以读取HDFS属性值以具有正确的缓冲区大小“dfs.stream-buffer-size”。
关于scala - 将HDFS文件读取为字符串的最佳方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38032835/