【发布时间】:2019-02-06 02:04:21
【问题描述】:
我有大约四个 *.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 "))
问题在于转储包含多个表,每个表都需要成为数据集。为此,我需要了解我们是否可以为一张桌子做到这一点。有没有为 scala spark 编写的 .sql 解析器?
有没有更快的方法?我可以从 .sql 自包含文件中直接将其读入 hive 吗?
更新 1:我正在根据 Ajay 给出的输入为此编写解析器
更新 2:将所有内容更改为基于数据集的代码以按照建议使用 SQL 解析器
【问题讨论】:
-
如果 sql 包含多个表,您如何设想它变成 1 个数据帧/数据集?否则你应该写自己的数据源格式hackernoon.com/…
-
已编辑。我想为一张表做数据框。编辑问题以更好地反映这一点。谢谢
-
sql转储中的条目(表定义和行)之间的分隔符是什么?
-
你有一个 create table 语句,后面跟着一堆 insert into table 语句。所以给定转储我写的代码可以提取所有表并将它们枚举到数据集中
-
我被困在所有创建语句都被提取到 RDDArray[String] 中,需要对其进行解析以便为多个表创建空数据集结构。使用解析器如何做到这一点?
标签: mysql scala apache-spark