最近用libhdfs写入的测试代码,test.txt为300M的文本[ol]#include #include "hdfs.h"int main(int argc, char **argv) { hdfsFS fs = hdfsConnect("name.node", 8020); const char* writePath = "test.txt"; hdfsFile writeFile = hdfsOpenFile(fs, writePath, O_WRONLY|O_CREAT, 0, 0, 0); if(!writeFile) { fprintf(stderr, "Failed to open %s for writing!\n", writePath); exit(-1); } const char* pszInFile = "/tmp/test.txt"; FILE* fp = fopen(pszInFile, "r+"); if (fp != NULL) { char szBuffer[40960]; int nReadLen = 0; tSize offset = 0; do { nReadLen = fread(szBuffer, 1, sizeof(szBuffer), fp); hdfsSeek(fs, writeFile, offset); tSize num_written_bytes = hdfsWrite(fs, writeFile, (void*)szBuffer, nReadLen); offset += nReadLen; } while(nReadLen > 0); } if (hdfsFlush(fs, writeFile)) { fprintf(stderr, "Failed to 'flush' %s\n", writePath); exit(-1); } hdfsCloseFile(fs, writeFile); return 0;}[/ol]复制代码 |