3

I am creating a table in hive;

create table patients(
  patient_id INT,
  age_group STRING,
  gender STRING,
  income_range STRING
)
ROW FORMAT DELIMITED FIELDS TERMINATED BY ' ';

load data local inpath '/mnt/patients.csv' into table patients;

Now when I am using the command:

hive>select * from patients limit 5;

I am getting the output:

NULL    75-84,  F,      32000-47999
NULL    75-84,  M,      16000-23999
NULL    85+,    M,      <16000
NULL    65-74,  F,      32000-47999
NULL    <65,    M,      <16000

But when I am using assigning patient_id as string its showing:

 910997967,      75-84,  F,      32000-47999
 506013497,      75-84,  M,      16000-23999
 432041392,      85+,    M,      <16000
 633048699,      65-74,  F,      32000-47999

I tried to use :

 hive>select CAST(patient_id AS int) from patients;

But its not changing the values to int and only showing

NULL
NULL
...

How could the values of patient_id can be converted to int values?

Thanks

3
  • 2
    It seems you have a comma as a part of the data. I would suggest to take care of it while loading the data into the table. Once the comma is removed from the string, the CAST will work correctly. Commented Jul 14, 2014 at 11:27
  • First make sure the delimiter you are using is correct w.r.t your csv file. Second check your int/double/float values in csv file, they should not have "" in the file. In your case it seems the problem of delimiters as @visakh pointed out. Commented Jul 14, 2014 at 12:22
  • @visakh:thanks a lot.your observation helped me Commented Jul 14, 2014 at 13:30

1 Answer 1

2

As @visakh pointed out that there is a comma(,) in your 1st column: patient_id.

You need to remove this.

You may use

    CAST(regexp_replace(patient_id, ',' , '') AS INT)

This is similar to Hive function to replace comma in column value

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.