基于我使用的注释中的@auprba链接
this link
提出了这个解决方案。
val replaced = df.selectExpr("""
named_struct(
.....................................................
....... Other columns ...............................
....... In a form of ...............................
....... '{columnname}', {columnname}, ...............
.....................................................
'user', named_struct(
'country', user.country,
'id', user.id,
'ip_address', user.ip_address,
'state', named_struct('hard_currency', null, 'level', null, 'session_id', user.state.session_id, 'soft_currency', null, 'xp', null)
)
) as named_struct
""").select("named_struct.*")
display(replaced)