【发布时间】: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