【问题标题】:ElephantBird UDF: ORDER BY on a json field generates a syntax errorElephantBird UDF:JSON 字段上的 ORDER BY 生成语法错误
【发布时间】:2026-01-17 20:15:02
【问题描述】:

我在 Hadoop 1.2.1 上使用 Pig 0.13.0。为了处理 JSON 文件,我还使用了 ElephantBird UDF 版本 4.5。到目前为止,我对 UDF 没有什么大问题,但是当我尝试通过 json 字段排序别名时,pig 编译器不是很高兴,我收到以下错误:

Failed to parse: <file find.pig, line 13, column 35>  Syntax error, unexpected symbol at or near 'long'

脚本如下所示(查看 ORDER 语句):

...
records = LOAD '$INPUT' USING com.twitter.elephantbird.pig.load.JsonLoader('-nestedLoad') AS (json:map[]);
sorted_records = ORDER records BY (long)json#'imp' desc;
...

我尝试删除(长)但无济于事。

【问题讨论】:

    标签: apache-pig elephantbird


    【解决方案1】:

    您不能按JsonLoader 返回的映射中的值对记录进行排序,也不能在order by 中进行类型转换。您可以做的是将json#'imp' 的值投影到您记录中的一个字段,然后按以下顺序排序:

    records = FOREACH records GENERATE json, (long)json#'imp' AS imp:long;
    sortedrecords = ORDER records BY imp desc;
    

    【讨论】: