拆分大文件
Split large files
我正在开发一个分布式系统,在服务器中,它将向处理它们的客户端分发一个巨大的任务并返回结果。
服务器必须接受大小为20Gb的大文件。
服务器必须将此文件拆分为较小的部分,并将路径发送给客户端,而客户端又将scp文件并处理它们。
我正在使用read
和write
来执行文件拆分,其执行速度慢得离谱。
法典
//fildes - Source File handle
//offset - The point from which the split to be made
//buffersize - How much to split
//This functions is called in a for loop
void chunkFile(int fildes, char* filePath, int client_id, unsigned long long* offset, int buffersize)
{
unsigned char* buffer = (unsigned char*) malloc( buffersize * sizeof(unsigned char) );
char* clientFileName = (char*)malloc( 1024 );
/* prepare client file name */
sprintf( clientFileName, "%s%d.txt",filePath, client_id);
ssize_t readcount = 0;
if( (readcount = pread64( fildes, buffer, buffersize, *offset ) ) < 0 )
{
/* error reading file */
printf("error reading file n");
}
else
{
*offset = *offset + readcount;
//printf("Read %ud bytesn And offset becomes %llun", readcount, *offset);
int clnfildes = open( clientFileName, O_CREAT | O_TRUNC | O_WRONLY , 0777);
if( clnfildes < 0 )
{
/* error opening client file */
}
else
{
if( write( clnfildes, buffer, readcount ) != readcount )
{
/* eror writing client file */
}
else
{
close( clnfildes );
}
}
}
free( buffer );
return;
}
- 有没有更快的方法来拆分文件?
- 客户端有什么方法可以在不使用 scp 的情况下访问其在文件中的块(无需传输即可读取)?
我正在使用C++。如果其他语言可以执行得更快,我准备使用它们。
您可以将
文件放在Web服务器的访问范围内,然后使用客户端中的curl
curl --range 10000-20000 http://the.server.ip/file.dat > result
将获得 10000 字节(从 10000 到 20000)
如果文件高度冗余并且网络速度很慢,则使用压缩可能有助于大大加快传输速度。例如执行
nc -l -p 12345 | gunzip > chunk
在客户端上,然后执行
dd skip=10000 count=10000 if=bigfile bs=1 | gzip | nc client.ip.address 12345
在服务器上,您可以动态传输执行 gzip 压缩的部分,而无需创建中间文件。
编辑
通过网络使用压缩从服务器获取文件部分的单个命令是
ssh server 'dd skip=10000 count=10000 bs=1 if=bigfile | gzip' | gunzip > chunk
rsync over SSH 和 --partial 是一个选项吗?然后,您可能也不需要拆分文件,因为如果传输中断,您可以继续。
文件拆分大小是事先知道的,还是沿着文件中的某个标记拆分的?
您可以将文件存入 NFS 共享设备,客户端可以在 RO 模式下挂载该设备。此后,客户端可以打开文件,并使用 mmap() 或 pread() 读取它的切片(文件片段)。通过这种方式,到客户端,将传输文件中刚需的一部分。
相关文章:
- 如何拆分文件中.txt字母并使用c ++使用数组进行扑克?
- C++(.cpp文件和.h文件)拆分代码并添加一个函数,提取 - 这很容易吗?
- 如何在拆分为多个文件的分层 SM 中退出子机器?(使用boost::MSM)
- 如何通过在逗号处拆分将文件读入 2D 列表
- 读取行文本文件,如果它得到逗号,则拆分
- 将 c++ 代码拆分为多个文件
- 从文件中逐块读取,然后逐行拆分测试
- 如何使用 c++ 逐行处理文本文件并将其参数拆分为变量?
- C++ 将类拆分为多个文件.当我指定类型时假定显式类型 int
- 如何将程序拆分为文件C 类
- 从文件中获取字符串并将其拆分
- C++强力球 - 文件拆分和错误(游戏类型)
- 按代码 c 拆分文件时出错
- 分析示例文本文件并将其拆分
- 如何将XML文件拆分为多个XML,以便每个新文件只包含原始文件中的一个文本节点
- 将大型数据文件拆分为多个小文件(文本格式)
- 将文本文件拆分为多个文件 c++
- 将一个文件拆分为多个文件
- 我应该始终将文件拆分为声明(.h)和定义(.cpp)吗
- 如何将数据文件拆分为多个部分