Search code examples
apache-pig

Float to int in Apache pig


I have a file which I am processing using apache pig. I have a chararray field which I am casting to float, if the value of that field is less than 0, I want to make it integer 0 and if it is greater than zero, then keep the float number as it is.

For ex: if number is -1234.56, make it integer 0. If number is 2345.67, keep it 2345.67

I am failing using the following code, Output is coming 0.0 and I want 0, due to which my job is failing. Please help.!

((float)field_name > 0 ? (float)field_name:(int)0) AS field_name,

Solution

  • Fields in Pig (such as field_name) must contain only values of the same type, such as all float or all int. When you run the code above, Pig automatically converts the int 0 to the float 0.0 since field_name must be able to take on float values like 2345.67. You probably got the warning Encountered Warning IMPLICIT_CAST_TO_FLOAT - this is why. I'd recommend reworking the rest of your code so that an output of 0.0 doesn't cause the job to fail.