libhdfs的hdfsWrite上传文件最大为4G,怎么办?

FlyGently 2017-05-08 04:41:52
libhdfs的
tSize hdfsWrite(hdfsFS fs, hdfsFile file, const void* buffer, tSize length);
其中
typedef int32_t tSize;
那么该函数一次上传的最大长度为4G,难道用C++向HDFS上传文件,最大上传4G吗?
感谢各位回答,不胜感激

...全文
1270 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
FlyGently 2017-10-24
  • 打赏
  • 举报
回复

    // 2.1 打开本地文件
    QFile readFile(strFileFull);
    if(!readFile.open((QIODevice::ReadOnly)))
    {
        qDebug("Failed to open file: %s!", strFileFull.toStdString().data());
        m_pStdItemModel->item(n, 2)->setText(tr("打开本地文件失败"));
        return false;
    }

    // 3. 向HDFS中写入文件
    QString strWritePath = Global::GetConfig().GetHDFSMissionDataPath() + strFilePath;   // if there has no '/' befor missionData, the path in HDFS will be /usr/<username>/temp/<strFileName>
    // 3.1 上传前记录文件是否已存在
    bool bExist = m_operateHDFS.exists(strWritePath.toStdString().data());
    // 3.2 hdfs open file
    if(!m_operateHDFS.open(strWritePath.toStdString().data(), O_WRONLY|O_CREAT))
    {
        m_pStdItemModel->item(n, 2)->setText(tr("HDFS文件打开失败"));
        return false;
    }
    int nGB = 1024 * 1024 * 1024;
    // 3.3 从本地文件读取nGB字节,写入HDFS,并设置文件偏移量
    qDebug("写前偏移%ld", m_operateHDFS.tell());
    for (int i=0; i<fileInfo.size()/nGB; i++)
    {
        // 3.3.1 从本地文件读取nGB字节,并设置文件偏移量
        readFile.seek(i * nGB);
        QByteArray qByteArry = readFile.read(nGB);
        // 3.3.2 hdfs write
        m_operateHDFS.write(qByteArry.data(), nGB);
        qDebug("写中偏移%ld", m_operateHDFS.tell());
    }
    // 3.3.3 读取本地文件中剩下不足1G的数据,写入HDFS
    int nRemaining = readFile.size() % nGB;
    readFile.seek(fileInfo.size() / nGB * nGB);
    QByteArray qByteArry = readFile.read(nRemaining);
    if (-1 == m_operateHDFS.write(qByteArry.data(), nRemaining))
    {
        m_pStdItemModel->item(n, 2)->setText(tr("HDFS写入失败"));
        return false;
    }
    qDebug("写后偏移%ld", m_operateHDFS.tell());
    // 3.4 hdfs flush
    if (-1 == m_operateHDFS.flush())
    {
        m_pStdItemModel->item(n, 2)->setText(tr("HDFS flush失败"));
        return false;
    }
    // 3.4 hdfs close file
    m_operateHDFS.close();
    // 2.3 close file
    readFile.close();
FlyGently 2017-10-24
  • 打赏
  • 举报
回复
我自己搞定了,其中m_operateHDFS是我自己封装的读写hdfs的类的对象

    // 2.1 打开本地文件
    QFile readFile(strFileFull);
    if(!readFile.open((QIODevice::ReadOnly)))
    {
        qDebug("Failed to open file: %s!", strFileFull.toStdString().data());
        m_pStdItemModel->item(n, 2)->setText(tr("打开本地文件失败"));
        return false;
    }

    // 3. 向HDFS中写入文件
    QString strWritePath = Global::GetConfig().GetHDFSMissionDataPath() + strFilePath;   // if there is not '/' befor missionData, the path in HDFS will be /usr/<username>/temp/<strFileName>
    // 3.1 上传前记录文件是否已存在
    bool bExist = m_operateHDFS.exists(strWritePath.toStdString().data());
    // 3.2 hdfs open file
    if(!m_operateHDFS.open(strWritePath.toStdString().data(), O_WRONLY|O_CREAT))
    {
        m_pStdItemModel->item(n, 2)->setText(tr("HDFS文件打开失败"));
        return false;
    }
    // 2.2 read file
    int nGB = 1024 * 1024 * 1024;
    // 3.3 hdfs write
    qDebug("写前偏移%ld", m_operateHDFS.tell());
    for (int i=0; i<fileInfo.size()/nGB; i++)
    {
        readFile.seek(i * nGB);
        QByteArray qByteArry = readFile.read(nGB);
        m_operateHDFS.write(qByteArry.data(), nGB);
        qDebug("写中偏移%ld", m_operateHDFS.tell());
    }
    readFile.seek(fileInfo.size() / nGB * nGB);
    if (-1 == m_operateHDFS.write(readFile.read(readFile.size() % nGB), readFile.size() % nGB))
    {
        m_pStdItemModel->item(n, 2)->setText(tr("HDFS写入失败"));
        return false;
    }
    qDebug("写后偏移%ld", m_operateHDFS.tell());
    // 3.4 hdfs flush
    if (-1 == m_operateHDFS.flush())
    {
        m_pStdItemModel->item(n, 2)->setText(tr("HDFS flush失败"));
        return false;
    }
    // 3.4 hdfs close file
    m_operateHDFS.close();
    // 2.3 close file
    readFile.close();
FlyGently 2017-06-06
  • 打赏
  • 举报
回复
没人回我啊没人回我

932

社区成员

发帖
与我相关
我的任务
社区描述
云计算 云存储相关讨论
社区管理员
  • 云存储
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧