pig对文本null的处理非常特殊。会处理成两种null,还会处理成''这样的空值。

比方,读name,age,sex日志信息。name取值处理,假设记录为“。,,”这样,会将name取值为null,假设记录为“,19。男”则name会处理为''。相同是空值。pig读取后的取值却不一样。所以一定要小心。

pig读取日志信息。遇到取值为空的字段会处理为两种,一种取值为''。还有一种为null。

详细样例:读取日志中倒数第4个字段(所有为空,两个逗号之间无值",,"),

pig读入后处理为两种值(''和 null)。





1日志中空处理为null:

(5,148,b84daa9b-194e-4c4c-9595-ce4bfabca918,605378805132617404,2014-11-05 18:31:05,2014-11-05 18:31:05,1,62052,2,,,,,,,,239.130.237.121,2,-1,,,-1,e15b6c6675c6d6e8eb7851ccc866608787daeadd,b84daa9b-194e-4c4c-9595-ce4bfabca918,02:00:00:00:00:00,-991608703440210811,,,,,75061,200,,2,2,1,7.0,,,,,,)

(5,148,b84daa9b-194e-4c4c-9595-ce4bfabca918,605378805132617404,2014-11-05 18:31:05,2014-11-05 18:31:05,2,62052,2,,,,,,,,239.130.237.121,2,-1,,,-1,e15b6c6675c6d6e8eb7851ccc866608787daeadd,b84daa9b-194e-4c4c-9595-ce4bfabca918,02:00:00:00:00:00,-991608703440210811,,,,,75061,200,,2,2,1,7.0,,,,,,)





2日志中的空处理为'':

(3,90,864616028213476,1412364855586,2014-08-25 15:07:42,,1,14999,2,,,,,,460,00,112.5.236.229,2,864616028213476,3ff1c154fb35073a,,864616028213476|3ff1c154fb35073a,,,,864616028213476|3ff1c154fb35073a,,,,,311,35,-1,,1,3,2.x,1,91,,35.0,105.0,132012121230123)

(5,148,ddeb5f0f-09a7-456e-a9dc-5fb5e96c5453,682937329735483418,2014-11-04 20:08:37,2014-11-04 20:08:37,1,62052,2,,,,,,,,160.35.136.117,1,-1,,,-1,e72da4be06382bd0826be09927f650ca2570add9,ddeb5f0f-09a7-456e-a9dc-5fb5e96c5453,02:00:00:00:00:00,-3733654770696849299,,,,,66454,206,,2,2,1,7.1,,,,38.878998,-76.9898,032010032322002)

(3,90,864616028213476,1412364855586,2014-08-25 15:07:42,,2,14999,2,,,,,,460,00,112.5.236.229,2,864616028213476,3ff1c154fb35073a,,864616028213476|3ff1c154fb35073a,,,,864616028213476|3ff1c154fb35073a,,,,,311,35,-1,,1,3,2.x,1,91,,35.0,105.0,132012121230123)

(5,148,ddeb5f0f-09a7-456e-a9dc-5fb5e96c5453,682937329735483418,2014-11-04 20:08:37,2014-11-04 20:08:37,2,62052,2,,,,,,,,160.35.136.117,1,-1,,,-1,e72da4be06382bd0826be09927f650ca2570add9,ddeb5f0f-09a7-456e-a9dc-5fb5e96c5453,02:00:00:00:00:00,-3733654770696849299,,,,,66454,206,,2,2,1,7.1,,,,38.878998,-76.9898,032010032322002)





处理代码例如以下:

--citylevel report analysis:pig -p date=2014-07-30 -p year=2014 -p file_path=/user/wizad/test -f

SET job.name 'test_citylevel_reporth_istorical';

SET job.priority HIGH;





--REGISTER piggybank.jar;

REGISTER wizad-etl-udf-0.1.jar;





--DEFINE SequenceFileLoader org.apache.pig.piggybank.storage.SequenceFileLoader();

DEFINE SequenceFileLoader com.XXX.xxx.etl.pig.SequenceFileCSVLoader();









%default Cleaned_Log /user/wizad/test/wizad/cleaned/2014-10*/*/part*





%default AD_Data /user/wizad/data/wizad/metadata/ad/part*

%default Campaign_Data /user/wizad/data/wizad/metadata/campaign/part*

%default Region_Template /user/wizad/data/wizad/metadata/region_template/part-m-00000

%default Addtion_Data /user/wizad/data/report/region_addition/addition_data.txt





%default Industry_Path $file_path/report/historical/citylevel/$year/industry

%default Industry_Path $file_path/report/historical/citylevel/$year/industry

%default Industry_SUM $file_path/report/historical/citylevel/$year/industry_sum

%default Industry_TMP $file_path/report/historical/citylevel/$year/industry_tmp





%default Industry_Brand_Path $file_path/report/historical/citylevel/$year/industry_brand

%default Industry_Brand_SUM $file_path/report/historical/citylevel/$year/industry_brand_sum

%default Industry_Brand_TMP $file_path/report/historical/citylevel/$year/industry_brand_tmp





%default ALL_Path $file_path/report/historical/citylevel/$year/all

%default ALL_SUM $file_path/report/historical/citylevel/$year/all_sum

%default ALL_TMP $file_path/report/historical/citylevel/$year/all_tmp





%default output_path /user/wizad/tmp/result









--origin_cleaned_data = LOAD '$Cleaned_Log' USING PigStorage(',')

origin_cleaned_data = LOAD '$Cleaned_Log' USING SequenceFileLoader 

AS (ad_network_id:chararray,

    wizad_ad_id:chararray,

    guid:chararray,

    id:chararray,

    create_time:chararray,

    action_time:chararray,

    log_type:chararray, 

    ad_id:chararray,

    positioning_method:chararray,

    location_accuracy:chararray,

    lat:chararray, 

    lon:chararray,

    cell_id:chararray,

    lac:chararray,

    mcc:chararray,

    mnc:chararray,

    ip:chararray,

    connection_type:chararray,

    imei:chararray,

    android_id:chararray,

    android_advertising_id:chararray,

    udid:chararray,

    openudid:chararray,

    idfa:chararray,

    mac_address:chararray,

    uid:chararray,

    density:chararray,

    screen_height:chararray,

    screen_width:chararray,

    user_agent:chararray,

    app_id:chararray,

    app_category_id:chararray,

    device_model_id:chararray,

    carrier_id:chararray,

    os_id:chararray,

    device_type:chararray,

    os_version:chararray,

    country_region_id:chararray,

    province_region_id:chararray,

    city_region_id:chararray,

    ip_lat:chararray,

    ip_lon:chararray,

    quadkey:chararray);





my_test1 = filter origin_cleaned_data by guid == 'b84daa9b-194e-4c4c-9595-ce4bfabca918';

dump my_test1;

describe my_test1;

--store my_test into '$output_path/mytest' using PigStorage(',');





my_test2 = filter origin_cleaned_data by guid == '864616028213476' or guid == 'ddeb5f0f-09a7-456e-a9dc-5fb5e96c5453';

dump my_test2;

describe my_test2;

--store my_test into '$output_path/mytest' using PigStorage(',');





--将第2种空取值''过滤为unknown

unknown_data = FOREACH origin_cleaned_data GENERATE wizad_ad_id,guid,log_type,

((city_region_id == '') ? 'unknown' : city_region_id) AS city_region_id;  --(wizad_ad_id,guid,log_type,city_region_id)

--将第1种空取值null过滤为isnull

null_data =  FOREACH origin_cleaned_data GENERATE wizad_ad_id,guid,log_type,

((city_region_id is NULL) ? 'isnull' : city_region_id) AS city_region_id;  --(wizad_ad_id,guid,log_type,city_region_id)





--看看unknown和isnull的数据

all_unknown = filter unknown_data by city_region_id == 'unknown';

dump all_unknown;

--store all_unknown into '$output_path/unknown' using PigStorage(',');

all_null = filter null_data by city_region_id == 'isnull';

dump all_null;

--store all_null into '$output_path/isnull' using PigStorage(',');





--把两种都过滤为no_use

origin_historical = FOREACH origin_cleaned_data GENERATE wizad_ad_id,guid,log_type,

((city_region_id == '') or (city_region_id == null) or (city_region_id is null) ?

'no_use' : city_region_id) AS city_region_id;  --(wizad_ad_id,guid,log_type,city_region_id)

dump origin_historical;

describe origin_historical;









两种数据分别的结果例如以下:

unknown数据:

(90,864616028213476,1,unknown)

(90,862624024878336,1,unknown)

(90,990001402489819,1,unknown)

(90,862343020727070,1,unknown)

(201,1ff90f55-f5cd-4b2a-9357-5bde0e3ff526,1,unknown)

(201,c3916c92-a70c-4d34-babd-d3fc021cf642,1,unknown)

(201,00:c6:10:dd:81:17,1,unknown)

(201,88:53:95:da:9e:03,1,unknown)

......





而null数据:

(148,b84daa9b-194e-4c4c-9595-ce4bfabca918,1,isnull)

(148,13fbe940-7cd0-44a1-b637-a0df8ea83621,1,isnull)

(148,b84daa9b-194e-4c4c-9595-ce4bfabca918,2,isnull)

(148,13fbe940-7cd0-44a1-b637-a0df8ea83621,2,isnull)

看了一些资料,pig对null处理的一些总结,部分引自网络:

Pig数据流的语言。擅长于处理纯文本信息,在模式定义方面提供了更大的灵活性,包含数据类型的选择。这和传统的关系型数据库是有本质差别的。(传统的RDBMS必需要事先定义严格的表结构)

缺点,太过灵活。

对文本为空时。处理也比較特殊。

假设Pig的函数有4种类型:计算函数(Evalfunction)比方MAX。筛选函数(Filterfunction)如ISEMPTY。载入函数(loadfunction)。存储函数(stroefunction)如PigStorage函数。

这些函数对空值有着特殊处理:



1,首先载入时(载入/存储函数:PigStorage, BinStorage, BinaryStorage, TextLoader, PigDump),传统的关系数据库写入一个类型不符的数据到数据库。比方字符‘a’写到定义为int的字段。会报错。

Pig不同,如load时。字符‘a’传到在模式定义中int字段。pig会用空值(null)代替之,同一时候会输出提示信息,但不终止语句运行。本质是大数据集有一定比例的损坏数据,是一种常见情况。此时我们能够通过例如以下语句进行筛选:

corrupt_records= FILTER records BY temperature is not null;

或者用split语句:

Splitrecords into good_one if temperature is not null, bad_one is temperature is null;

2,其它函数遇到空值的特殊处理。pig函数空值的殊处理:

比較操作(==。!=,,<,=,<=)。matches。算数操作(+,-,*,/ ,包括%。?,CASE)中,假设有一个操作数为空,那么结果为空。

CAST 操作:将一个null数据从一个数据类型转换到还有一个数据类型。结果为空

AVG。MIN,MAX。SUM。COUNT :这几个操作将忽略空值

SIZE :随意计算的对象为null。结果也为null

CONCAT :随意一个字表达式为空,结果为空

tuple(.) or map(#):假设 被引用的对象为空,那么结果为空 。

FILTER 操作:filter的表达式为空,不会拒绝操作。

如b = filter a by X!=5 ,假设X为空,。X也为空,X!=5 将为空,那么filter将不会处理这一行数据。

 

三元操作符 ? :假设一个bool表达式的结果为null,结果将为空。

null == 2 ? 1 : 4 -- returns null

   2 == 2 ? 1 : 'fred' -- type error; both values must be of the same type



COUNT_STAR ,只是滤null数据

下面操作会产生null :

1、除0

2、用户的UDFs

3、引用一个不存在的字段

4、引用一个map中不存在的字段

5、引用一个tuple中不存在的字段

6、load不存在的数据时产生null。空字符串不会被load。会被替换成null null能够作为一个常量使用 。

7、load时数据类型不匹配产生null

GROUP /COGROUP/JOIN: 用group处理一个关系时,一个关系中的null会被聚集在一起当做一个null处理 。

当cogroup来处理多个关系是,假设有key为空的情况时,多个关系之间的的空是不一样的。会被分别当做不同的null key来处理。

如数据 :

a :

1 5 4

3 6

b :

1 7

2 8

  10

JOIN【inner】如:join中空和空是匹配不上的,会被过滤掉 。在join之前过滤出key为空的数据 ,有助于提高join的速度。 a = load './t1.txt' as (a1:int,a2:int); b = load './t2.txt' as (b1:int,b2:int); c= join a by a1,b by b1 ; dump c ; (1,5,1,7)

JOIN【outer】 d = join a by a1 left,b by b1 ; dump d ; (1,5,1,7)

(3,6,,)

(,4,,) 

d = join a by a1 right,b by b1; dump d ; (1,5,1,7)

(,,2,8)

(,,,10) 

d = join a by a1 full,b by b1; dump d ; (1,5,1,7)

(,,2,8)

(3,6,,)

(,4,,)

(,,,10)

pig对null的处理(实际,对空文本处理为两种取值null或‘’)的更多相关文章

  1. 由多线程引起的map取值为null的分析

    昨天写了一个多线程的程序,却发现了一个很奇特的问题,就是我的map对象明明put了,可是get的时候竟然会取到null,而且尝试多次,有时候成功,有时候取到null,并不确定. 程序代码如下: pub ...

  2. 数据库插入数据失败,log提示不能将值 NULL 插入列 'id'

    已经记不住具体的log信息了,意思就是ID如果没有设置为自增长的情况下就不能插入数据,而建表时ID字段是设置为"not null",所以就不能顺利插入数据. 解决方法有两种: ①建 ...

  3. 运算符关键字。数据区别大小写。日期范围。判空的两种写法。NOT IN的两种写法。IN范围可含NULL,但NOT IN值范围不能含NULL。

    比较:>,<,=,>=,<=,<>(!=) 逻辑:AND,OR,NOT 范围:BETWEEN...AND... 范围:IN,NOT IN 判空:IS NULL, I ...

  4. Spring @Value取值为null或@Autowired注入失败

    @Value 用于注入.properties文件中定义的内容 @Autowired 用于装配bean 用法都很简单,很直接,但是稍不注意就会出错.下面就来说说我遇到的问题. 前两天在项目中遇到了一个问 ...

  5. 【XAF问题】不能将值NULL插入列"Oid"

    一.问题 1. 不能将值NULL插入列"Oid" 二.解决方法 解决方法:删表,oid不能为空,继承的对象变了

  6. c#:无法将 NULL 转换成“System.DateTime”,因为它是一种值类型

    摘自:http://www.blogjava.net/parable-myth/archive/2010/09/30/333454.html 在C# 2.0里面的数据类型中,分为值类型和引用类型,引用 ...

  7. 创建SQL作业错误的解决方法(不能将值 NULL 插入列 'owner_sid',表 'msdb.dbo.sysjobs';列不允许有空值。)

    在用SQL语句创建SQL Server作业时有时出现如下错误: 消息 515,级别 16,状态 2,过程 sp_add_job,第 137 行 不能将值 NULL 插入列 'owner_sid',表  ...

  8. 让索引包含null值的两种方法

    1. 把有NULL值的列与一个常数,或者一个带有not null约束的列一同索引 create index ind_01 on t01(col01,1); 或者 create index ind_01 ...

  9. c#:无法将 NULL 转换成“System.DateTime”,因为它是一种值类型(转)

    摘自:http://www.blogjava.net/parable-myth/archive/2010/09/30/333454.html 在C# 2.0里面的数据类型中,分为值类型和引用类型,引用 ...

随机推荐

  1. android 发送短信 怎样做到一条一条的发送,仅仅有在上一条发送成功之后才发送下一条短信

    android发送短信截获上一条发送是否成功,然后再来发送下一条短信 1.问题:在项目中遇到例如以下要求:待发短信有N条,实现一条一条的发送并在上一条短信发送成功之后再来发送下一条. for(int ...

  2. Search Insert Position--寻找插入点Given a sorted array and a target value, return the index if the target

    问题:链接 Given a sorted array and a target value, return the index if the target is found. If not, retu ...

  3. 架构设计的UML图形思考

    本篇紧接着上一篇   基本OOP知识  ,介绍高焕堂老师的第二讲. 架构设计的UML图形思考.本篇最重要的是三个词语:图形.思考.UML. 架构师的作用体现主要在项目开发前期.在整个项目还没有完毕的时 ...

  4. Adobe Acrobat Ⅺ Pro安装激活

    1.注意一定要断网安装,如果你有防火墙拦截亦可(注意:系统自带那防火墙不行). 2.将AcrobatPro_11_Web_WWMUI.exe解压到一个目录下,找到目录下的setup.exe安装,安装时 ...

  5. Web 应用程序项目 XXXX 已配置为使用 IIS。 无法访问 IIS 元数据库。您没有足够的特权访问计算机上的 IIS 网站。(转载)

    Web 应用程序项目 XXXX 已配置为使用 IIS. 无法访问 IIS 元数据库.您没有足够的特权访问计算机上的 IIS 网站. 2012年05月19日 ⁄ 综合 ⁄ 共 261字 ⁄ 字号 小 中 ...

  6. uart串口协议

      uart串口协议   /* USART Word Length ---------------------------------------------------------*/     US ...

  7. Ibatis调用存储过程实现增删改以及分页查询

    1.Ibatis实现增删改操作很简单了,通常我是将某一模块的增删改功能写在一个存储过程里,通过一个标识符去区分执行增加还是修改抑或删除操作. statement: <!-- 存储过程:实现学生的 ...

  8. [转]给Linux系统管理员准备的Nmap命令的29个实用范例+ tsysv 系统服务器管理器

    原文链接:http://os.51cto.com/art/201401/428152.htm Nmap即网络映射器对Linux系统/网络管理员来说是一个开源且非常通用的工具.Nmap用于在远程机器上探 ...

  9. 如何搭建Visual Studio的内核编程开发环境

    最近正在看<寒江独钓——Windows内核安全编程>这本书,感觉这本书非常好,有兴趣的朋友可以买来看看,有关这本书的信息请参考:http://www.china-pub.com/19559 ...

  10. 【项目分析】利用C#改写JAVA中的Base64.DecodeBase64以及Inflater解码

    原文:[项目分析]利用C#改写JAVA中的Base64.DecodeBase64以及Inflater解码 最近正在进行项目服务的移植工作,即将JAVA服务的程序移植到DotNet平台中. 在JAVA程 ...