Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hive inserting values to an array complex type column

Tags:

jdbc

hadoop

hive

I am unable to append data to tables that contain an array column using insert into statements; the data type is array < varchar(200) >

Using jodbc I am unable to insert values into an array column by values like :

INSERT INTO demo.table (codes) VALUES (['a','b']);

does not recognises the "[" or "{" signs.

Using the array function like ...

INSERT INTO demo.table (codes) VALUES (array('a','b'));

I get the following error using array function:

Unable to create temp file for insert values Expression of type TOK_FUNCTION not supported in insert/values

Tried the workaround...

INSERT into demo.table (codes) select array('a','b');

unsuccessfully:

Failed to recognize predicate '<EOF>'. Failed rule: 'regularBody' in statement

How can I load array data into columns using jdbc ?

like image 356
Salvador Aceves Avatar asked May 25 '15 22:05

Salvador Aceves


People also ask

What are complex data types in Hive?

Similar to Spark, Hive also support complex data types which includes Array, Map, Struct and union. Array is used to store the list of elements. Map is used to store key/value pair. Struct is for parent and child assosiations.

How do you add values in Hive?

Syntax: INSERT INTO TABLE <table_name> VALUES (<add values as per column entity>); Example: To insert data into the table let's create a table with the name student (By default hive uses its default database to store hive tables).

How do I add a column to an existing table in Hive?

Yes, we can add column inside a table in Hive using a command: ALTER TABLE table_name ADD COLUMNS (column _name datatypes);

Can we add column in external table in Hive?

Since it's an external table, you can just drop the table and recreate with additional columns placed at the end.


2 Answers

My Table has two columns: a STRING, b ARRAY<STRING>.

When I use @Kishore Kumar Suthar's method, I got this:

FAILED: ParseException line 1:33 cannot recognize input near '(' 'a' ',' in statement

But I find another way, and it works for me:

INSERT INTO test.table 
SELECT "test1", ARRAY("123", "456", "789") 
FROM dummy LIMIT 1;

dummy is any table which has atleast one row.

like image 70
White Avatar answered Sep 23 '22 08:09

White


make a dummy table which has atleast one row.

INSERT INTO demo.table (codes) VALUES (array('a','b')) from dummy limit 1;

hive> select codes demo.table;
OK
["a","b"]
Time taken: 0.088 seconds, Fetched: 1 row(s)
like image 44
Kishore Avatar answered Sep 22 '22 08:09

Kishore