【发布时间】:2019-01-01 12:04:39
【问题描述】:
我在 AWS S3 中有一个非常大的文件(几 GB),我只需要文件中满足特定条件的少量行。我不想将整个文件加载到内存中,然后搜索并打印那几行 - 这样做的内存负载太高了。正确的方法是只在内存中加载那些需要的行。
根据 AWS 文档to read from file:
fullObject = s3Client.getObject(new GetObjectRequest(bucketName, key));
displayTextInputStream(fullObject.getObjectContent());
private static void displayTextInputStream(InputStream input) throws IOException {
// Read the text input stream one line at a time and display each line.
BufferedReader reader = new BufferedReader(new InputStreamReader(input));
String line = null;
while ((line = reader.readLine()) != null) {
System.out.println(line);
}
System.out.println();
}
这里我们使用BufferedReader。我不清楚下面发生了什么。
我们是否在每次读取新行时都对 S3 进行网络调用,并且只将当前行保留在缓冲区中?还是将整个文件加载到内存中,然后由 BufferedReader 逐行读取?还是介于两者之间?
【问题讨论】:
-
来自link you posted:注意您的网络连接保持打开状态,直到您读取所有数据或关闭输入流。我们建议您尽快阅读直播内容。
-
我的问题更多的是——将整个文件加载到内存中,还是只加载我正在阅读的行,或者介于两者之间的缓冲区?
-
只需编写一个小示例应用程序并尝试使用上述代码从S3读取文件。如果它会立即将孔文件读入内存,您肯定会遇到OOM。
标签: java amazon-s3 inputstream bufferedreader bufferedinputstream