【问题标题】:Loading a TAR file and extracting its *bz2 contents to sdout with bzcat使用 bzcat 加载 TAR 文件并将其 *bz2 内容提取到 sdout
【发布时间】:2015-03-10 02:54:51
【问题描述】:

this question 之后,我正在尝试以有效的方式将带有 bz2 压缩 json 文件的 40 GB TAR file 加载到 PostgreSQL 中。

根据上面提到的答案,我正在尝试分离流程并使用外部工具来创建以下流程。

  • 使用 TAR(在本例中为 bsdtar,因为 TAR 在其 Windows 版本中不包括解压缩)打开文件并将其解压缩到 SDOUT,只有 *.bz2 文件。
  • 通过调用 bzcat 提取 *BZ2 文件(导出到 sdout)
  • 在我的 python 脚本“file_handling”中打开它,它将每个传入的行映射到一条推文并将其作为 csv 输出到标准输出
  • 通过管道将其加载到 PSQL 以将其加载到一个 COPY 命令中。

我目前在到达 bzcat 时遇到错误,这是我必须构建执行上述内容的行:

pipeline = [filename[1:3] + " && ",  # Change drive to H so that TAR can find the file without a drive name (doesn't like absolute paths, apparently).
            '"C:\\Tools\\GnuWin32\\gnuwin32\\bin\\bsdtar" vxOf ' + filename_nodrive + ' "*.bz2"',  # Call to tar, outputs to stdin
            " | C:\\Tools\\GnuWin32\\gnuwin32\\bin\\bzcat.exe"#,  # Forward its output to bzcat
            ' | python "D:\Cloud\Dropbox\Coding\GitHub\pyTwitter\pyTwitter_filehandling.py"', # Extract Tweets
            ' | "C:\Program Files\PostgreSQL\9.4\bin\psql.exe" -1f copy.sql ' + secret_login_d
           ]
module_call = "".join(pipeline)
module_call = "H: && "C:\Tools\GnuWin32\gnuwin32\bin\bsdtar" vxOf "Twitter datastream/Sourcefiles/archiveteam-twitter-stream-2013-01.tar" "*.bz2" | C:\Tools\GnuWin32\gnuwin32\bin\bzcat.exe | python "D:\Cloud\Dropbox\Coding\GitHub\pyTwitter\pyTwitter_filehandling.py" | "C:\Program Files\PostgreSQL\9.4in\psql.exe" -1f copy.sql "user=xxx password=xxx host=localhost port=5432 dbname=xxxxxx""

执行 TAR 的代码时,TAR 文件输出到 CMD 提示符,提示我一切正常。但是,bzcat这一行却带来了错误:

x 01/29/06/39.json.bz2
bzcat.exe: Data integrity error when decompressing.
    Input file = (stdin), output file = (stdout)

It is possible that the compressed file(s) have become corrupted.
You can use the -tvv option to test integrity of such files.

运行 -tvv 给了我:

huff+mtf data integrity (CRC) error in data

我尝试使用 7-zip (GUI) 提取相同的存档:这仍然有效。任何有关如何解决此问题的帮助将不胜感激。我正在运行带有 GNUWin32 的 Windows 8.1。

【问题讨论】:

  • 感谢@MattV 的这篇文章!对我的工作非常有用。您能否分享“file_handling”的python代码,以便我可以在存档上尝试这种方法?也许通过 github。谢谢!
  • 这是很久以前的事了,希望这仍然有意义:gist.github.com/MVersteeg/bb4a754823e7609bd204acbdb479d37d
  • 感谢 @MattV 提供出色的 Python 代码。还有一个问题:你有 copy.sql 文件吗?尝试写copy.sql很久了,结果总是“COPY 0”。

标签: tar bzip2 gnuwin32


【解决方案1】:

bsdtar.exe 将文件数据中的translating newline 字节写入 DOS CRLF 序列,导致 bzip2 输出流损坏。

GNU tar 在使用相对路径时有效,但在 Windows 中不处理绝对路径。

最好的办法是改用 7-zip:

7z.exe x -so -ir!*.json.bz2 archive.tar | bzcat | ...

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-03-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-06-18
    • 1970-01-01
    相关资源
    最近更新 更多