【问题标题】:Apache Pig - Is it possible to serialize a variable?Apache Pig - 是否可以序列化变量?
【发布时间】:2013-05-24 12:16:02
【问题描述】:

我们以 wordCount 为例:

input_lines = LOAD '/tmp/my-copy-of-all-pages-on-internet' AS (line:chararray);

-- Extract words from each line and put them into a pig bag
-- datatype, then flatten the bag to get one word on each row
bag_words = FOREACH input_lines GENERATE FLATTEN(TOKENIZE(line)) AS word;

是否可以序列化“bag_words”变量,这样我们就不必在每次执行脚本时都重新构建整个包?

谢谢。

【问题讨论】:

  • 你不能把 bag_words 写到一个文件然后再读进去吗?

标签: hadoop mapreduce bigdata apache-pig


【解决方案1】:

STORE bag_words INTO 'some-output-directory';

稍后再读入以跳过 foreach 生成、展平、标记化。

【讨论】:

  • 备注:将输出存储到directory(如果不存在)。
  • 好主意。但是接下来我应该使用什么来加载它?
  • @shanks_roux reloaded = load '/path/to/dir' as (word:chararray);
  • 你确定 "word:charrarray" 吗?如果我这样做,将无法遍历袋子元组。
  • @shanks_roux 您可以关闭 AS (word:chararray) 子句,但如果您想指定字段,则必须按数字引用字段(使用 Pig 位置符号:$0、$1、$2 等..) - 但是你失去了你创建的结构,它将整行引用为“单词”。
【解决方案2】:

您可以使用 STORE 命令在 pig 中输出任何别名:您可以使用标准格式(如 CSV)或编写自己的 PigLoader 类来实现任何特定行为。然后,您可以在单独的脚本中加载此输出,从而绕过初始加载。

【讨论】:

  • 谢谢。但我不知道如何通过存储和加载变量来保持包结构......你有什么想法吗?
猜你喜欢
  • 2011-08-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-06-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多