gpt4 book ai didi

mysql - 如何使用 Spark 从 .sql 转储中提取包含数据的表?

转载 作者:行者123 更新时间:2023-11-29 01:35:13 24 4
gpt4 key购买 nike

我有大约四个 *.sql 独立转储(每个大约 20GB),我需要将它们转换为 Apache Spark 中的数据集。

我已经尝试使用 InnoDB 安装和制作本地数据库并导入转储,但这似乎太慢了(花了大约 10 个小时)

我直接将文件读入spark使用

import org.apache.spark.sql.SparkSession

var sparkSession = SparkSession.builder().appName("sparkSession").getOrCreate()
var myQueryFile = sc.textFile("C:/Users/some_db.sql")

//Convert this to indexed dataframe so you can parse multiple line create / data statements.
//This will also show you the structure of the sql dump for your usecase.

var myQueryFileDF = myQueryFile.toDF.withColumn("index",monotonically_increasing_id()).withColumnRenamed("value","text")


// Identify all tables and data in the sql dump along with their indexes

var tableStructures = myQueryFileDF.filter(col("text").contains("CREATE TABLE"))
var tableStructureEnds = myQueryFileDF.filter(col("text").contains(") ENGINE"))

println(" If there is a count mismatch between these values choose different substring "+ tableStructures.count()+ " " + tableStructureEnds.count())

var tableData = myQueryFileDF.filter(col("text").contains("INSERT INTO "))

问题是转储包含多个表,每个表都需要成为一个数据集。为此,我需要了解我们是否可以为一张 table 做到这一点。是否有任何为 scala spark 编写的 .sql 解析器?

有没有更快的方法?我可以直接从 .sql 独立文件将其读入配置单元吗?

更新 1:我正在根据 Ajay 给出的输入为此编写解析器

更新 2:将所有内容更改为基于数据集的代码以按照建议使用 SQL 解析器

最佳答案

Is there any .sql parser written for scala spark ?

是的,有一个而且您似乎已经在使用它了。这就是 Spark SQL 本身!惊讶吗?

SQL 解析器接口(interface) (ParserInterface) 可以从 SQL 语句的文本表示创建关系实体。这就是几乎你的情况,不是吗?

请注意 ParserInterface 一次处理单个 SQL 语句,因此您必须以某种方式解析整个转储并找到表定义和行。

ParserInterface 可用作 SessionStatesqlParser

scala> :type spark
org.apache.spark.sql.SparkSession

scala> :type spark.sessionState.sqlParser
org.apache.spark.sql.catalyst.parser.ParserInterface

Spark SQL 附带了几种方法,可以提供接口(interface)的入口点,例如SparkSession.sqlDataset.selectExpr 或简单的 expr 标准函数。您也可以直接使用 SQL 解析器。


无耻插件 你可能想阅读关于 ParserInterface — SQL Parser Contract 的内容在《精通 Spark SQL》一书中。

关于mysql - 如何使用 Spark 从 .sql 转储中提取包含数据的表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52108677/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com