spark sql 将字符串列插入到数组类型列的结构中
spark sql Insert string column to struct of array type column
我正在尝试将 STRING 类型的列插入到 ARRAY of STRUCT TYPE 列中,但遇到错误。你能帮忙提供正确的方向来做插入吗?
在 databricks notebook 中,我有一个原始的 table (raw_lms.rawTable
),其中所有列都是字符串类型。这需要插入一个转换 table (tl_lms.transformedTable
),其中列是结构类型的数组。
CREATE TABLE raw_lms.rawTable
( PrimaryOwners STRING
,Owners STRING
)
USING DELTA LOCATION 'xxxx/rawTable'
CREATE TABLE tl_lms.transformedTable
( PrimaryOwners array<struct<Id:STRING>>
,Owners array<struct<Id:STRING>>
)
USING DELTA LOCATION 'xxxx/transformedTable'
原始 table 填充了以下值:例如
INSERT INTO TABLE raw_lms.rawTable
VALUES
("[{'Id': '1393fe1b-bba2-4343-dff0-08d9dea59a03'}, {'Id': 'cf2e6549-5d07-458c-9d30-08d9dd5885cf'}]",
"[]"
)
我尝试插入转换 table 并得到以下错误:
INSERT INTO tl_lms.transformedTable
SELECT PrimaryOwners,
Owners
FROM raw_lms.rawTable
Error in SQL statement: AnalysisException: cannot resolve
'spark_catalog.raw_lms.rawTable.PrimaryOwners
' due to data type
mismatch: cannot cast string to array<struct<Id:string>>;
不想爆数据。我只需要在不同列数据类型的 rawTable
和 transformedTable
之间简单地插入一行。
感谢您的宝贵时间和帮助。
如错误消息所述,您不能将字符串作为数组插入。您需要使用 array
and named_struct
函数。
更改原始类型 table 以更正类型和类型而不是字符串,然后试试这个:
INSERT INTO TABLE raw_lms.rawTable
VALUES
(array(named_struct('id', '1393fe1b-bba2-4343-dff0-08d9dea59a03'), named_struct('id', 'cf2e6549-5d07-458c-9d30-08d9dd5885cf')),
null
);
或者,如果您想将列保留为原始字符串 table,则在插入之前使用 from_json
将字符串解析为正确的类型:
INSERT INTO tl_lms.transformedTable
SELECT from_json(PrimaryOwners, 'array<struct<Id:STRING>>'),
from_json(Owners, 'array<struct<Id:STRING>>')
FROM raw_lms.rawTable
我正在尝试将 STRING 类型的列插入到 ARRAY of STRUCT TYPE 列中,但遇到错误。你能帮忙提供正确的方向来做插入吗?
在 databricks notebook 中,我有一个原始的 table (raw_lms.rawTable
),其中所有列都是字符串类型。这需要插入一个转换 table (tl_lms.transformedTable
),其中列是结构类型的数组。
CREATE TABLE raw_lms.rawTable
( PrimaryOwners STRING
,Owners STRING
)
USING DELTA LOCATION 'xxxx/rawTable'
CREATE TABLE tl_lms.transformedTable
( PrimaryOwners array<struct<Id:STRING>>
,Owners array<struct<Id:STRING>>
)
USING DELTA LOCATION 'xxxx/transformedTable'
原始 table 填充了以下值:例如
INSERT INTO TABLE raw_lms.rawTable
VALUES
("[{'Id': '1393fe1b-bba2-4343-dff0-08d9dea59a03'}, {'Id': 'cf2e6549-5d07-458c-9d30-08d9dd5885cf'}]",
"[]"
)
我尝试插入转换 table 并得到以下错误:
INSERT INTO tl_lms.transformedTable
SELECT PrimaryOwners,
Owners
FROM raw_lms.rawTable
Error in SQL statement: AnalysisException: cannot resolve 'spark_catalog.raw_lms.rawTable.
PrimaryOwners
' due to data type mismatch: cannot cast string to array<struct<Id:string>>;
不想爆数据。我只需要在不同列数据类型的 rawTable
和 transformedTable
之间简单地插入一行。
感谢您的宝贵时间和帮助。
如错误消息所述,您不能将字符串作为数组插入。您需要使用 array
and named_struct
函数。
更改原始类型 table 以更正类型和类型而不是字符串,然后试试这个:
INSERT INTO TABLE raw_lms.rawTable
VALUES
(array(named_struct('id', '1393fe1b-bba2-4343-dff0-08d9dea59a03'), named_struct('id', 'cf2e6549-5d07-458c-9d30-08d9dd5885cf')),
null
);
或者,如果您想将列保留为原始字符串 table,则在插入之前使用 from_json
将字符串解析为正确的类型:
INSERT INTO tl_lms.transformedTable
SELECT from_json(PrimaryOwners, 'array<struct<Id:STRING>>'),
from_json(Owners, 'array<struct<Id:STRING>>')
FROM raw_lms.rawTable