我有大约四个 *.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 解析器
是否有为 scala Spark 编写的 .sql 解析器?
是的,有一个,而且您似乎已经在使用它了。这就是 Spark SQL 本身!惊讶吗?
SQL 解析器接口(ParserInterface
)可以从 SQL 语句的文本表示创建关系实体。那是almost你的情况是吧?
请注意ParserInterface
一次处理一个 SQL 语句,所以你必须somehow解析整个转储并找到表定义和行。
The ParserInterface
可用为sqlParser
of a SessionState
.
scala> :type spark
org.apache.spark.sql.SparkSession
scala> :type spark.sessionState.sqlParser
org.apache.spark.sql.catalyst.parser.ParserInterface
Spark SQL 附带了多种提供接口入口点的方法,例如SparkSession.sql
, Dataset.selectExpr
或者简单地expr
标准功能。您也可以直接使用 SQL 解析器。
无耻插头您可能想阅读有关ParserInterface — SQL 解析器合约 https://jaceklaskowski.gitbooks.io/mastering-spark-sql/spark-sql-ParserInterface.html在《掌握 Spark SQL》一书中。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)