【问题标题】:How to accelerate a shell script for a mongoDB application?如何加速 mongoDB 应用程序的 shell 脚本?
【发布时间】:2014-06-12 13:52:53
【问题描述】:

我开始为我的工作编写 shell 脚本,但我必须承认,我离成为新手还很遥远。因此,我想向您寻求帮助/建议。

我为大数据应用程序构建了一个脚本(采用快速而肮脏的方法,将互联网上的东西修补在一起)以递归方式遍历文件夹结构并将所有 XML 文件转换为 JSON。

我的脚本现状是:

#!/bin/sh
# Shell script to find out all the files under a directory and
#its subdirectories. This also takes into consideration those files
#or directories which have spaces or newlines in their names

cd /Users/q337498/Desktop/Archiv/2014/01/10

DIR="."

function list_files()
{
if !(test -d "$1")
then echo $1; return;
fi

cd "$1"
#echo; echo `pwd`:; #Display Directory name

for i in *
    do
        if test -d "$i"; then # if dictionary
            if [ "$(ls -A $i)" ]; then
                list_files "$i" #recursively list files
                cd ..
            else
               echo "$i is Empty"
            fi
        else
            java -jar /Users/q337498/Desktop/XML2JSON/SaxonEE9-5-1-4J/saxon9ee.jar -s:"$i" -xsl:/Users/q337498/Desktop/xsltjson-master/conf/xml-to-json.xsl -o:output/$(pwd)/${i%%[.]*}
#            if jsonlint /Users/q337498/Desktop/Archiv/2014/01/08/$(pwd)/${i%%[.]*} -q; then
#                echo "GOOD"
#            else
#                echo "NOT GOOD"
#            fi
#          echo ${i%%[.]*}
#          echo "$i"; #Display File name
        fi
    done
}

if [ $# -eq 0 ]
    then list_files .
    exit 0
fi

for i in $*
    do
        DIR="$1"
        list_files "$DIR"
        shift 1 #To read next directory/file namedone
    done

此代码有效,但问题是对于 60000 个文件,在配备 16gb RAM 和 2.8 Ghz i7 的 macbook pro 上最多需要 15 个小时。而且我需要转换 1000 万个文件。

您认为我可以如何加速脚本?并行化?拿出一些命令?我有哪些选择,我将如何实际实施它们?

文件最终会保存在 MongoDB 中,因此如果有人知道将 xml 转换为 json 并将其上传到 mongo 的更好方法,也欢迎他的输入。

干杯,

嘟嘟

【问题讨论】:

    标签: json mongodb shell parallel-processing bigdata


    【解决方案1】:

    我在这里看到 2 个直接问题:

    1. 您正在为每个文件调用一次 java,因此会导致每个文件的 JVM 启动时间加起来会占用大量时间。
    2. 您正在运行单线程

    所以我建议你:

    • 编写一个执行目录遍历和转换的 Java 程序
    • 对性能差异进行基准测试
    • 尝试使用其他 Java 库进行 XML->JSON 转换https://github.com/beckchr/staxon/wiki/Benchmark
    • 如果需要提高性能,请使用 java.util.concurrent 向您的应用程序添加多线程。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-05-06
      • 2011-09-27
      • 2022-06-14
      • 1970-01-01
      相关资源
      最近更新 更多