How to convert a String to Integer in Hive? - Big Data In Real World

How to convert a String to Integer in Hive?

How does Shuffle Hash Join work in Spark?
January 1, 2021
How to properly add jars to a Spark application?
January 6, 2021
How does Shuffle Hash Join work in Spark?
January 1, 2021
How to properly add jars to a Spark application?
January 6, 2021

Converting a String column to an Integer column or converting a column from one type to another is quite simple in Hive. Simply use the cast function to cast the type from one to another.

Here is how you use it – cast(str_column as int) 

SELECT col1, cast(str_col as int), col3 from sample_table;

Note that int value is between –2147483648 and 2147483647. You should use bigint If the values in the column are greater than the upper bound of int – cast(string_filed as bigint)

What happens if Hive encountered a string which is not an integer?

If you have a non numerical value (or a non integer) value in a column and you attempted to convert that to an Integer. Hive will be forgiving in that regard. It will simply substitute null and continue processing.

 

Big Data In Real World
Big Data In Real World
We are a group of Big Data engineers who are passionate about Big Data and related Big Data technologies. We have designed, developed, deployed and maintained Big Data applications ranging from batch to real time streaming big data platforms. We have seen a wide range of real world big data problems, implemented some innovative and complex (or simple, depending on how you look at it) solutions.

Comments are closed.

How to convert a String to Integer in Hive?
This website uses cookies to improve your experience. By using this website you agree to our Data Protection Policy.

Hadoop In Real World is now Big Data In Real World!

X