2015-03-25 30 views
0

我在HDFS上有一個目錄例如:/ user/customers,在這個目錄中我每隔3 min,我想編寫一個shell腳本來檢查這個文件夾,如果有新文件可用,那麼這個文件數據將被放入HBASE中,我已經知道我將如何將數據放入HBASE中。但我對shell腳本非常陌生,我想知道如何獲得新的文件名。如何使用shell腳本每4分鐘在HDFS(Hadoop)上查找目錄中是否有新文件

我的hadoop的命令,將數據文件的HBASE如下:

hadoop jar /opt/mapr/hbase/hbase-0.94.12/hbase-0.94.12-mapr-1310.jar importtsv -Dimporttsv.separator=, -Dimporttsv.columns=HBASE_ROW_KEY,cust:phno,cust:name,cust:memebershiptype /user/tablename customer.csv 

如今的想法是與在文件夾中最近傾倒的文件名來替換這個customer.csv文件名,然後運行這個命令。

所以,如果我沒有錯,我將需要一個cron工作來完成調度部分。但是我需要關於如何在上面提到的命令中獲得新文件名的邏輯。接下來我要學習的是每4分鐘安排一次crontab。 請指導專家。

回答

0

試試這個腳本。它會給出idea.basically首先我列出了文件並將它們存儲到customer_all_file.txt.in for循環傳遞文件名稱,將文件名稱存儲到已處理files.difference命令將查找新文件並將它們存儲到need_to_processed files.its非常簡單的通過它。

hadoop fs -ls hdfs://IPNamenode/user/customers/ | sed '1d;s/ */ /g' | cut -d\ -f8 | xargs -n 1 basename > /home/givepath/customer_all_file.txt 


diff /home/givpath/customer_all_files.txt /home/givepath/customer_processedfiles.txt > /home/givepath/need_to_process.txt 

for line in `awk '{ print $2 }' /home/givepath/need_to_process.txt`; 
do 
echo "$line" 

hadoop jar /opt/mapr/hbase/hbase-0.94.12/hbase-0.94.12-mapr-1310.jar importtsv -Dimporttsv.separator=, -Dimporttsv.columns=HBASE_ROW_KEY,cust:phno,cust:name,cust:memebershiptype /user/tablename $line 


echo "$line" >> /home/givepath/customer_already_processedfiles.txt 

done 
+0

'diff /home/givpath/customer_all_files.txt /home/givepath/customer_processedfiles.txt>/home/givepath/need_to_process.txt' 增加了一個額外的行'1d0',並且在開始處添加了字符'<' – user2960226 2015-03-26 11:00:53

+0

離開它在差異level.just更新for循環awk打印$ 2值。 awk'{print $ 2}'newfile1.txt – 2015-03-26 11:20:36

+0

給我錯誤'./find_unprocessed_files.sh:命令替換:第28行:意外的EOF查找匹配的''' ./find_unprocessed_files.sh:命令替換:第29行:語法錯誤:意外的文件結尾 ' – user2960226 2015-03-26 12:00:48

0

重新命名部分:

請問您的所有CSV文件具有相同的名稱爲customer.csv?如果是,則需要在將每個文件上傳到hdfs時對其進行重命名。

crontab的部分:

您可以通過運行shell腳本,每4分鐘:

*/4 * * * * /your/shell/script/path 

通過在終端中輸入crontab -e加入這一行。

+0

感謝您的回覆先生,我的CSV文件將有不同的名稱。例如customer1.csv,customer2.csv ..... customern.csv,在我運行hadoop命令加載HBASE上的數據後,我會用一些字母來重新命名它,以便我知道數據是否已經上傳。你能幫我找到如何找到新文件在文件夾中嗎? – user2960226 2015-03-25 11:56:03

相關問題