【发布时间】:2021-12-11 06:48:43
【问题描述】:
我有一个文件 (~.9GB),我正在尝试读取缓冲区大小为 1MiB 的文件,它会导致 65536 字节的短读取。
bash-4.2$ hadoop fs -ls /x/F1
-rw-r--r-- 3 hdfs supergroup 996147200 2021-10-25 13:56 /x/F1
bash-4.2$
为了缩小范围,我在下面有一个简单的示例代码。
void readtest()
{
hdfsFS fs = hdfsConnect("default", 0);
const char* readPath = "/x/F1";
hdfsFile readFile = hdfsOpenFile(fs, readPath, O_RDONLY |O_CREAT, 0, 0, 0);
if(!readFile) {
fprintf(stderr, "Failed to open %s for reading!\n", readPath);
exit(-1);
}
char* buffer = malloc(1048576);
int count = hdfsRead(fs, readFile, (void*) buffer, 1048576);
printf("\n count on read %d \n", count);
}
bash-4.2$ ./sample
count on read 65536
bash-4.2$
然后我使用了原生 blocksize api,blocksize 原来是 128MB ,但是 read 仍然返回 64K。
void readtest()
{
hdfsFS fs = hdfsConnect("default", 0);
const char* readPath = "/x/F1";
hdfsFile readFile = hdfsOpenFile(fs, readPath, O_RDONLY |O_CREAT, 0, 0, 0);
if(!readFile) {
fprintf(stderr, "Failed to open %s for writing!\n", readPath);
exit(-1);
}
int blocksize = hdfsGetDefaultBlockSizeAtPath(fs, readPath);
printf("\n blocksize %d \n", blocksize);
char* buffer = malloc(blocksize);
int count = hdfsRead(fs, readFile, (void*) buffer, blocksize);
printf("\n count on read %d \n", count);
}
bash-4.2$ ./sample
blocksize 134217728
count on read 65536
bash-4.2$
为什么会有这样的行为?请有任何建议。
【问题讨论】: