背景
原有功能是这样子的,我们允许用户写一个sql模板,通过${xxx}
来动态传入参数。于是用到了groovy的模板功能,在构造sql的时候,使用模板来对参数进行动态调整。简单代码如下:
String script = "SELECT id, name, age, detail FROM student WHERE grade=${grade}";
ScriptEngineManager factory = new ScriptEngineManager();
SimpleTemplateEngine engine = new SimpleTemplateEngine();
Map<String, Object> bindings = new HashMap<>();
bindings.put("grade", "2021");
System.out.println(engine.createTemplate(script).make(bindings).toString());
// 输出 SELECT id, name, age FROM student WHERE grade=2021
在表中,’detail’是一个json字段。用hive的函数GET_JSON_OBJECT()
来解析json获取里面的字段,于是script变成了
String script = "SELECT id, name, age, GET_JSON_OBJECT(detail, '$.telephone') FROM student WHERE grade=${grade}";
但是改成这样之后,发现一个问题,hive的这个函数使用了groovy的保留字符$
,于是在构建sql的时候会报错。
Exception in thread "main" groovy.lang.GroovyRuntimeException: Failed to parse template script (your template may contain an error or be trying to use expressions not currently supported): startup failed:
SimpleTemplateScript1.groovy: 1: token recognition error at: '.' @ line 1, column 63.
ge, GET_JSON_OBJECT(detail, '$.telephone
^
可以看到,这边还是把$
认为是groovy template的占位符。
解决方案
首先不得不吐槽一下国内博客的质量。。。但即使去外网找,资料也少的可怜。只能自己思考,一个个方案试错。
最终解决方案很简单,即将函数GET_JSON_OBJECT()
中的$
变成转义字符\\$
,即可通过。
String script = "SELECT id, name, age, GET_JSON_OBJECT(detail, '\\$.telephone') FROM student WHERE grade=${grade}";
ScriptEngineManager factory = new ScriptEngineManager();
SimpleTemplateEngine engine = new SimpleTemplateEngine();
Map<String, Object> bindings = new HashMap<>();
bindings.put("grade", "2021");
System.out.println(engine.createTemplate(script).make(bindings).toString());
//输出:SELECT id, name, age, GET_JSON_OBJECT(detail, '$.telephone') FROM student WHERE grade=2021
但是还有一个坑。在代码中测试的时候,字符串中的值应该记为\\$
,而在保存前端修改保存到数据库的时候,应该记为\$
。