1. 程式人生 > >hadoop中查詢某個字串所在的hdfs位置

hadoop中查詢某個字串所在的hdfs位置

在/home/test/2017-08-23這個目錄中查詢包含0001a794d86f0844的檔案

1、shell for迴圈

適用於hdfs容量比較小的的

for file in `hadoop fs -ls /home/test/2017-08-23 |awk '{print $NF}'`
do echo $file
hadoop fs -text $file |fgrep "0001a794d86f0844" --color 
done 

2、藉助hive中的虛擬列

適用於查詢比較大的檔案,藉助於mr

建立臨時表,只有一列。如:

create external table temp_find_table (line string ) 
ROW
FORMAT DELIMITED FIELDS TERMINATED BY '\\U0000' location '/home/test/2017-08-23' ;
select line,INPUT__FILE__NAME ,BLOCK__OFFSET__INSIDE__FILE from temp_find_table where line like '%0001a794d86f0844%' ;

其中第2列是記錄所在檔案位置,第3列是記錄在檔案中的偏移量。