在猪脚本中按日期排序

时间:2016-01-12 11:14:02

标签: apache-pig

在猪0.11.0之后,引入了datetime基本变量类型进行处理。在我的情况下,我必须按日期时间订购。我用这种方式

data = LOAD 'database_name.table_name' USING org.apache.hcatalog.pig.HCatLoader() AS (id:chararray,name:chararray,birth_date_time:chararray);

selected_data = FOREACH data GENERATE id, name,ToDate(birth_date_time,'yyyy-MM-dd HH:mm:ss') AS birth_date_time;

ordered_data = ORDER selected_data BY birth_date_time DESC;

DUMP ordered_data;

但是我不行。抛出此错误

  

org.apache.pig.impl.logicalLayer.FrontendException:ERROR 1066:无法使用   打开别名cba_ordered的迭代器。后端错误:无法执行   从支持的错误重新创建异常:   AttemptID:attempt_1452577118821_0005_m_000000_3信息:错误:   org.joda.time.DateTime.compareTo(Lorg /乔达/时间/ ReadableInstant;)我           在org.apache.pig.PigServer.openIterator(PigServer.java:872)           在org.apache.pig.tools.grunt.GruntParser.processDump(GruntParser.java:774)           在org.apache.pig.tools.pigscript.parser.PigScriptParser.parse(PigScriptParser.java:372)           在org.apache.pig.tools.grunt.GruntParser.parseStopOnError(GruntParser.java:198)           在org.apache.pig.tools.grunt.GruntParser.parseStopOnError(GruntParser.java:173)           在org.apache.pig.tools.grunt.Grunt.exec(Grunt.java:84)           在org.apache.pig.Main.run(Main.java:607)           在org.apache.pig.Main.main(Main.java:156)           at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)           at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)           at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)           at java.lang.reflect.Method.invoke(Method.java:606)           在org.apache.hadoop.util.RunJar.main(RunJar.java:212)引起:org.apache.pig.backend.executionengine.ExecException:ERROR 2997:   无法从支持的错误重新创建异常:   AttemptID:attempt_1452577118821_0005_m_000000_3信息:错误:   org.joda.time.DateTime.compareTo(Lorg /乔达/时间/ ReadableInstant;)我           在org.apache.pig.backend.hadoop.executionengine.mapReduceLayer.Launcher.getErrorMessages(Launcher.java:217)           在org.apache.pig.backend.hadoop.executionengine.mapReduceLayer.Launcher.getStats(Launcher.java:151)           在org.apache.pig.backend.hadoop.executionengine.mapReduceLayer.MapReduceLauncher.launchPig(MapReduceLauncher.java:429)           在org.apache.pig.PigServer.launchPlan(PigServer.java:1324)           at org.apache.pig.PigServer.executeCompiledLogicalPlan(PigServer.java:1309)           在org.apache.pig.PigServer.storeEx(PigServer.java:980)           在org.apache.pig.PigServer.store(PigServer.java:944)           在org.apache.pig.PigServer.openIterator(PigServer.java:857)           ......还有12个

我们如何通过date_time字段订购?

1 个答案:

答案 0 :(得分:0)

我建议您使用ToUnixTime(datetime)函数来订购日期列。

请在下面查看,并按照这样做。

data = LOAD 'database_name.table_name' USING org.apache.hcatalog.pig.HCatLoader() AS (id:chararray,name:chararray,birth_date_time:chararray);

selected_data = FOREACH data { 
bdt = (datetime)ToDate(birth_date_time,'yyyy-MM-dd HH:mm:ss');
GENERATE id, name, bdt AS birth_date_time, ToUnixTime(bdt) AS birth_date_time_unix;
};

ordered_data = ORDER selected_data BY birth_date_time_unix DESC;

DUMP ordered_data;

如果有效,请告诉我。