arrays - 如何插入具有列数组的数据框> 在 Postgresql 中?
问题描述
我正在尝试在 Postgresql 中存储具有嵌套模式的数据框。有人可以帮我解释一下如何在 Postgres 中存储列(坐标)和(user_mentions)吗?我读过 postgres 可以存储数组类型,但是在尝试写入 DB 时出现错误。我不完全确定我的表是否正确创建。
错误:
Exception in thread "main" java.lang.IllegalArgumentException: Can't get JDBC type for array<array<double>>
数据框架构:
root
|-- created_at: string (nullable = true)
|-- id: long (nullable = true)
|-- text: string (nullable = true)
|-- source: string (nullable = true)
|-- user_id: long (nullable = true)
|-- in_reply_to_status_id: string (nullable = true)
|-- in_reply_to_user_id: long (nullable = true)
|-- lang: string (nullable = true)
|-- retweet_count: long (nullable = true)
|-- reply_count: long (nullable = true)
|-- coordinates: array (nullable = true)
| |-- element: array (containsNull = true)
| | |-- element: double (containsNull = true)
|-- hashtags: array (nullable = true)
| |-- element: string (containsNull = true)
|-- user_mentions: array (nullable = true)
| |-- element: struct (containsNull = true)
| | |-- id: long (nullable = true)
| | |-- id_str: string (nullable = true)
| | |-- indices: array (nullable = true)
| | | |-- element: long (containsNull = true)
| | |-- name: string (nullable = true)
| | |-- screen_name: string (nullable = true)
Postgres 表创建:
create table test-table (created_at varchar, id int, text text, source text, user_id int, in_reply_to_status_id varchar, in_reply_to_user_id int, lang varchar, retweet_count int, reply_count int, coordinates double precision[][], hashtags text[], user_mentions text[]);
火花斯卡拉代码:
val df_1 = df.select(col("created_at"), col("id"), col("text"), col("source"), col("user.id").as("user_id"),
col("in_reply_to_status_id"), col("in_reply_to_user_id"),
col("lang"), col("retweet_count"), col("reply_count"), col("place.bounding_box.coordinates"),
col("entities.hashtags"), col("entities.user_mentions")).withColumn("coordinates", explode(col("coordinates")))
df_1.show(truncate = false)
df_1.printSchema()
df_1.write
.format("jdbc")
.option("url", "postgres_url")
.option("dbtable", "xxx.mytable")
.option("user", "user")
.option("password", "pass")
.save()
样本输入:
坐标栏:
[[80.063341, 26.348309], [80.063341, 30.43339], [88.2027, 30.43339], [88.2027, 26.348309]]
用户提及:
[[123456789, 123456789, [0, 15], Name, ScreenName]]
解决方案
Spark 仅支持使用 JDBC 读取和写入单维数组。您可以将数据转换为多行(即explode
在多行中包含 double[]),或者您可以将数据转换double[][]
为逗号分隔的string[]
或普通的string
.
例如[[1, 2], [3, 4]]
可以转换为["1,2", "3,4"]
推荐阅读
- debugging - Neovim 上的工作 pwa-chrome 类型启动 vimspector 配置是什么?
- php - 从数据库中获取值作为表
- arrays - 如何根据vue js中的数组唯一ID在选择选项中显示数据
- r - 逻辑回归输出建议
- node.js - 通过基于 nodejs 的微服务处理大量并行请求以修改 nosql 数据库中的许可证计数
- c++ - 编辑基于 Llvm 的项目时,Clang 代码模型在 qtcreator 中停止工作
- javascript - 如何在反应原生图像滑块框中更改图像大小
- flutter - 绘制函数 CustomPaint 和 PictureRecorder 以不同的尺寸工作
- javascript - Odoo14 JS 标签未翻译
- html - 我正在尝试使用 aframe 在 asp.net 核心上运行 360 度视频,但视频未加载