前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >MLSQL数据源开发指南

MLSQL数据源开发指南

作者头像
用户2936994
发布2019-03-22 16:06:17
4280
发布2019-03-22 16:06:17
举报
文章被收录于专栏:祝威廉祝威廉

前言

MLSQL支持标准的Spark DataSource数据源。典型使用如下:

代码语言:javascript
复制
load hive.`public.test` as test;

set data='''
{"key":"yes","value":"no","topic":"test","partition":0,"offset":0,"timestamp":"2008-01-24 18:01:01.001","timestampType":0}
''';

-- load data as table
load jsonStr.`data` as datasource;

select * from datasource as table1;

那么我们如何实现自己的数据源呢?下面我们会分两部分,第一部分是已经有第三方实现了的标准Spark数据源的集成,第二个是你自己创造的新的数据源。

标准Spark 数据源的在封装

我们以HBase为例,这是一个已经实现了标准Spark数据源的驱动,对应的类为org.apache.spark.sql.execution.datasources.hbase。 现在我们要把他封装成MLSQL能够很好兼容的数据源。

我们先看看具体使用方法

代码语言:javascript
复制
--设置链接信息
connect hbase where `zk`="127.0.0.1:2181"
and `family`="cf" as hbase1;

-- 加载hbase 表
load hbase.`hbase1:mlsql_example`
as mlsql_example;

select * from mlsql_example as show_data;


select '2' as rowkey, 'insert test data' as name as insert_table;

-- 保存数据到hbase表
save insert_table as hbase.`hbase1:mlsql_example`;

为了实现上述MLSQL中的hbase数据源,我们只要实现创建一个类实现一些接口就可以实现上述功能:

代码语言:javascript
复制
package streaming.core.datasource.impl
class MLSQLHbase(override val uid: String) extends MLSQLSource with MLSQLSink  with MLSQLRegistry with WowParams {
  def this() = this(BaseParams.randomUID())

你需要保证你的包名和上面一致,也就是streaming.core.datasource.impl或者是streaming.contri.datasource.impl,其次类的名字你随便定义,我们这里定义为MLSQLHBase。 他需要实现一些接口:

  1. MLSQLSource 定义了数据源的名字,实现类以及如何进行数据装载。
  2. MLSQLSink 定义了如何对数据进行存储。
  3. MLSQLRegistry 注册该数据源
  4. WowParams 可以让你暴露出你需要的配置参数。也就是load/save语法里的where条件。

实现load语法

先看看MLSQLSource多有哪些接口要实现:

代码语言:javascript
复制
 trait MLSQLDataSource {
  def dbSplitter = {
    "."
  }

  def fullFormat: String

  def shortFormat: String

  def aliasFormat: String = {
    shortFormat
  }

}

trait MLSQLSourceInfo extends MLSQLDataSource {
  def sourceInfo(config: DataAuthConfig): SourceInfo

  def explainParams(spark: SparkSession): DataFrame = {
    import spark.implicits._
    spark.createDataset[String](Seq()).toDF("name")
  }
}

trait MLSQLSource extends MLSQLDataSource with MLSQLSourceInfo {
  def load(reader: DataFrameReader, config: DataSourceConfig): DataFrame
}

可以看到MLSQLSource 需要实现的方法比较多,我们一个一个来介绍:

代码语言:javascript
复制
def dbSplitter = {
    "."
  }

  def fullFormat: String

  def shortFormat: String

  def aliasFormat: String = {
    shortFormat
  }

dbSplitter定义了库表的分割符号,默认是.,但比如hbase其实是:。 fullFormat是你完整的数据源名字,shortFormat则是短名。aliasFormat一般和shortFormat保持一致。

这里我们覆盖实现结果如下:

代码语言:javascript
复制
 override def fullFormat: String = "org.apache.spark.sql.execution.datasources.hbase"

  override def shortFormat: String = "hbase"

  override def dbSplitter: String = ":"

接着是sourceInfo方法,它的作用主要是提取真实的库表,比如hbase的命名空间和表名。这里是我们HBase的实现:

入参config: DataAuthConfig:

config 参数主要有三个值,分别是path, config, 和df . path 其实就是 load hbase.\jack`` ... 中的jack, config 是个Map, 其实就是where条件形成的,df则可以让你拿到spark 对象。

ConnectMeta.presentThenCall 介绍:

ConnectMeta.presentThenCall 可以让你拿到connect语法里的所有配置选项,然后和你load语法里的where条件进行合并从而拿到所有的配置选项。

代码语言:javascript
复制
override def sourceInfo(config: DataAuthConfig): SourceInfo = {   
    val Array(_dbname, _dbtable) = if (config.path.contains(dbSplitter)) {
      config.path.split(dbSplitter, 2)
    } else {
      Array("", config.path)
    }

    var namespace = _dbname

    if (config.config.contains("namespace")) {
      namespace = config.config.get("namespace").get
    } else {
      if (_dbname != "") {
        val format = config.config.getOrElse("implClass", fullFormat)
       //获取connect语法里的信息
        ConnectMeta.presentThenCall(DBMappingKey(format, _dbname), options => {
          if (options.contains("namespace")) {
            namespace = options.get("namespace").get
          }
        })
      }
    }

    SourceInfo(shortFormat, namespace, _dbtable)
  }

现在实现注册方法:

代码语言:javascript
复制
override def register(): Unit = {
    DataSourceRegistry.register(MLSQLDataSourceKey(fullFormat, MLSQLSparkDataSourceType), this)
    DataSourceRegistry.register(MLSQLDataSourceKey(shortFormat, MLSQLSparkDataSourceType), this)
  }

大家照着写就行。

最后实现最核心的load方法:

代码语言:javascript
复制
override def load(reader: DataFrameReader, config: DataSourceConfig): DataFrame = {
    val Array(_dbname, _dbtable) = if (config.path.contains(dbSplitter)) {
      config.path.split(dbSplitter, 2)
    } else {
      Array("", config.path)
    }

    var namespace = ""

    val format = config.config.getOrElse("implClass", fullFormat)
  // 获取connect语法里的所有配置参数
    if (_dbname != "") {
      ConnectMeta.presentThenCall(DBMappingKey(format, _dbname), options => {
        if (options.contains("namespace")) {
          namespace = options("namespace")
        }
        reader.options(options)
      })
    }

    if (config.config.contains("namespace")) {
      namespace = config.config("namespace")
    }

    val inputTableName = if (namespace == "") _dbtable else s"${namespace}:${_dbtable}"

    reader.option("inputTableName", inputTableName)

    //load configs should overwrite connect configs
    reader.options(config.config)
    reader.format(format).load()
  }

上面的代码其实就是调用了标准的spark datasource api进行操作的。

实现Save语法

代码语言:javascript
复制
trait MLSQLSink extends MLSQLDataSource {
  def save(writer: DataFrameWriter[Row], config: DataSinkConfig): Any
}

因为前面我们已经了MLSQLDataSource需要的方法,所以现在我们只要是实现save语法即可,很简单,也是调用标准的datasource api完成写入:

代码语言:javascript
复制
override def save(writer: DataFrameWriter[Row], config: DataSinkConfig): Unit = {
    val Array(_dbname, _dbtable) = if (config.path.contains(dbSplitter)) {
      config.path.split(dbSplitter, 2)
    } else {
      Array("", config.path)
    }

    var namespace = ""

    val format = config.config.getOrElse("implClass", fullFormat)
    if (_dbname != "") {
      ConnectMeta.presentThenCall(DBMappingKey(format, _dbname), options => {
        if (options.contains("namespace")) {
          namespace = options.get("namespace").get
        }
        writer.options(options)
      })
    }

    if (config.config.contains("namespace")) {
      namespace = config.config.get("namespace").get
    }

    val outputTableName = if (namespace == "") _dbtable else s"${namespace}:${_dbtable}"

    writer.mode(config.mode)
    writer.option("outputTableName", outputTableName)
    //load configs should overwrite connect configs
    writer.options(config.config)
    config.config.get("partitionByCol").map { item =>
      writer.partitionBy(item.split(","): _*)
    }
    writer.format(config.config.getOrElse("implClass", fullFormat)).save()
  }

最后

最后我们定义我们都可以接受那些常用的配置参数

代码语言:javascript
复制
override def explainParams(spark: SparkSession) = {
    _explainParams(spark)
  }

  final val zk: Param[String] = new Param[String](this, "zk", "zk address")
  final val family: Param[String] = new Param[String](this, "family", "default cf")

完整的代码参看: https://github.com/allwefantasy/streamingpro/blob/master/streamingpro-mlsql/src/main/java/streaming/core/datasource/impl/MLSQLHbase.scala

实现loadJson

具体的语法如下:

代码语言:javascript
复制
set data='''
{"key":"yes","value":"no","topic":"test","partition":0,"offset":0,"timestamp":"2008-01-24 18:01:01.001","timestampType":0}
''';

-- load data as table
load jsonStr.`data` as datasource;

select * from datasource as table1;

实现相当简单:

代码语言:javascript
复制
class MLSQLJSonStr(override val uid: String) extends MLSQLBaseFileSource with WowParams {
  def this() = this(BaseParams.randomUID())


  override def load(reader: DataFrameReader, config: DataSourceConfig): DataFrame = {
    val context = ScriptSQLExec.contextGetOrForTest()
    val items = cleanBlockStr(context.execListener.env()(cleanStr(config.path))).split("\n")
    val spark = config.df.get.sparkSession
    import spark.implicits._
    reader.options(rewriteConfig(config.config)).json(spark.createDataset[String](items))
  }

  override def save(writer: DataFrameWriter[Row], config: DataSinkConfig): Unit = {
    throw new RuntimeException(s"save is not supported in ${shortFormat}")
  }

  override def register(): Unit = {
    DataSourceRegistry.register(MLSQLDataSourceKey(fullFormat, MLSQLSparkDataSourceType), this)
    DataSourceRegistry.register(MLSQLDataSourceKey(shortFormat, MLSQLSparkDataSourceType), this)
  }

  override def fullFormat: String = "jsonStr"

  override def shortFormat: String = fullFormat

}
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2019.03.21 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 前言
  • 标准Spark 数据源的在封装
  • 实现load语法
  • 实现Save语法
  • 最后
  • 实现loadJson
相关产品与服务
TDSQL MySQL 版
TDSQL MySQL 版(TDSQL for MySQL)是腾讯打造的一款分布式数据库产品,具备强一致高可用、全球部署架构、分布式水平扩展、高性能、企业级安全等特性,同时提供智能 DBA、自动化运营、监控告警等配套设施,为客户提供完整的分布式数据库解决方案。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档