在字符串中搜索特定文本 - Hive

Search for a particular text in a string - Hive

/google/gmail/inbox
 /google/drive/map
 /google/apps
 /yahoo/news/cricket
 /yahoo/mail/
 /yahoo/sports
 /wiki/ind/jack
 /wiki/us/jil

我需要获取所需的页组。如果我使用 hive 查询搜索以 'google' 开头的页组,我需要获取前 3 行的数据。

/google/gmail/inbox
 /google/drive/map
 /google/apps

这样我需要根据页面组获取数据


我使用 like 函数搜索了字符串。

select * from table where field like '%/google/%';

这个问题有点模棱两可,但我相信您正在尝试在字符串中搜索单词 google 并在 return 中搜索包含单词 google 的行字符串.

假设您有以下 table:

create table test (val string);

它包含以下记录:

hive> select * from test;
/google/gmail/inbox
/google/drive/map
/yahoo/mail/

您可以使用以下查询 select 包含字符串 google 的行:

select val from test
where instr(val, 'google') = 2;

这给出了结果:

/google/gmail/inbox
/google/drive/map

instr给出了你搜索的字符串的位置。在这种情况下,google 的位置是 2。如果你想获取所有包含 google 的行,那么你可以使用:

select val from test
where instr(val, 'google') != 0;

您可以从 documentation.

中了解各种 Hive 字符串函数

听起来您需要页组。可能是 google,但似乎也可能是 yahoo。如果你想通过搜索引擎提取页面组,你可以使用正则表达式。您可以在 (page1|page2|...|pageN) 中放置多个网站。

Select column from table
where column rlike '.*(google|yahoo).*'

输出:

/google/gmail/inbox
/google/drive/map
/google/apps

您可能想要创建一个新列,将其命名为搜索引擎或登录页。路径中的第一个位置似乎是着陆页。您可以通过这种方式提取登录页面:

select * from
    (Select column
    , regexp_extract('^(\/[a-zA-Z]*\/)',1) as landing_page
    from table) a
  where landing page in ('google','yahoo',...,'bing')
  ;

输出:

column                   new column
/google/gmail/inbox      /google/
/google/drive/map        /google/
/google/apps             /google/
/yahoo/news/cricket      /yahoo/
/yahoo/mail/             /yahoo/
/yahoo/sports            /yahoo/
/bing/meats/delisandwich /bing/
/bing/maps/delis         /bing/

如果您不想要 /google/ 而只想 google 那么请执行:

regexp_extract('^\/([a-zA-Z]*)\/',1) as landing_page

现在我假设着陆页在您描述的路径中排在第一位。