Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to insert values into array and map in sql databricks?

I'm able to create a schema in databricks but how can I insert data into the array and map? I just cannot find any information regarding SQL. It's all about python, scala, but I'm looking for sql only.

CREATE TABLE IF NOT EXISTS mydb.arraytest (
  capacity array<INT>,
  mapper MAP<INT, STRING>,
  device_type STRING,
  location_id INT
)

Gives me the expected schema:

+-------------+-----------------+ 
| col_name    | data_type       | 
+-------------+-----------------+
| capacity    | array<int>      | 
| mapper      | map<int,string> | 
| device_type | string          |
| location_id | int             |

# Partitioning
Not partitioned

But I just don't know how can I insert values into the array and the map?

None of what I tried worked:

INSERT INTO
  mydb.arraytest
VALUES
  (array<1,2,3>, (1, 'mapstring1'), 'string1', 1)
  (<1,2,3>, ....
like image 855
Wondarar Avatar asked Jul 07 '26 04:07

Wondarar


1 Answers

The Array and Map functions start with brackets, so something like this should work for you:

%sql
INSERT INTO arraytest
VALUES (Array(1,2,3), Map(1, 'mapstring1'), 'string1', 1)
like image 80
wBob Avatar answered Jul 09 '26 04:07

wBob



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!