shell脚本处理大数据系列之(一)方法小结

转自:http://longriver.me/?p=57

方法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

转载于:https://www.cnblogs.com/yaohaitao/p/5779091.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值