sql-code-intelligence 支持标准的Spark SQL补全,也支持[MLSQL](allwefantasy/mlsql)语法补全。
其基本交互方式是,前端将当前用户正在书写的SQL脚本以及当前的光标所在的行列号传递给sql-code-intelligence,sql-code-intelligence会给出一串提示列表。
sql-code-intelligence非常适合配合前端的SQL 编辑器使用。
譬如当前用户书写的SQL如下,鼠标在第三行第十列
此时系统会提示:
如下语句:
假设db.table1 表的字段为a,b,c,d 其中鼠标在低3行第七列,在此位置,会提示:
可以看到,系统具有非常强的跨语句能力,会自动展开*,并且推测出每个表的schema信息从而进行补全。
下载后 tar xvf sql-code-intelligence-0.1.0.tar
解压,执行如下指令即可运行:
java -cp .:sql-code-intelligence-0.1.0.jar tech.mlsql.autosuggest.app.Standalone -config ./config/application.yml
默认端口是9004.
访问接口: http://127.0.0.1:9003/run/script?executeMode=autoSuggest
下面我直接用了一段scala代码访问接口,大家也可以用PostMan:
object Test {
def main(args: Array[String]): Unit = {
val time = System.currentTimeMillis()
val response = Request.Post("http://127.0.0.1:9003/run/script").bodyForm(
Form.form().add("executeMode", "autoSuggest").add("sql",
"""
|select spl from jack.drugs_bad_case_di as a
|""".stripMargin).add("lineNum", "2").add("columnNum", "10").build()
).execute().returnContent().asString()
println(System.currentTimeMillis() - time)
println(response)
}
}
返回结果如下:
[{"name":"split", "metaTable":{"key":{"db":"__FUNC__","table":"split"}, "columns":[ {"name":null,"dataType":"array","isNull":true,"extra":{"zhDoc":"\nsplit函数。用于切割字符串,返回字符串数组\n"}},{"name":"str","dataType":"string","isNull":false,"extra":{"zhDoc":"待切割字符"}}, {"name":"pattern","dataType":"string","isNull":false,"extra":{"zhDoc":"分隔符"}}]}, "extra":{}}]
可以知道提示了split,并且这是一个函数,函数的参数以及返回值都有定义。
我们知道,如果有Schema信息,对提示的帮助会非常大,【SQL Code Intelligence】目前让用户有三种可选方式:
这里我们简单介绍下第一种使用方式。
我下面是使用scala代码完成,用户也可以使用POSTMan之类的工具完成注册。
def registerTable(port: Int = 9003) = {
val time = System.currentTimeMillis()
val response = Request.Post(s"http://127.0.0.1:${port}/run/script").bodyForm(
Form.form().add("executeMode", "registerTable").add("schema",
"""
|CREATE TABLE emps(
| empid INT NOT NULL,
| deptno INT NOT NULL,
| locationid INT NOT NULL,
| empname VARCHAR(20) NOT NULL,
| salary DECIMAL (18, 2),
| PRIMARY KEY (empid),
| FOREIGN KEY (deptno) REFERENCES depts(deptno),
| FOREIGN KEY (locationid) REFERENCES locations(locationid)
|);
|""".stripMargin).add("db", "db1").add("table", "emps").
add("isDebug", "true").build()
).execute().returnContent().asString()
println(response)
}
创建表的语句类型支持三种:db,hive,json。 分别对应MySQL语法,Hive语法,Spark SQL schema json格式。默认是MySQL的语法。
接着就系统就能够提示了:
def testSuggest(port: Int = 9003) = {
val time = System.currentTimeMillis()
val response = Request.Post(s"http://127.0.0.1:${port}/run/script").bodyForm(
Form.form().add("executeMode", "autoSuggest").add("sql",
"""
|select emp from db1.emps as a |""".stripMargin).add("lineNum", "2").add("columnNum", "10").
add("isDebug", "true").build()
).execute().returnContent().asString()
println(response)
}
这个时候系统已经能投提示 empid 和empname字段了。