您好,欢迎来到知库网。
搜索
您的当前位置:首页StreamingPro 支持多输入,多输出配置

StreamingPro 支持多输入,多输出配置

来源:知库网

前言

最近正好有个需求,就是从不同的数据库以及表里拉出数据,经过一定的处理放到ES里供查询,最好还能放个到parquet里,这样可以支持更复杂的SQL。之前StreamingPro是只能配置一个数据源的,所以做了些改造,方便配置多个数据源,以及多个写出。

输入配置

{
        "name": "batch.sources",
        "params": [
          {
            "path": "file:///tmp/sample.csv",
            "format": "com.databricks.spark.csv",
            "outputTable": "test",
            "header": "true"
          },
          {
            "path": "file:///tmp/sample.csv",
            "format": "com.databricks.spark.csv",
            "outputTable": "test2",
            "header": "true"
          }
        ]
      },

以前用的是 batch.source, 如果你有多个输入源,则需要使用batch.sources 组件。每个源需要配置一个outputTable,也就是说这个源取个名字,方便后面使用。

如果是数据库,则可以这么写:

{
        "name": "batch.sources",
        "params": [
          {
             url:"jdbc:mysql://localhost/test?user=fred&password=secret",
            "dbtable":"table1",
            "driver":"com.mysql...",
            "path": "-",
            "format": "jdbc",
            "outputTable": "test",

          },
          {
            "path": "-",
            "format": "com.databricks.spark.csv",
            "outputTable": "test2",
            "header": "true"
          }
        ]
      },

输出

{
        "name": "batch.outputs",
        "params": [
          {
            "format": "json",
            "path": "file:///tmp/kk2",
            "inputTableName": "finalOutputTable"
          },
          {
            "format": "parquet",
            "path": "file:///tmp/kk3",
            "inputTableName": "finalOutputTable"
          }
        ]
      }

我这里同时输出为json以及parquet格式。

一个简单但是涉及点比较多的例子

{
  "convert-multi-csv-to-json": {
    "desc": "测试",
    "strategy": "spark",
    "algorithm": [],
    "ref": [],
    "compositor": [
      {
        "name": "batch.sources",
        "params": [
          {
            "path": "file:///tmp/sample.csv",
            "format": "com.databricks.spark.csv",
            "outputTable": "test",
            "header": "true"
          },
          {
            "path": "file:///tmp/sample.csv",
            "format": "com.databricks.spark.csv",
            "outputTable": "test2",
            "header": "true"
          }
        ]
      },
      {
        "name": "batch.sql",
        "params": [
          {
            "sql": "select city as tp  from test limit 100",
            "outputTableName": "sqlTable"
          }
        ]
      },
      {
        "name": "batch.script",
        "params": [
          {
            "inputTableName": "sqlTable",
            "outputTableName": "scriptTable",
            "useDocMap": true
          },
          {
            "-": "val count = doc(\"tp\").toString.length;Map(\"count\"->count)"
          }
        ]
      },
      {
        "name": "batch.sql",
        "params": [
          {
            "sql": "select scriptTable.tp,scriptTable.count,test2.city,test2.name  from scriptTable,test2 limit 100",
            "outputTableName": "finalOutputTable"
          }
        ]
      },
      {
        "name": "batch.outputs",
        "params": [
          {
            "format": "json",
            "path": "file:///tmp/kk2",
            "inputTableName": "finalOutputTable"
          },
          {
            "format": "parquet",
            "path": "file:///tmp/kk3",
            "inputTableName": "finalOutputTable"
          }
        ]
      }
    ],
    "configParams": {
    }
  }
}

在 batch.sql 里你可以引用任何一个源的表,或者之前已经在batch.sql里申明的outputTable, 同理batch.script。 而在batch.outputs里,你则可以将任何一张表写入到MySQL,ES,HDFS等文件存储系统中。

将配置文件保存一下,然后就可以启动了:

SHome=/Users/allwefantasy/streamingpro
./bin/spark-submit   --class streaming.core.StreamingApp \
--master local[2] \
--name test \
$SHome/streamingpro-0.4.8-SNAPSHOT-online-1.6.1.jar    \
-streaming.name test    \
-streaming.platform spark \
-streaming.job.file.path file://$SHome/batch.json

Copyright © 2019- zicool.com 版权所有 湘ICP备2023022495号-2

违法及侵权请联系:TEL:199 1889 7713 E-MAIL:2724546146@qq.com

本站由北京市万商天勤律师事务所王兴未律师提供法律服务