在 HDFS 的文件中搜索字符串或数字范围

Search for a string or number range in a file in HDFS

我想在 HDFS 中搜索并列出完全包含我的搜索字符串的文件, 我的第二个要求是是否有任何可能的方法来搜索文件 HDFS 中的一系列值。

假设下面是我的文件,它包含以下数据

/user/hadoop/test.txt

101,abc
102,定义
103,ghi
104,aaa
105,bbb

是否有任何可能的方法来搜索范围 [101-104] 以便 returns 包含以下数据范围的文件。

.

显示具有模式的文件名。让我们循环遍历包含文件的 hdfs 目录。

hdfs_files=`hdfs dfs -ls /user/hadoop/|awk '{print }'`
for file in `echo $hdfs_files`;
 do
  patterns=`hdfs dfs -cat $file|egrep -o "10[1-4]"`
  patterns_count=`echo $patterns|tr ' ' "\n"|wc -l`
   if [ $patterns_count -eq 4 ]; then 
    echo $file;
   fi
 done

现在解决第二个要求“在文件 HDFS 中搜索一系列值”使用 shell 命令:-

hdfs dfs -cat /user/hadoop/test.txt|egrep "10[1-4]"

输出:-

101,abc
102,def
103,ghi
104,aaa

或者只匹配第一列

hdfs dfs -cat /user/hadoop/test.txt|egrep -o "10[1-4]"

输出:-

101
102
103
104