拆分大文件

Split large files

本文关键字:文件 拆分      更新时间:2023-10-16

我正在开发一个分布式系统,在服务器中,它将向处理它们的客户端分发一个巨大的任务并返回结果。
服务器必须接受大小为20Gb的大文件。

服务器必须将此文件拆分为较小的部分,并将路径发送给客户端,而客户端又将scp文件并处理它们。

我正在使用readwrite来执行文件拆分,其执行速度慢得离谱。

法典

//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;
}  
  1. 有没有更快的方法来拆分文件?
  2. 客户端有什么方法可以在不使用 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() 读取它的切片(文件片段)。通过这种方式,到客户端,将传输文件中刚需的一部分。