在hive中将值从字符串转换为int

时间:2014-07-14 10:40:53

标签: casting hive

我正在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;

现在我正在使用命令:

hive>select * from patients limit 5;

我收到了输出:

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

但是当我使用将patient_id指定为字符串时,显示:

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

我试图使用:

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

但是它没有将值更改为int而只显示

NULL
NULL
...

如何将patient_id的值转换为int值?

由于

1 个答案:

答案 0 :(得分:1)

正如@visakh指出你的第一栏中有一个逗号(,):patient_id。

你需要删除它。

您可以使用

x

这类似于 Hive function to replace comma in column value

相关问题