今天通过不断的尝试,终于知道这个flatten的用法了。其实吧,有时候关键是要test,才能充分理解解说。不过,同事给说的有点问题,误导了我。整的我一直没明白怎么回事。

这是官方的解释:

The FLATTEN operator looks like a UDF syntactically, but it is actually an operator that changes the structure of tuples and bags in a way that a UDF cannot. Flatten un-nests tuples as well as bags. The idea is the same, but the operation and result is different
for each type of structure.

For tuples, flatten substitutes the fields of a tuple in place of the tuple. For example, consider a relation that has a tuple of the form (a, (b, c)). The expression GENERATE $0, flatten($1), will cause that tuple to become (a, b, c).

For bags, the situation becomes more complicated. When we un-nest a bag, we create new tuples. If we have a relation that is made up of tuples of the form ({(b,c),(d,e)}) and we apply GENERATE flatten($0), we end up with two tuples (b,c) and (d,e). When we
remove a level of nesting in a bag, sometimes we cause a cross product to happen. For example, consider a relation that has a tuple of the form (a, {(b,c), (d,e)}), commonly produced by the GROUP operator. If we apply the expression GENERATE $0, flatten($1)
to this tuple, we will create new tuples: (a, b, c) and (a, d, e).

我试验下来也是这样的,我今天把第一种和第二种情况都尝试了,实验证明,即使是第二种,其实一次flatten就够了,就得到schema了。这样的数据,

Joe {(Joe,18,3.8)}

Bill {(Bill,20,3.9)}

John {(John,18,4.0)}

Mary {(Mary,19,3.8),(Mary,19,5.0)}

a = load 'result' as (f1:chararray,B: bag {T: tuple(t1:chararray, t2:int, t3:float)});

b = foreach a GENERATE FLATTEN(B) as (t1:chararray,t2:int,t3:float);

这个是可以一次性flatten的。但是更高的复杂度我每测试,应该是需要两次这种操作的吧。真是真是对bag, tuple也长了见识了。明天看看能否把数据传输到UDF中操作。

总结一句话,在不确定时要首先看官方文档,然后就先拿小数据测试一下,看看每一步得到的是什么结构describe,同时store后看看是什么结果,是否和自己想的一样。整体来说还是很清晰的。

pig flatten的更多相关文章

  1. Pig Flatten 解包操作,解元组

    Flatten Operator The FLATTEN operator looks like a UDF syntactically, but it is actually an operator ...

  2. 【Pig源码分析】谈谈Pig的数据模型

    1. 数据模型 Schema Pig Latin表达式操作的是relation,FILTER.FOREACH.GROUP.SPLIT等关系操作符所操作的relation就是bag,bag为tuple的 ...

  3. Pig + Ansj 统计中文文本词频

    最近特别喜欢用Pig,拥有能满足大部分需求的内置函数(built-in functions),支持自定义函数(user defined functions, UDF),能load 纯文本.avro等格 ...

  4. Hadoop:pig 安装及入门示例

    pig是hadoop的一个子项目,用于简化MapReduce的开发工作,可以用更人性化的脚本方式分析数据. 一.安装 a) 下载 从官网http://pig.apache.org下载最新版本(目前是0 ...

  5. Pig用户自定义函数(UDF)转

    原文地址:http://blog.csdn.net/zythy/article/details/18326693 我们以气温统计和词频统计为例,讲解以下三种用户自定义函数. 用户自定义函数 什么时候需 ...

  6. pig 介绍与pig版 hello world

    前两天使用pig做ETL,粗浅的看了一下,没有系统地学习,感觉pig还是值得学习的,故又重新看programming pig. 以下是看的第一章的笔记: What is pig? Pig provid ...

  7. xml in hadoop ETL with pig summary

    项目中需要把source为xml的文件通过flume放置到hdfs,然后通过MR导入到vertica中去,我之前做过简单的 尝试,是通过pig的piggybank的xmlloader然后Regex_e ...

  8. 使用Pig预测电信用户的移动路径

    实战数据: 预期结果: 测试数据: 002|2014-09-10 00-09|东油大学 002|2014-09-10 09-17|学苑小区 001|2014-09-12 00-09|东油大学 001| ...

  9. 2014-08-05 pig

    Pig的数据类型能够分为两种:一种是scalar类型,包含单一的value,一种是complex类型,包含有其他的类型. 对于scalar类型: int,long,float,double,chara ...

随机推荐

  1. flex TweenLite

    本贴已在 AS天地会转发,大家可以参考:http://bbs.actionscript3.cn/viewthread.php?tid=11090&pid=91142&page=1&am ...

  2. Android系统OTA升级包制作【转】

    本文转载自:http://blog.csdn.net/dingfengnupt88/article/details/52882788 Android系统升级分为整包升级和差分包升级,整包升级就是将系统 ...

  3. 简化Hadoop命令

    1. 安装客户端(通过端用户可以方便的和集群交互) 2. 简化Hadoop命令 修改~/.bashrcalias hadoop='/home/work/hadoop/client/hadoop-cli ...

  4. WebSocket实现Web聊天室

    一.客户端: JS代码如下: /* * 这部分js将websocket封装起来 */ var websocket = null; //判断当前浏览器是否支持WebSocket if ('WebSock ...

  5. codeforces 676B 模拟 递推

    题意:每秒从最高处的杯子倒一杯酒下来,酒流的方式如图,问t秒装满酒的杯子的数目. 思路:把第一杯的值设为t,glass[i][j]=(glass[i-1][j-1]-1)/2+(glass[i-1][ ...

  6. vue backup

    为了解决跨域问题 目前在项目,目录下创建了vue.config.js文件,目前是指向别外的一个端口,之前是记得在express后端进行端口指定的,先备注一下,后期跟进 npm ls | grep ax ...

  7. QuickReport FastReport

    一.QuickReport1.安装Component->Install packages->X:/Program Files/Borland/Delphi7/Bin/dclqrt70.bp ...

  8. EF学习 开始操作02

    历史版本和未来版本 本文介绍有关实体框架 (EF5) 最新版本的信息,其中大部分内容也适用于旧版本.有关完整版本列表以及各版本引入的功能的详细信息,请参阅 “版本历史”. “历史版本”页面包含实体框架 ...

  9. CNN检测模型统计检出率

    X, y = get_feature_charseq() #max_document_length=64 volcab_file = "volcab.pkl" assert os. ...

  10. JS+css3焦点轮播图PC端

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...