方法1:
單進程處理大規模的文件速度如(上million量級)比較慢,可以采用awk取模的方法,將文件分而治之,這樣可以利用充分的利用多核CPU的優勢
1
2
3
4
|
for
((i=0;i<5;i++));
do
cat
query_ctx.20k |
awk
'NR%5=='
$i
''
|\
wc
-l 1> output_$i 2>err_$i &
done
|
方法2:
另外也可以使用split的方法,或者hashkey 的辦法把大文件分而治之,
該辦法的缺陷是需要對大文件預處理,這個划分大文件的過程是單進程,也比較的耗時
1
2
3
4
5
6
7
8
9
10
|
infile=$1
opdir=querys
opfile=res
s=`
date
"+%s"
`
while
read
line
do
imei=`.
/awk_c
"$line"
`
no=`.
/tools/default
$imei 1000`
echo
$line >> $opdir/$opfile-$no
done
<$infile
|
方法3:
該方法是方法2的延伸,在預處理之后,可以使用shell腳本起多個進程來並行執行,當然為了防止進程之間因為並行造成的混亂輸出,可以使用鎖的辦法,也可以通過划分命名的辦法。下面的例子比較巧妙使用mv 操作。這一同步操作起到互斥鎖的作用,使得增加進程更加靈活,只要機器資源夠用,隨時增加進程,都不會造成輸出上的錯誤。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
output=hier_res
input=dbscan_res
prefix1=tmp-
prefix2=res-
for
file
in
`
ls
$input
/res
*`
do
tmp=`
echo
${
file
#*-}`
ofile1=${prefix1}${tmp}
ofile2=${prefix2}${tmp}
if
[ ! -f $output/$ofile1 -a ! -f $output/$ofile2 ];
then
touch
$output
/aaa_
$tmp
mv
$output
/aaa_
$tmp $output/$ofile1
if
[ $? -
eq
0 ]
then
echo
"dealing "
$
file
cat
$
file
| python hcluster.py 1> $output/$ofile1 2> hier.err
mv
$output/$ofile1 $output/$ofile2
fi
fi
done
|