Carga CSV con comas en los campos citados


Estoy tratando de cargar un archivo CSV en una tabla de colmena de la siguiente manera:

CREATE TABLE mytable
(
num1 INT,
text1 STRING,
num2 INT,
text2 STRING
)
ROW FORMAT DELIMITED FIELDS TERMINATED BY ",";

LOAD DATA LOCAL INPATH '/data.csv'
OVERWRITE INTO TABLE mytable;    


El csv está delimitado por una coma (,) y se ve así:

1, "some text, with comma in it", 123, "more text"

Esto devolverá datos corruptos ya que hay un ',' en la primera cadena.
¿Hay alguna manera de establecer un delimitador de texto o hacer que Hive ignore el ',' en cadenas?

No puedo cambiar el delimitador del csv ya que se extrae de una fuente externa.

Author: wrschneider, 2012-11-29

5 answers

El problema es que Hive no maneja textos citados. Puede preprocesar los datos cambiando el delimitador entre los campos (por ejemplo: con un trabajo de Hadoop-streaming) o también puede intentar usar un SerDe CSV personalizado que use OpenCSV para analizar los archivos.

 26
Author: Lorand Bendig,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2012-11-29 16:52:44

Si puede volver a crear o analizar sus datos de entrada, puede especificar un carácter de escape para la TABLA CREATE:

ROW FORMAT DELIMITED FIELDS TERMINATED BY "," ESCAPED BY '\\';

Aceptará esta línea como 4 campos

1,some text\, with comma in it,123,more text
 28
Author: libjack,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2012-11-30 13:59:11

A partir de Hive 0.14, el SerDe CSV es una parte estándar de la instalación de Hive

ROW FORMAT SERDE 'org.apache.hadoop.hive.serde2.OpenCSVSerde'

(Véase: https://cwiki.apache.org/confluence/display/Hive/CSV+Serde )

 18
Author: wrschneider,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2016-02-11 03:16:21

Mantenga el delimitador entre comillas simples, funcionará.

ROW FORMAT DELIMITED 
FIELDS TERMINATED BY ','
LINES TERMINATED BY '\n';

Esto funcionará

 0
Author: suyash,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2016-08-09 19:23:11

Agregue una barra diagonal hacia atrás en LOS CAMPOS TERMINADOS POR'\; '

Por Ejemplo:

CREATE  TABLE demo_table_1_csv
COMMENT 'my_csv_table 1'
ROW FORMAT DELIMITED
FIELDS TERMINATED BY '\;'
LINES TERMINATED BY '\n'
STORED AS TEXTFILE
LOCATION 'your_hdfs_path'
AS 
select a.tran_uuid,a.cust_id,a.risk_flag,a.lookback_start_date,a.lookback_end_date,b.scn_name,b.alerted_risk_category,
CASE WHEN (b.activity_id is not null ) THEN 1 ELSE 0 END as Alert_Flag 
FROM scn1_rcc1_agg as a LEFT OUTER JOIN scenario_activity_alert as b ON a.tran_uuid = b.activity_id;

Lo he probado, y funcionó.

 0
Author: Mantej Singh,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2017-09-14 14:54:07