公司的etl调度之前一直在azkaban上,后来开发了一个统一的平台,需要把调度全部迁移过去。新的调度以脚本为最小颗粒度,全部配置完成后可以精细化的调度,以及作为脚本血缘的基础数据。
分析
查看脚本内容,格式为from a.b join c.d 的为前置依赖,insert overwrite table xx.xx 的为结果表
后置依赖则查询所有脚本,找出包含上述结果表的脚本
脚本内容的格式不统一,有的from 后面是1个空格,有的是2个空格;有的from 后面直接跟表名,有的换行单独展示表名。因此有下面的统一处理:
删除注释行,打印行,spark设置信息
if (content.startsWith("#")
||content.startsWith("source")
|| content.startsWith("echo")
|| content.startsWith("set")
|| content.startsWith("--")) {
continue;
}
去掉换行符,把脚本内容读取到一行,把多个空格都替换成1个空格
StringBuilder stringBuilder = new StringBuilder();
InputStreamReader in = null;
String content="";
try {
in = new InputStreamReader(new FileInputStream(scriptFile), "UTF-8");
BufferedReader br = new BufferedReader(in);
while ((content = br.readLine()) != null) {
content = content.trim();
//#行剔除,echo 行剔除,set开头的剔除
if (content.startsWith("#") ||content.startsWith("source") || content.startsWith("echo") || content.startsWith("set") || content.startsWith("--")) {
continue;
}
stringBuilder.append(content+" ");
}
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
return stringBuilder.toString().trim().replace("\r\n\t","").toLowerCase().replaceAll(" + ", " ");
通过正则表达式,找出前置依赖表和结果表,剔除temp开头的数据
String text = new FileUtil().readFileToString(scriptFile);
Pattern pattern = Pattern.compile("(?i)\\b(from|join)\\b(?![^\\[\\]]*\\])\\s+(\\[[^\\[\\]]+\\]|\\S+)");
Matcher matcher = pattern.matcher(text);
while (matcher.find()) {
tableName = matcher.group(2);
if (!tableName.startsWith("temp") && !tableName.startsWith("(")) {
if (!names.contains(tableName)) {
names.add(tableName);
}
}
Pattern pattern = Pattern.compile("(?i)\\b(insert overwrite table)\\b(?![^\\[\\]]*\\])\\s+(\\[[^\\[\\]]+\\]|\\S+)");
Matcher matcher = pattern.matcher(text);
while (matcher.find()) {
tableName = matcher.group(2);
if (!tableName.startsWith("temp") && !tableName.startsWith("(")) {
if (!names.contains(tableName)) {
names.add(tableName);
}
}
后置依赖表则通过遍历脚本,查找上述结果表来获取;排除脚本自己
for (String key : keywords) {
if (content.contains(key) && !result.contains(file)) {
//排除脚本自己
if ((!file.getName().equals(scriptName + ".sh") && !file.getName().equals(scriptName + ".sql")) && !file.getAbsolutePath().contains("\\backup\\")) {
System.out.println(file.getAbsolutePath());
System.out.println("找出包含结果表的脚本:" + content);
result.add(file);
}
}
}