I have records grouped by ID1 and ID2 such as the following:
ID1 ID2 date_time
Apple pear 2020-03-09T12:11:25:622Z
Apple pear 2020-03-09T12:23:36:825Z
Apple lemon 2020-03-08T08:01:16:030Z
Apple lemon 2020-03-09T10:11:12:930Z
Apple lemon 2020-03-09T15:13:02:610Z
Lime peach 2020-03-09T07:34:06:825Z
Lime peach 2020-03-09T07:54:12:220Z
Melon banana 2020-03-09T03:54:11:041Z
Melon banana 2020-03-09T09:22:10:220Z
Orange pear 2020-03-09T11:13:36:217Z
Orange pear 2020-03-09T11:23:26:040Z
Orange pear 2020-03-09T11:43:35:721Z
I'm trying to extract records that has a maximum record passing a time frame, such that if I want to extract records with max time no greater than noon on March 9th, the above records will be subsetted to:
ID1 ID2 date_time
Lime peach 2020-03-09T07:34:06:825Z
Lime peach 2020-03-09T07:54:12:220Z
Melon banana 2020-03-09T03:54:11:041Z
Melon banana 2020-03-09T09:22:10:220Z
Orange pear 2020-03-09T11:13:36:217Z
Orange pear 2020-03-09T11:23:26:040Z
Orange pear 2020-03-09T11:43:35:721Z
I used cast(regexp_replace('2020-03-09T16:05:06:827Z','(.*?)T(.*?):([^:]*?)Z$','$1 $2\\.$3') as timestamp)
to convert the date_time to timestamp thanks to @leftjoin.
Appreciate for any help.
Added vertex issue occurred after sub-setting by time:
Note: I'm only allowed to make temporary table.
First: I dedup the raw dataset by ID1 and ID2
create temporary table data1 as
select id1, id2, name, value,
cast(regexp_replace(oldtime,'(.*?)T(.*?):([^:]*?)Z$','$1 $2\\.$3') as timestamp) as date_time
from (
select ID1, ID2,
ROW_NUMBER() OVER (PARTITION BY id1, id2,name, value ORDER BY value) as rn,
case when name like '%_TIME' then value end as date_time
from df.data1
)undup
where undup.rn = 1 AND
value <> '' and value is not null
order by ID1, ID2, date_time ASC
then I subset by time:
create temporary table data2 as
select *
from(
select *,
max(date_time) over (partition by id1,id2) as max_date
from data1
) s
where max_date <= '2020-03-12 12:00:00'
After this I get a vertex error..
ERROR: Execute error: Error while processing statement: FAILED: Execution Error, return code 2 from
org.apache.hadoop.hive.ql.exec.tez.TezTask. Vertex failed, vertexName=Reducer 2, vertexId=vertex_1583806821840_6890_2_01,
diagnostics=[Task failed, taskId=task_1583806821840_6890_2_01_000036, diagnostics=[TaskAttempt 0 failed, info=[Error: Error
while running task ( failure ) : attempt_1583806821840_6890_2_01_000036_0:java.lang.RuntimeException:
java.lang.RuntimeException: org.apache.hadoop.hive.ql.metadata.HiveException: Hive Runtime Error while processing row at
org.apache.hadoop.hive.ql.exec.tez.TezProcessor.initializeAndRunProcessor(TezProcessor.java:296) at
org.apache.hadoop.hive.ql.exec.tez.TezProcessor.run(TezProcessor.java:250) at
org.apache.tez.runtime.LogicalIOProcessorRuntimeTask.run(LogicalIOProcessorRuntimeTask.java:374) at
org.apache.tez.runtime.task.TaskRunner2Callable$1.run(TaskRunner2Callable.java:73) at
org.apache.tez.runtime.task.TaskRunner2Callable$1.run(TaskRunner2Callable.java:61) at
java.security.AccessController.doPrivileged(Native Method) at javax.security.auth.Subject.doAs(Subject.java:422) at
org.apache.hadoop.security.UserGroupInformation.doAs(UserGroupInformation.java:1730) at
org.apache.tez.runtime.task.TaskRunner2Callable.callInternal(TaskRunner2Callable.java:61) at
org.apache.tez.runtime.task.TaskRunner2Callable.callInternal(TaskRunner2Callable.java:37) at
org.apache.tez.common.CallableWithNdc.call(CallableWithNdc.java:36) at
com.google.common.util.concurrent.TrustedListenableFutureTask$TrustedFutureInterruptibleTask.runInterruptibly(TrustedListenab
leFutureTask.java:108) at com.google.common.util.concurrent.InterruptibleTask.run(InterruptibleTask.java:41) at
com.google.common.util.concurrent.TrustedListenableFutureTask.run(TrustedListenableFutureTask.java:77) at
java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149) at
java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624) at
java.lang.Thread.run(Thread.java:748)Caused by: java.lang.RuntimeException:
org.apache.hadoop.hive.ql.metadata.HiveException: Hive Runtime Error while processing row at
org.apache.hadoop.hive.ql.exec.tez.ReduceRecordSource.pushRecord(ReduceRecordSource.java:304) at
org.apache.hadoop.hive.ql.exec.tez.ReduceRecordProcessor.run(ReduceRecordProcessor.java:318) at
org.apache.hadoop.hive.ql.exec.tez.TezProcessor.initializeAndRunProcessor(TezProcessor.java:267) ... 16 moreCaused by:
org.apache.hadoop.hive.ql.metadata.HiveException: Hive Runtime Error while processing row at
org.apache.hadoop.hive.ql.exec.tez.ReduceRecordSource$GroupIterator.next(ReduceRecordSource.java:378) at
org.apache.hadoop.hive.ql.exec.tez.ReduceRecordSource.pushRecord(ReduceRecordSource.java:294) ... 18 moreCaused by:
java.lang.NullPointerException at
org.apache.hadoop.hive.ql.exec.persistence.PTFRowContainer.first(PTFRowContainer.java:115) at
org.apache.hadoop.hive.ql.exec.PTFPartition.iterator(PTFPartition.java:114) at
org.apache.hadoop.hive.ql.udf.ptf.BasePartitionEvaluator.getPartitionAgg(BasePartitionEvaluator.java:200) at
org.apache.hadoop.hive.ql.udf.ptf.WindowingTableFunction.evaluateFunctionOnPartition(WindowingTableFunction.java:155) at
org.apache.hadoop.hive.ql.udf.ptf.WindowingTableFunction.iterator(WindowingTableFunction.java:538) at
org.apache.hadoop.hive.ql.exec.PTFOperator$PTFInvocation.finishPartition(PTFOperator.java:349) at
org.apache.hadoop.hive.ql.exec.PTFOperator.process(PTFOperator.java:123) at
org.apache.hadoop.hive.ql.exec.Operator.baseForward(Operator.java:994) at
org.apache.hadoop.hive.ql.exec.Operator.forward(Operator.java:940) at
org.apache.hadoop.hive.ql.exec.Operator.forward(Operator.java:927) at
org.apache.hadoop.hive.ql.exec.SelectOperator.process(SelectOperator.java:95) at
org.apache.hadoop.hive.ql.exec.tez.ReduceRecordSource$GroupIterator.next(ReduceRecordSource.java:363) ... 19 more],
TaskAttempt 1 failed, info=[Error: Error while running task ( failure ) :
attempt_1583806821840_6890_2_01_000036_1:java.lang.RuntimeException: java.lang.RuntimeException:
org.apache.hadoop.hive.ql.metadata.HiveException: Hive Runtime Error while processing row at
org.apache.hadoop.hive.ql.exec.tez.TezProcessor.initializeAndRunProcessor(TezProcessor.java:296) at
org.apache.hadoop.hive.ql.exec.tez.TezProcessor.run(TezProcessor.java:250) at
org.apache.tez.runtime.LogicalIOProcessorRuntimeTask.run(LogicalIOProcessorRuntimeTask.java:374) at
org.apache.tez.runtime.task.TaskRunner2Callable$1.run(TaskRunner2Callable.java:73) at
org.apache.tez.runtime.task.TaskRunner2Callable$1.run(TaskRunner2Callable.java:61) at
java.security.AccessController.doPrivileged(Native Method) at javax.security.auth.Subject.doAs(Subject.java:422) at
org.apache.hadoop.security.UserGroupInformation.doAs(UserGroupInformation.java:1730) at
org.apache.tez.runtime.task.TaskRunner2Callable.callInternal(TaskRunner2Callable.java:61) at
org.apache.tez.runtime.task.TaskRunner2Callable.callInternal(TaskRunner2Callable.java:37) at
org.apache.tez.common.CallableWithNdc.call(CallableWithNdc.java:36) at
com.google.common.util.concurrent.TrustedListenableFutureTask$TrustedFutureInterruptibleTask.runInterruptibly(TrustedListenab
leFutureTask.java:108) at com.google.common.util.concurrent.InterruptibleTask.run(InterruptibleTask.java:41) at
com.google.common.util.concurrent.TrustedListenableFutureTask.run(TrustedListenableFutureTask.java:77) at
java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149) at
java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624) at
java.lang.Thread.run(Thread.java:748)Caused by: java.lang.RuntimeException:
org.apache.hadoop.hive.ql.metadata.HiveException: Hive Runtime Error while processing row at
org.apache.hadoop.hive.ql.exec.tez.ReduceRecordSource.pushRecord(ReduceRecordSource.java:304) at
org.apache.hadoop.hive.ql.exec.tez.ReduceRecordProcessor.run(ReduceRecordProcessor.java:318) at
org.apache.hadoop.hive.ql.exec.tez.TezProcessor.initializeAndRunProcessor(TezProcessor.java:267) ... 16 moreCaused by:
org.apache.hadoop.hive.ql.metadata.HiveException: Hive Runtime Error while processing row at
org.apache.hadoop.hive.ql.exec.tez.ReduceRecordSource$GroupIterator.next(ReduceRecordSource.java:378) at
org.apache.hadoop.hive.ql.exec.tez.ReduceRecordSource.pushRecord(ReduceRecordSource.java:294) ... 18 moreCaused by:
java.lang.NullPointerException at
org.apache.hadoop.hive.ql.exec.persistence.PTFRowContainer.first(PTFRowContainer.java:115) at
org.apache.hadoop.hive.ql.exec.PTFPartition.iterator(PTFPartition.java:114) at
org.apache.hadoop.hive.ql.udf.ptf.BasePartitionEvaluator.getPartitionAgg(BasePartitionEvaluator.java:200) at
org.apache.hadoop.hive.ql.udf.ptf.WindowingTableFunction.evaluateFunctionOnPartition(WindowingTableFunction.java:155) at
org.apache.hadoop.hive.ql.udf.ptf.WindowingTableFunction.iterator(WindowingTableFunction.java:538) at
org.apache.hadoop.hive.ql.exec.PTFOperator$PTFInvocation.finishPartition(PTFOperator.java:349) at
org.apache.hadoop.hive.ql.exec.PTFOperator.process(PTFOperator.java:123) at
org.apache.hadoop.hive.ql.exec.Operator.baseForward(Operator.java:994) at
org.apache.hadoop.hive.ql.exec.Operator.forward(Operator.java:940) at
org.apache.hadoop.hive.ql.exec.Operator.forward(Operator.java:927) at
org.apache.hadoop.hive.ql.exec.SelectOperator.process(SelectOperator.java:95) at
org.apache.hadoop.hive.ql.exec.tez.ReduceRecordSource$GroupIterator.next(ReduceRecordSource.java:363) ... 19 more],
TaskAttempt 2 failed, info=[Error: Error while running task ( failure ) :
attempt_1583806821840_6890_2_01_000036_2:java.lang.RuntimeException: java.lang.RuntimeException:
org.apache.hadoop.hive.ql.metadata.HiveException: Hive Runtime Error while processing row at
org.apache.hadoop.hive.ql.exec.tez.TezProcessor.initializeAndRunProcessor(TezProcessor.java:296) at
org.apache.hadoop.hive.ql.exec.tez.TezProcessor.run(TezProcessor.java:250) at
org.apache.tez.runtime.LogicalIOProcessorRuntimeTask.run(LogicalIOProcessorRuntimeTask.java:374) at
org.apache.tez.runtime.task.TaskRunner2Callable$1.run(TaskRunner2Callable
Use max analytic function and filter by it:
with your_data as (
select stack (12,
'Apple', 'pear' , '2020-03-09T12:11:25:622Z',
'Apple', 'pear' , '2020-03-09T12:23:36:825Z',
'Apple', 'lemon' , '2020-03-08T08:01:16:030Z',
'Apple', 'lemon' , '2020-03-09T10:11:12:930Z',
'Apple', 'lemon' , '2020-03-09T15:13:02:610Z',
'Lime', 'peach' , '2020-03-09T07:34:06:825Z',
'Lime', 'peach' , '2020-03-09T07:54:12:220Z',
'Melon', 'banana' , '2020-03-09T03:54:11:041Z',
'Melon', 'banana' , '2020-03-09T09:22:10:220Z',
'Orange','pear' , '2020-03-09T11:13:36:217Z',
'Orange','pear' , '2020-03-09T11:23:26:040Z',
'Orange','pear' , '2020-03-09T11:43:35:721Z'
) as (ID1,ID2,date_time)
)
select ID1,ID2,date_time
from
(
select max(timestamp(regexp_replace(date_time,'(.*?)T(.*?):([^:]*?)Z$','$1 $2\\.$3'))) over (partition by ID1,ID2) as max_date,
ID1,ID2,timestamp(regexp_replace(date_time,'(.*?)T(.*?):([^:]*?)Z$','$1 $2\\.$3')) as date_time
from your_data
)s
where max_date<='2020-03-09 12:00:00'
Result:
id1 id2 date_time
Lime peach 2020-03-09 07:54:12.22
Lime peach 2020-03-09 07:34:06.825
Melon banana 2020-03-09 09:22:10.22
Melon banana 2020-03-09 03:54:11.041
Orange pear 2020-03-09 11:43:35.721
Orange pear 2020-03-09 11:23:26.04
Orange pear 2020-03-09 11:13:36.217