Mysql注释添加到Wiki-Confluence

本文介绍了一个需求,即如何将Metabase中宽表字段的注释批量导入Confluence Wiki,以简化查询和维护工作。通过创建文章、获取版本号、解析JSON、整合数据库注释和执行脚本等步骤,实现了自动化处理。提供了项目GitHub地址和Confluence接口参考链接。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

最近收到一个需求,要把Metabase里所有宽表的字段注释写入到Wiki(我们的Wiki是Confluence搭建的),以方便查找。近期经济不好,公司人员变动也比较大,辛苦了新来的同学们,确实不知道从哪查字段意义。但是Metabase所有表加起来有近千个字段了,写Wiki要耗费大量时间,并且以后每次修改还要维护,想想就头大,于是想着改成全都自动化地来处理这个事情,先看一下最后的效果:

每天早上7点自动跑一遍,已经完全满足需求了。下面说一下具体实现:

1. 创建一篇文章,注意标题要有汉字,不然pageId出不来。文章创建好之后,后面只做修改就可以。

2. 在shell里获取到现有内容,并写入到文件content.json,实际目的是获取到版本号VersionID。在目录的stash.sh文件。

因为Confluence调用接口修改的话,要传VersionID的,并且只能在现有的VersionID上加1,所以必须要得到版本号。

#获取到文章内容
getjson=`curl -u yourusername:yourpassword http://192.168.1.1:8090/rest/api/content/14123063`
content=${getjson}
#把内容写入到文件
echo "$content" > content.json

文章内容写入到文件是为了方便后面处理,shell解析Json毕竟不擅长。

3. 在Java中解析文件content.json,并获致到VersionID。在代码的JsonUtil文件

    /**
     * 加过之后的Version
     * @return
     */
    public static String getVersion( ) {
        String content = getContent();
        String jsonStr = content.replace("\\", "");
        JSONObject jsonObject = JSON.parseObject(jsonStr);
        jsonObject = jsonObject.getJSONObject("version");
        return String.valueOf(Integer.parseInt(jsonObject.get("number").toString()) + 1);
    }

    /**
     * 获取文件内容
     * @return
     */
    public static String getContent() {
        try {
            BufferedReader in = new BufferedReader(new FileReader("content.json"));
            String str = in.readLine();
            return str;
        } catch (IOException e) {
            e.printStackTrace();
        } finally {

        }
        return "";
    }

4. 获取到数据库注释信息,并将注释信息和版本号VersionID写入到write.sh, 在项目的DatabaseUtil文件内

    /**
     * 获取数据库下的所有表名和字段注释
     */
    public static List<String> getTableInfo() {
        List<String> tableNames = new ArrayList<>();
        Connection conn = getConnection();
        ResultSet rs = null;
        try {
            //打开文件
            File file =new File("write.sh");
            if(!file.exists()){
                file.createNewFile();
            }
            FileWriter fileWritter = new FileWriter(file.getName());
            fileWritter.write("curl -u yourusername:yourpassword -X PUT -H 'Content-Type: application/json' -d '{\"id\":\"14123063\",\"type\":\"page\", " +
                    "\"title\":\"pivot层\", \"space\":{\"key\":\"BI\"}, " +
                    "\"body\":{\"storage\":{\"value\": " +
                    "\"");

            //获取数据库的元数据
            DatabaseMetaData db = conn.getMetaData();
            //从元数据中获取到所有的表名
            rs = db.getTables(null, null, null, new String[]{"TABLE"});
            while (rs.next()) {
                //获取表名
                String tableName = rs.getString(3);
                String tableComment = rs.getString(5);
                System.out.println("<h1>" + tableName + "     " + tableComment + "</h1>");
                fileWritter.write("<h1>" + tableName + "     " + tableComment + "</h1>");
                //获取字段名
                PreparedStatement pStemt = null;
                String tableSql = SQL + tableName;
                //表列注释
                List<String> columnComments = new ArrayList<>();//列名注释集合
                ResultSet crs = null;
                pStemt = conn.prepareStatement(tableSql);
                crs = pStemt.executeQuery("show full columns from " + tableName);
                while (crs.next()) {
                    String comment = crs.getString("Comment");
                    comment = comment.replace("\r\n", "");
                    columnComments.add(comment);
                }

                pStemt = conn.prepareStatement(tableSql);
                //结果集元数据
                ResultSetMetaData rsmd = pStemt.getMetaData();
                //表列数
                fileWritter.write("<table>");
                int size = rsmd.getColumnCount();
                for (int i = 0; i < size; i++) {
                    fileWritter.write("<tr>");
                    String columnName = rsmd.getColumnName(i + 1);
                    String columnTypeName = rsmd.getColumnTypeName(i + 1);
                    System.out.println(columnName + "    " + columnTypeName + "    " + columnComments.get(i));
                    fileWritter.write("<td>" + columnName + "</td>");
                    fileWritter.write("<td>" + columnTypeName + "</td>");
                    fileWritter.write("<td>" + columnComments.get(i) + "</td>");
                    fileWritter.write("</tr>");
                }
                fileWritter.write("</table>");

            }

            fileWritter.write("\", " +
                    "\"representation\":\"storage\"}}, " +
                    "\"version\":{\"number\":"
                    + JsonUtil.getVersion() +
                    "}}'  http://192.168.1.1:8090/rest/api/content/14123063"
            );


            fileWritter.flush();
            fileWritter.close();
        } catch (SQLException e) {
            LOGGER.error("getTableNames failure", e);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                rs.close();
                closeConnection(conn);
            } catch (SQLException e) {
                LOGGER.error("close ResultSet failure", e);
            }
        }
        return tableNames;
    }

5. 执行write.sh,写入Confluence,完毕。

 

参考项目地址:https://github.com/wangfei0904306/wikison/tree/master/wikison

1. 主起动文件在项目wikison/wikison/start.sh

2.DatabaseUtil有数据库连接地址、用户名和密码需要配置

3. 完全自动化需要在服务器上加定时任务脚本,执行时CD到当前目录,比较简单不述

4. 代码没整理,代码量比较小经济又不好,就这样吧

Confluence接口说明地址:https://developer.atlassian.com/server/confluence/confluence-rest-api-examples/

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值