根据数据源生成SQL

根据数据源信息生成 SparkSQL 和 JdbcSQL,包含DDL、DML、DQL。

接口地址:/api/rest_j/v1/metadataQuery/getSparkSql

请求方式:GET

请求数据类型:application/x-www-form-urlencoded

请求参数:

参数名说明是否必须数据类型
dataSourceName数据源名称String
system系统名称String
database数据库名称String
table表名称String

响应示例:

  1. {
  2. "method": null,
  3. "status": 0,
  4. "message": "OK",
  5. "data": {
  6. "sparkSql": {
  7. "ddl": "CREATE TEMPORARY TABLE test USING org.apache.spark.sql.jdbc OPTIONS ( url 'jdbc:mysql://localhost:3306/test', dbtable 'test', user 'root', password 'password')",
  8. "dml": "INSERT INTO test SELECT * FROM ${resultTable}",
  9. "dql": "SELECT id,name FROM test"
  10. }
  11. }
  12. }

目前支持jdbc、kafka、elasticsearch、mongo 数据源,可以根据SparkSQLDDL注册 spark table 进行查询

接口地址:/api/rest_j/v1/metadataQuery/getJdbcSql

请求方式:GET

请求数据类型:application/x-www-form-urlencoded

请求参数:

参数名说明是否必须数据类型
dataSourceName数据源名称String
system系统名称String
database数据库名称String
table表名称String

响应示例:

  1. {
  2. "method": null,
  3. "status": 0,
  4. "message": "OK",
  5. "data": {
  6. "jdbcSql": {
  7. "ddl": "CREATE TABLE `test` (\n\t `id` varchar(64) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NOT NULL COMMENT '列名是id',\n\t `name` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NOT NULL COMMENT '列名是name',\n\t PRIMARY KEY (`id`)\n\t) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci",
  8. "dml": "INSERT INTO test SELECT * FROM ${resultTable}",
  9. "dql": "SELECT id,name FROM test"
  10. }
  11. }
  12. }

目前支持 JDBC 数据源,如:mysql、oracle、postgres等,JdbcSQLDDL可以用于前端展示。

  1. 需要先注册数据源

定义DDL_SQL_TEMPLATE,获取数据源信息进行替换

  1. public static final String JDBC_DDL_SQL_TEMPLATE =
  2. "CREATE TEMPORARY TABLE %s "
  3. + "USING org.apache.spark.sql.jdbc "
  4. + "OPTIONS ("
  5. + " url '%s',"
  6. + " dbtable '%s',"
  7. + " user '%s',"
  8. + " password '%s'"
  9. + ")";

根据表schema信息拼接DDL

  1. public String generateJdbcDdlSql(String database, String table) {
  2. StringBuilder ddl = new StringBuilder();
  3. ddl.append("CREATE TABLE ").append(String.format("%s.%s", database, table)).append(" (");
  4. try {
  5. List < MetaColumnInfo > columns = getColumns(database, table);
  6. if (CollectionUtils.isNotEmpty(columns)) {
  7. for (MetaColumnInfo column: columns) {
  8. ddl.append("\n\t").append(column.getName()).append(" ").append(column.getType());
  9. if (column.getLength() > 0) {
  10. ddl.append("(").append(column.getLength()).append(")");
  11. }
  12. if (!column.isNullable()) {
  13. ddl.append(" NOT NULL");
  14. }
  15. ddl.append(",");
  16. }
  17. String primaryKeys =
  18. columns.stream()
  19. .filter(MetaColumnInfo::isPrimaryKey)
  20. .map(MetaColumnInfo::getName)
  21. .collect(Collectors.joining(", "));
  22. if (StringUtils.isNotBlank(primaryKeys)) {
  23. ddl.append(String.format("\n\tPRIMARY KEY (%s),", primaryKeys));
  24. }
  25. ddl.deleteCharAt(ddl.length() - 1);
  26. }
  27. } catch (Exception e) {
  28. LOG.warn("Fail to get Sql columns(获取字段列表失败)");
  29. }
  30. ddl.append("\n)");
  31. return ddl.toString();
  32. }

部分数据源支持直接获取DDL

mysql

  1. SHOW CREATE TABLE 'table'

oracle

  1. SELECT DBMS_METADATA.GET_DDL('TABLE', 'table', 'database') AS DDL FROM DUAL