0
votes

I am trying to cast a string into a datetime object in hadoop pig. But Grunt give me a strange error message : it is like It can't choose the correct 'ToDate' function. It is asking for an 'explicit cast', but I have no clue how to do so. Any idea ?

=> ERROR 1045: Could not infer the matching function for org.apache.pig.builtin.ToDate as multiple or none of them fit. Please use an explicit cast.

grunt> describe infos_by_nu_affa;
infos_by_nu_affa: {NU_AFFA: bytearray,affaires: {(NU_AFFA:bytearray,NU_PCP: bytearray,debut: bytearray,fin: bytearray)},prestations: {(NU_AFFA: bytearray,montant: bytearray,date: bytearray,NU_presta: bytearray,beneficiaire: bytearray)},clients: {(NU_PCP: bytearray,nom: bytearray,prenom: bytearray)}}


derivees = foreach infos_by_nu_affa  generate *, ToDate(affaires.debut, 'dd/MM/yyyy') as (debut:datetime);
2015-03-28 15:46:36,089 [main] ERROR org.apache.pig.tools.grunt.Grunt - ERROR 1045: <line 155, column 0> Could not infer the matching function for org.apache.pig.builtin.ToDate as multiple or none of them fit. Please use an explicit cast.


grunt> dump infos_by_nu_affa
(affaire5,{(affaire5,client5,01/01/14,05/01/15)},{},{(client5,enders,kililan)})
(affaire6,{(affaire6,client6,01/01/13,01/06/14)},{},{(client6,blanco,martine)})
(affaire7,{(affaire7,client7,01/01/10,02/03/13)},{},{(client7,sarah,moore)})
(affaire8,{(affaire8,client8,01/01/15,01/01/01)},{},{(client8,evrard,dominique)})



grunt> derivees = foreach infos_by_nu_affa 
>> generate *,
>> COUNT(prestations) as nb_prestations,
>> ToDate(affaires.debut, 'dd/MM/yy') as debut;
2015-03-28 15:56:06,729 [main] ERROR org.apache.pig.tools.grunt.Grunt - ERROR 1045: 
<line 155, column 0> Could not infer the matching function for org.apache.pig.builtin.ToDate as multiple or none of them fit. Please use an explicit cast.
2

2 Answers

1
votes

The reason is you are passing bag datatype(ie,affaires.debut) as input to the ToDate function but Todate function will accept only chararray or byterarray as input. To solve this issue, you need to flatten the (affaires.debut) before passing to the ToDate function. It should be something like this

derivees_temp = foreach infos_by_nu_affa  generate *,FLATTEN(affaires.debut) as (debut_temp:chararray);
derivees = foreach derivees_temp  generate *, ToDate(debut_temp, 'dd/MM/yyyy') as (debut:datetime);

Note: In the first stmt(ie,derivees_temp), after flattening the datatype for debut_temp is chararray and the second stmt(ie, derivees) after ToDate the datatype for debut is datetime.

0
votes

We get these kind of exception when we doesn't pass correct argument type for the function. You are also facing same here. ToDate function support various type of argument please refer the link and correct the code as per your need.