pig flatten
今天通过不断的尝试,终于知道这个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的更多相关文章
- Pig Flatten 解包操作,解元组
Flatten Operator The FLATTEN operator looks like a UDF syntactically, but it is actually an operator ...
- 【Pig源码分析】谈谈Pig的数据模型
1. 数据模型 Schema Pig Latin表达式操作的是relation,FILTER.FOREACH.GROUP.SPLIT等关系操作符所操作的relation就是bag,bag为tuple的 ...
- Pig + Ansj 统计中文文本词频
最近特别喜欢用Pig,拥有能满足大部分需求的内置函数(built-in functions),支持自定义函数(user defined functions, UDF),能load 纯文本.avro等格 ...
- Hadoop:pig 安装及入门示例
pig是hadoop的一个子项目,用于简化MapReduce的开发工作,可以用更人性化的脚本方式分析数据. 一.安装 a) 下载 从官网http://pig.apache.org下载最新版本(目前是0 ...
- Pig用户自定义函数(UDF)转
原文地址:http://blog.csdn.net/zythy/article/details/18326693 我们以气温统计和词频统计为例,讲解以下三种用户自定义函数. 用户自定义函数 什么时候需 ...
- pig 介绍与pig版 hello world
前两天使用pig做ETL,粗浅的看了一下,没有系统地学习,感觉pig还是值得学习的,故又重新看programming pig. 以下是看的第一章的笔记: What is pig? Pig provid ...
- xml in hadoop ETL with pig summary
项目中需要把source为xml的文件通过flume放置到hdfs,然后通过MR导入到vertica中去,我之前做过简单的 尝试,是通过pig的piggybank的xmlloader然后Regex_e ...
- 使用Pig预测电信用户的移动路径
实战数据: 预期结果: 测试数据: 002|2014-09-10 00-09|东油大学 002|2014-09-10 09-17|学苑小区 001|2014-09-12 00-09|东油大学 001| ...
- 2014-08-05 pig
Pig的数据类型能够分为两种:一种是scalar类型,包含单一的value,一种是complex类型,包含有其他的类型. 对于scalar类型: int,long,float,double,chara ...
随机推荐
- python cookbook 数据结构
保留最后n个元素: from collections import deque def search (lines, pattern, history=): previous_lines = dequ ...
- springboot5
1.改造购物车系统 1.1.创建购物车的Spring Boot工程 1.1.导入依赖 <project xmlns="http://maven.apache.org/POM/4.0.0 ...
- nodejs模块之http&&url
我们使用nodejs中的http模块来进行网络操作 一.什么是HTTP协议: 超文本传输协议(HyperText Transfer Protocol)HTTP假定其下层协议提供可靠传输. 因此,任何能 ...
- Python运算和和表达式 学习笔记
光荣之路Python公开课第二讲 Python运算符和表达式. 一 Python运算符 Python运算符包括 算术运算符,赋值运算符,位运算符,逻辑运算符,身份运算符,成员运算符. 1. 算术运算符 ...
- FHQ_treap
上个月还在舔\(splay\):\(FHQ-treap\)太好打了吧真香 前言 还是建议先把\(splay\)学好再看,讲得会比较粗略(但该有的不会少),或者左转其他文章 \(FHQ-treap\)是 ...
- hd acm1005
问题: 已知递推公式:f[i] = ( a * f[i-1] + b * f[i-2] ) % 7,f[1]=1,f[2]=1. 需要你输入三个数a,b,n.其中a,b用来补充上述公式,用补充后的公式 ...
- Vim的map
linux系统下.vimrc文件(这个文件可以在家目录新建): 这个文件记录着vim的配置信息: 如: "显示行号 set number "键映射map “如按F5,在word的前 ...
- java深入探究07-jdbc下
1.BeanUtils组件 1)使用:导入commons-beanutils-1.8.3.jar核心包,日志支持包: commons-logging-1.1.3.jar 缺少日志的jar文件报错:ja ...
- MySQL- SQL UNION 和 UNION ALL 操作符
在数据库查询中我们常常遇到这样一种情况,想把两个子查询的结果合并在一起变成一条 sql 去执行而不是多个sql分次执行.只是后我们就可以使用 UNION 和 UNION ALL 操作符来操作了. SQ ...
- 基于js的网页换肤(不需要刷新整个页面,只需替换css文件)
1. [代码][JS]代码 <HTML><HEAD><link ID="skin" rel="stylesheet" typ ...