0

我是 Pig 的完整初学者。我已经安装了 cdh4 pig 并且连接到了 cdh4 集群。我们需要处理这些大量的 Web 日志文件(这些文件已经被加载到 HDFS)。不幸的是,日志语法非常复杂(不是典型的逗号分隔文件)。一个限制是我目前无法使用其他工具预处理日志文件,因为它们太大并且无法存储副本。这是日志中的原始行:

"2013-07-02 16:17:12 -0700","?c=Thing.Render&d={%22renderType%22:%22Primary%22,%22renderSource%22:%22Folio%22,%22things%22:[ {%22itemId%22:%225442f624492068b7ce7e2dd59339ef35%22,%22userItemId%22:%22873ef2080b337b57896390c9f747db4d%22,%22listId%22:%22bf5bbeaa8eae459a83fb9e2ceb99930d%22,%22ownerId%22:%222a4034e6b2e800c3ff2f128fa4f1b387%22}],%22redirectId%22:%22tgvm %22,%22sourceId%22:%226da6f959-8309-4387-84c6-a5ddc10c22dd%22,%22valid%22:false,%22pageLoadId%22:%224ada55ef-4ea9-4642-ada5-d053c45c00a4%22,%22clientTime%22 :%222013-07-02T23:18:07.243Z%22,%22clientTimeZone%22:5,%22process%22:%22ml.mobileweb.fb%22,%22c%22:%22Thing.Render%22}", "http://m.someurl.com/listthing/5442f624492068b7ce7e2dd59339ef35?rdrId=tgvm&userItemId=873ef2080b337b57896390c9f747db4d&fmlrdr=t&itemId=5442f624492068b7ce7e2dd59339ef35&subListId=bf5bbeaa8eae459a83fb9e2ceb99930d&puid=2a4034e6b2e800c3ff2f128fa4f1b387&mlrdr=t ","Mozilla/5.0 (iPhone; CPU iPhone OS 6_1_3 like Mac OS X) AppleWebKit/536.26 (KHTML, 像 Gecko) Mobile/10B329 [FBAN/FBIOS;FBAV/6.2;FBBV/228172;FBDV/iPhone4,1;FBMD/iPhone;FBSN/iPhone OS;FBSV/6.1.3;FBSS/2; FBCR/Sprint;FBID/电话;FBLC/en_US;FBOP/1]","10.nn.nn.nnn","nn.nn.nn.nn, nn.nn.0.20"

您可能注意到那里嵌入了一些 json,但它是 url 编码的。在 url 解码之后(Pig 可以做 url 解码吗?)这是 json 的样子:

{"renderType":"Primary","re​​nderSource":"Folio","things":[{"itemId":"5442f624492068b7ceowner7e2dd59339ef35","userItemId":"873ef2080b337b57896390c9f747db4d","listId":"bf5bbeae2eae4599adf""bf5bbea8eae4599adf" :"2a4034e6b2e800c3ff2f128fa4f1b387"}],"redirectId":"tgvm","sourceId":"6da6f959-8309-4387-84c6-a5ddc10c22dd","valid":false,"pageLoadId":"4ada55ef-4ea9-4642-ada5- d053c45c00a4","clientTime":"2013-07-02T23:18:07.243Z","clientTimeZone":5,"process":"ml.mobileweb.fb","c":"Thing.Render"}

我需要提取 json 中的不同字段和实际上是一个集合的“事物”字段。我还需要提取日志中的其他查询字符串值。Pig 可以直接处理这种源数据吗?如果可以,您能否指导我如何让 Pig 能够解析和加载它?

谢谢!

4

2 回答 2

1

对于如此复杂的任务,您通常需要编写 Load 函数。我推荐第 11 章。在Programming Pig中编写加载和存储函数官方文档中的加载/存储函数太简单了。

于 2013-07-04T01:35:30.947 回答
1

我做了很多实验,学到了很多东西。尝试了几个 json 库、piggybank 和 java.net.URLDecoder。甚至尝试过 CSVExcelStorage。我注册了图书馆并能够部分解决问题。当我针对更大的数据集运行测试时,它开始在源数据的某些行中遇到编码问题,从而导致异常和作业失败。所以我最终使用 Pig 的内置正则表达式功能来提取所需的值:

A = load '/var/log/live/collector_2013-07-02-0145.log' using TextLoader();
-- fix some of the encoding issues
A = foreach A GENERATE REPLACE($0,'\\\\"','"'); 
-- super basic url-decode
A = foreach A GENERATE REPLACE($0,'%22','"');

-- extract each of the fields from the embedded json
A = foreach A GENERATE 
    REGEX_EXTRACT($0,'^.*"redirectId":"([^"\\}]+).*$',1) as redirectId, 
    REGEX_EXTRACT($0,'^.*"fromUserId":"([^"\\}]+).*$',1) as fromUserId, 
    REGEX_EXTRACT($0,'^.*"userId":"([^"\\}]+).*$',1) as userId, 
    REGEX_EXTRACT($0,'^.*"listId":"([^"\\}]+).*$',1) as listId, 
    REGEX_EXTRACT($0,'^.*"c":"([^"\\}]+).*$',1) as eventType,
    REGEX_EXTRACT($0,'^.*"renderSource":"([^"\\}]+).*$',1) as renderSource,
    REGEX_EXTRACT($0,'^.*"renderType":"([^"\\}]+).*$',1) as renderType,
    REGEX_EXTRACT($0,'^.*"engageType":"([^"\\}]+).*$',1) as engageType,
    REGEX_EXTRACT($0,'^.*"clientTime":"([^"\\}]+).*$',1) as clientTime,
    REGEX_EXTRACT($0,'^.*"clientTimeZone":([^,\\}]+).*$',1) as clientTimeZone;

我决定不使用 REGEX_EXTRACT_ALL 以防字段顺序不同。

于 2013-07-05T21:08:40.863 回答