哦哦哦,我本是對資料探勘有著無限的興趣,現在卻從事大資料處理的工作,但是上班是上班,平時依然奮發向上,自學機器學習的東西。但是作為一名非常合格的員工(哈哈,很有不要臉的氣質,這很sunnyin),不能總是在自己博文中記錄其他的學識吧,萬一哪天被老大看到,豈不得讓我雨露均沾,受得萬般恩寵。。。。
好了,今天的廢話又是說的如此到位,下面進入正題。也就是我現在的這個loooooooooooow專案的資料處理架構。
當然今天不是來講架構的,而是向貼上去一段我寫的shell指令碼。希望看到的朋友給出一些寶貴的建議。
先簡單說一下我們這個loooooooooow架構:
其中交換機與ftp共享平台之間是用的萬兆光纖傳輸,每個檔案有380m左右大小,每小時有將近200個檔案需要處理,也就是一天大概處理1.8t的資料量,需要插入10張表中。
為了實現在處理資料時可以併發處理,我針對每一張表都寫了兩個指令碼,其中乙個放在交換機中,另乙個放在hadoop集群中(最好放在datanode節點中)。
**如下:
#/bin/bash
#this script will deal with the dns_data.
login_ftp_user=$1
login_ftp_passwd=$2
remote_dir=/home/sunnyin/test/longname #test dir
local_dir=$3
#test dir /home/hadoop/test_818/dataxx
ftp_host=$4
time_now=$5
length_p5=`echo
$|wc -l`
data_dir=$local_dir/dns/$time_now
#介面路徑
#check the parameter
if [ -z $ ]; then
time_now=`date +%y%m%d%h`
echo
"will get data of $time_now...."
elif [ $
-ne10 ];then
echo
"the parameter 5 is wrong,please check and run again..."
exit
1else
echo
"will get data of $time_now...."
fi#make dir and change dir
if [ -d
$ ]; thencd$
else
echo
"the dictionary is not exist,will create..."
mkdir -p $cd$
fi#login ftp
for (( i=0;i<=15;i++))
doif [ $i
-lt10 ];then
j=`printf
"%02d"
"$i"`
else
j=$i
fiftp -n
user $$cd
$/dns
prompt
mget *$
$*close
bye!
done
if [ $? -eq
0 ]; then
echo
"files transfer is completed...(0~15 minutes data)"
else
echo
"file transfer failed"
exit1fi
#make a tuple(files name)
files=(`ls $data_dir`)
table_name_time=(`ls $data_dir|awk -f _ ''`) #time:yyyymmhhmmss
#split
for file in$do
split -c 128m $file
-d-a
3$table_name_time
if [ $? -eq
0 ]; then
echo
"split $ completed..."
else
echo
-e"split $ failed!!\nplease check the file:$"
continue
fidone
#compress
cd$data_dir
gzip dns*
if [ $? -eq
0 ]; then
echo
"compress files completed..."
else
echo
"comgress files failed!!"
exit1fi
echo
"transmitting file to cluster......"
scp -r $data_dir/*.gz [email protected]:/home/hadoop/data_dir/dns/$time_now
echo
"end of transmission......"
ssh [email protected] sh load_hive_dns.sh
echo
"load data to hive database......."
這個指令碼還是比較老的,你會發現上面有乙個步驟是對檔案進行切片處理,那樣做是因為當時還不知道每個檔案有380左右的大小,後來知道這個資訊之後發現直接對檔案進行壓縮之後便可以直接入庫(hdfs中每個塊的大小設定為128m大小)。
另乙個放在hadoop集群的指令碼:
#/bin/bash
#load_hive_dns.sh
local_dir=/home/hadoop/test_818/data05/dns
time_now=`date +%y%m%d%h`
time_month=`date +%m`
time_day=`date +%d`
time_hour=`date +%h`
target_dir=$local_dir/$time_now
cd$target_dir
hive
set mapred.output.compress=true;
set mapred.output.compression.codec=org.apache.hadoop.io.compress.gzipcodec;
alter table load_test.partition_test add if not exists partition (month='$time_month',day='$time_day',hour='$time_hour');
exit;
eofhdfs dfs -put $/* /user/hive/warehouse/load_test.db/partition_test/month=$time_month/day=$time_day/hour=$time_hour
echo
"load data completed..."
**中的三個set也是多餘的,不用每次都設定,放在這裡是想提醒一下為了在入庫時不出錯,最好在執行時set一下這些引數。
這裡之所以沒有用hive進行load,是因為那樣會很慢,因為每一次load都會啟動hive,如果用hdfs的put命令,則可以省去很多時間。
保證以上指令碼自動執行的前提是兩個環境之間進行了金鑰設定。
分享知識,讓自己快樂!
oracle資料入庫
oracle資料入庫 注意 先要處理檔案中的分隔符 將資料分列 建立為標準的sql語句 1.在oracle資料庫中建立要入庫的表如果有該錶則不用建立 注 建立欄位的資料型別要符合實際邏輯 varchar2欄位最大為4000 2.把csv檔案放入到伺服器上的資料夾中 要和ctl檔案放到一塊 3.cmd...
oracle資料入庫
oracle資料入庫 注意 先要處理檔案中的分隔符 將資料分列 建立為標準的sql語句 1.在oracle資料庫中建立要入庫的表如果有該錶則不用建立 注 建立欄位的資料型別要符合實際邏輯 varchar2欄位最大為4000 2.把csv檔案放入到伺服器上的資料夾中 要和ctl檔案放到一塊 3.cmd...
15 爬蟲資料入庫
python連線mysql資料庫 資料插入操作 資料更新操作 資料查詢操作 查詢分頁實現 連線資料庫 db pymysql.connect host 127.0.0.1 user root password root database mysql connection print db 所有的操作都...