Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hive Converting from Double to String Not in Scientific

Tags:

hive

I have a table with a column double type, and I am trying to convert that from double to string.

However, when I use the cast command, it "smartly" convert that into scientific notation. like below:

select 
    number, 
    cast(number as string), 
from ...

it looks like

number     c1_
9999999902  9.999999902E9
9999999902  9.999999902E9
9999999902  9.999999902E9
9999999902  9.999999902E9
9999999909  9.999999909E9

Can anyone show me how to avoid converting that into scientific and keep the raw text?

like image 934
B.Mr.W. Avatar asked Sep 15 '15 01:09

B.Mr.W.


People also ask

How do I change datatype in Hive?

By using this command below one can change the column data type: ALTER TABLE table_name CHANGE column_name column_name new_datatype; I hope this works.

What Hiveql function can you use to convert data from one type to another?

Hive CAST function converts the value of an expression to any other type. The result of the function will be NULL in case if function cannot converts to particular data type. You can use this function when passing a column value or literal to a function that expects a parameter with a different data type.

What is Concat_ws in Hive?

Description. CONCAT_WS() stands for Concatenate With Separator and is a special form of CONCAT() . The first argument is the separator for the rest of the arguments. The separator is added between the strings to be concatenated. The separator can be a string, as can the rest of the arguments.

What is coalesce in Hive?

COALESCE(T v1, T v2, …) Returns the first v that is not NULL, or NULL if all v's are NULL. Return: T. CASE a WHEN b THEN c [WHEN d THEN e]* [ELSE f] END. This is similar to the switch statement where when a = b, returns c; when a = d, returns e; else returns f.


2 Answers

Hive converts double to scientific representation while cast to string because Hive treats double itself in a same way. Therefore, problem is not with cast to string.

See below example:

 select 9999999902.0, cast(9999999902.0 as BIGINT), cast(cast(9999999902.0 as BIGINT)  as string) from ..

Output:

OK
9.999999902E9   9999999902
like image 95
Sachin Gaikwad Avatar answered Oct 17 '22 14:10

Sachin Gaikwad


Hive supports good old printf() function so that you can control the output format explicitly - check Language Manual UDF under "String functions"

like image 13
Samson Scharfrichter Avatar answered Oct 17 '22 15:10

Samson Scharfrichter