You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user-zh@flink.apache.org by 刘建刚 <li...@gmail.com> on 2021/10/26 03:27:26 UTC

How to execute multi SQL in one job

I have multi batch SQL commands separated by semicolon(;). The SQL commands
need to be executed in order(In other cases, the SQL command may share
sources or sinks). I want to execute them in one job. When I
use tableEnv.executeSql(multiSQL), it will throw errors.  How can I execute
them in one job? Thanks.

Re: How to execute multi SQL in one job

Posted by 刘建刚 <li...@gmail.com>.
Thanks very much. Forgive me for the simple question. I have found the doc
in the latest code. My inner code is too old...

Jake <ft...@qq.com> 于2021年10月26日周二 上午11:39写道:

>
> Hi, you can do like this:
>
> ```java
>
> val statementSet = tableEnv.createStatementSet()
> val insertSqlBuffer = ListBuffer.empty[String]
>
>
> val calciteParser = new CalciteParser(SqlUtil.getSqlParserConfig
> (tableEnv.getConfig))
> sqlArr
> .foreach(item => {
> println(item)
> val itemNode = calciteParser.parse(item)
>
> itemNode match {
> case sqlSet: SqlSet => {
> configuration.setString(sqlSet.getKeyString, sqlSet.getValueString)
> }
> case _: RichSqlInsert => insertSqlBuffer += item
> case _ => {
> println(item)
> val itemResult = tableEnv.executeSql(item)
> itemResult.print()
> }
> }
> })
>
> // execute batch inserts
> if (insertSqlBuffer.size > 0) {
> insertSqlBuffer.foreach(item => {
> println("insert sql: " + item)
> statementSet.addInsertSql(item)
> })
> val explain = statementSet.explain()
> println(explain)
> statementSet.execute()
> }
>
>
> ```
>
>
> On Oct 26, 2021, at 11:27, 刘建刚 <li...@gmail.com> wrote:
>
> I have multi batch SQL commands separated by semicolon(;). The SQL
> commands need to be executed in order(In other cases, the SQL command may
> share sources or sinks). I want to execute them in one job. When I
> use tableEnv.executeSql(multiSQL), it will throw errors.  How can I execute
> them in one job? Thanks.
>
>
>

Re: How to execute multi SQL in one job

Posted by 刘建刚 <li...@gmail.com>.
Thanks very much. Forgive me for the simple question. I have found the doc
in the latest code. My inner code is too old...

Jake <ft...@qq.com> 于2021年10月26日周二 上午11:39写道:

>
> Hi, you can do like this:
>
> ```java
>
> val statementSet = tableEnv.createStatementSet()
> val insertSqlBuffer = ListBuffer.empty[String]
>
>
> val calciteParser = new CalciteParser(SqlUtil.getSqlParserConfig
> (tableEnv.getConfig))
> sqlArr
> .foreach(item => {
> println(item)
> val itemNode = calciteParser.parse(item)
>
> itemNode match {
> case sqlSet: SqlSet => {
> configuration.setString(sqlSet.getKeyString, sqlSet.getValueString)
> }
> case _: RichSqlInsert => insertSqlBuffer += item
> case _ => {
> println(item)
> val itemResult = tableEnv.executeSql(item)
> itemResult.print()
> }
> }
> })
>
> // execute batch inserts
> if (insertSqlBuffer.size > 0) {
> insertSqlBuffer.foreach(item => {
> println("insert sql: " + item)
> statementSet.addInsertSql(item)
> })
> val explain = statementSet.explain()
> println(explain)
> statementSet.execute()
> }
>
>
> ```
>
>
> On Oct 26, 2021, at 11:27, 刘建刚 <li...@gmail.com> wrote:
>
> I have multi batch SQL commands separated by semicolon(;). The SQL
> commands need to be executed in order(In other cases, the SQL command may
> share sources or sinks). I want to execute them in one job. When I
> use tableEnv.executeSql(multiSQL), it will throw errors.  How can I execute
> them in one job? Thanks.
>
>
>

Re: How to execute multi SQL in one job

Posted by Jake <ft...@qq.com.INVALID>.
Hi, you can do like this: 

```java
val statementSet = tableEnv.createStatementSet()
val insertSqlBuffer = ListBuffer.empty[String]

val calciteParser = new CalciteParser(SqlUtil.getSqlParserConfig(tableEnv.getConfig))
sqlArr
    .foreach(item => {
        println(item)
        val itemNode = calciteParser.parse(item)

        itemNode match {
            case sqlSet: SqlSet => {
                configuration.setString(sqlSet.getKeyString, sqlSet.getValueString)
            }
            case _: RichSqlInsert => insertSqlBuffer += item
            case _ => {
                println(item)
                val itemResult = tableEnv.executeSql(item)
                itemResult.print()
            }
        }
    })

// execute batch inserts
if (insertSqlBuffer.size > 0) {
    insertSqlBuffer.foreach(item => {
        println("insert sql: " + item)
        statementSet.addInsertSql(item)
    })
    val explain = statementSet.explain()
    println(explain)
    statementSet.execute()
}

```


> On Oct 26, 2021, at 11:27, 刘建刚 <li...@gmail.com> wrote:
> 
> I have multi batch SQL commands separated by semicolon(;). The SQL commands need to be executed in order(In other cases, the SQL command may share sources or sinks). I want to execute them in one job. When I use tableEnv.executeSql(multiSQL), it will throw errors.  How can I execute them in one job? Thanks.


Re: How to execute multi SQL in one job

Posted by Jake <ft...@qq.com>.
Hi, you can do like this: 

```java
val statementSet = tableEnv.createStatementSet()
val insertSqlBuffer = ListBuffer.empty[String]

val calciteParser = new CalciteParser(SqlUtil.getSqlParserConfig(tableEnv.getConfig))
sqlArr
    .foreach(item => {
        println(item)
        val itemNode = calciteParser.parse(item)

        itemNode match {
            case sqlSet: SqlSet => {
                configuration.setString(sqlSet.getKeyString, sqlSet.getValueString)
            }
            case _: RichSqlInsert => insertSqlBuffer += item
            case _ => {
                println(item)
                val itemResult = tableEnv.executeSql(item)
                itemResult.print()
            }
        }
    })

// execute batch inserts
if (insertSqlBuffer.size > 0) {
    insertSqlBuffer.foreach(item => {
        println("insert sql: " + item)
        statementSet.addInsertSql(item)
    })
    val explain = statementSet.explain()
    println(explain)
    statementSet.execute()
}

```


> On Oct 26, 2021, at 11:27, 刘建刚 <li...@gmail.com> wrote:
> 
> I have multi batch SQL commands separated by semicolon(;). The SQL commands need to be executed in order(In other cases, the SQL command may share sources or sinks). I want to execute them in one job. When I use tableEnv.executeSql(multiSQL), it will throw errors.  How can I execute them in one job? Thanks.