Load many difficult objects in hive - hive

I have old version of hive, below hive 2.0, in this hive version i create a table:
create table test (int id, string name, values array<string>)
When i add data, i use this query:
insert into table test select 1, 'Sam', array('sql', 'c++', 'c#', 'java')
It work, but i need load more then one row in table, like:
insert into table select (1, 'Sam', array('sql', 'c++', 'c#', 'java')), (2, 'AN Other', array('paskal'))
How i can do this?

Hive doesn't support use UDFs in something like this
INSERT INTO TABLE test VALUES (1, 'Sam', array('sql', 'c++', 'c#', 'java')), (2, 'test1', array('paskal'));
you will get something like
Unable to create temp file for insert values Expression of type TOK_FUNCTION not supported in insert/values
A workaround would be something like this
INSERT INTO TABLE test
select 1, 'Sam', array('sql', 'c++', 'c#', 'java')
union all
select 2, 'test1', array('paskal');

Related

Select all from drivers table in database

I am writing a code to insert new drivers and vehicles into my database and along the way I came up with an error. Below is the code to create the drivers and vehicles table in my database.
create table drivers (
id serial primary key,
first_name varchar,
last_name varchar
);
create table vehicles (
id serial primary key,
make varchar,
model varchar,
driver_id integer references drivers(id)
);
Below is my INSERT statement
INSERT INTO drivers (first_name, last_name)
VALUES
('Amy', 'Hua'),
('UDOETE', 'AKAN'),
('UCHE', 'CALEB'),
('TERKIMBI', 'VANGE'),
('PETER', 'O. OKEOWO'),
('OTOGO', 'IRINEN'),
('OSAKA', 'GEORGE C.');
SELECT * from drivers;
INSERT INTO vehicles (make, model, driver(id))
VALUES
('2023 Acura', 'Integra', 1),
('2022 Acura', 'MDX', 2),
('2022 Acura', 'NSX', 3);
SELECT * from vehicles;
RETURNING *;
I was asked to Select all driver records; select all vehicle records; select only 3 vehicle records. But after writing the above code into it, it gave this error ERROR: syntax error at or near "(" Position: 44 and my code is just 22 lines.
Thanks in advance for your help.
'44' refers to the character position within the statement that caused the error:
yours:
INSERT INTO vehicles (make, model, driver(id))
must be:
INSERT INTO vehicles (make, model, driver_id)
This is my final code below, I removed the RETURNING statement and only displayed it with the SELECT statement.
INSERT INTO drivers (first_name, last_name)
VALUES
('Amy', 'Hua'),
('UDOETE', 'AKAN'),
('UCHE', 'CALEB'),
('TERKIMBI', 'VANGE'),
('PETER', 'O. OKEOWO'),
('OTOGO', 'IRINEN'),
('OSAKA', 'GEORGE C.');
SELECT * from drivers;
INSERT INTO vehicles (make, model, driver_id)
VALUES
('2023 Acura', 'Integra', 17),
('2022 Acura', 'MDX', 18),
('2022 Acura', 'NSX', 19);
SELECT * from vehicles;

EXPLAIN Failed. 3706: (-3706)Syntax error: expected something between ')' and ','

I've seen this error mentioned on StackOverflow, but not in the context I am using. I am relatively new to Teradata and this behavior is throwing me for a loop. Here is code that works:
INSERT INTO test_table (this, that) VALUES (1, 2);
Here is code that throws the error:
INSERT INTO test_table (this, that) VALUES (1, 2), (3, 4);
This is super confusing to me because the Teradata docs have the following example:
INSERT INTO cities VALUES (2, 'San Jose'), (3, 'Oakland');
Could someone show me what am I missing here? Thanks!
Teradata only allows you to insert one record with a single values. You can see this in the syntax diagram for insert -- there is no "backloop".
Two inserts is a simple workaround:
INSERT INTO test_table (this, that)
VALUES (1, 2);
INSERT INTO test_table (this, that)
VALUES (3, 4);

How to insert insert some sample data for this table in hive

I am doing some practice myself and here is the table I created on hive, I want to add some sample data into it, can anyone help please?
What I tried is below:
INSERT INTO VALUES (
'John Smith',
array('Toronto','Ontario', 'Canada'),
named_struct('male', 38),
map('Smith', 3)
);
Cannot convert column 2 from struct male:int to struct sex:string,age:int.
I figured out the solution!
The issue was with struct field.
INSERT INTO family_head VALUES (
'John Smith',
array('Toronto','Ontario', 'Canada'),
s_struct('sex', 'male', 'age', 38),
map('Smith', 3)
);
The result is like below:
I hope this is helpful to later readers.

Dumbfounded declaring a variable in sql

I have searched across the board and the internet trying to see what I'm doing wrong. I have endlessly gotten the "Declare: syntax" error no matter where in the code I have moved the declaration. I've tried it with and without # signs and semicolons thinking this was something different than C++. All the guides I've read say this should work so perhaps someone here can tell me what is wrong?
BEGIN TRANSACTION;
DECLARE #clID INT;
CREATE TABLE CLAIM(claimID, repDate, lossDate, claimNo, claimStat);
INSERT INTO CLAIM VALUES(1, '2016-10-1', '2016-10-1', 1, 'Open');
INSERT INTO CLAIM VALUES(2, '2016-10-1', '2016-10-1', 2, 'Open');
CREATE TABLE EXPOSURE(expID, claimID, coverage, claimEx, expStat);
INSERT INTO EXPOSURE VALUES(1, 2, 'BI', 'U152', 'Open');
INSERT INTO EXPOSURE VALUES(2, 2, 'PD', 'U152', 'Open');
CREATE TABLE RESERVELINE(resLineID, expID, claimID, covID, IP);
INSERT INTO RESERVELINE VALUES(1, 1, 2, 'BI', 02);
INSERT INTO RESERVELINE VALUES(1, 3, 3, 'CDL', 01);
CREATE TABLE RESULTS(claimID);
COMMIT;
INSERT INTO RESULTS
SELECT claimID FROM CLAIM WHERE repDate<'2016-10-3';
SELECT * FROM RESULTS;
--Must FIRST query first table with Date()
Thanks guys.

What's wrong with this SQL INSERT statement?

I want to insert a certain value into a certain field of five different rows altogether. But whenever I run this query it doesn't get executed. What's wrong with it, and how can I fix it?
INSERT INTO `employee`(`password`) VALUES ('abc') WHERE `id` IN (1,2,3,4,5);
An INSERT can't have a WHERE clause. It looks like you meant to do an update:
UPDATE `employee`
SET `password` = 'abc'
WHERE `id` IN (1,2,3,4,5);
Or perhaps a multi-row insert:
INSERT INTO `employee` (`id`, `password`)
VALUES (1, 'abc'), (2, 'abc'), (3, 'abc'), (4, 'abc'), (5, 'abc');
Also, FYI, you really shouldn't be storing passwords as plain text, which it looks like you may be doing.