0

我正在尝试在“azkaban”数据库上构建一个查询层。(使用的语言:Java)我遇到了,我认为这是一个简单的问题(但结果很烦人)。

这是我正在运行的查询:

select exec_id, CONVERT(log USING latin1)from execution_logs

“log”是“longblob”类型的列

这是我用于读取“日志”的 Java 代码:

try {
    Connection conn = AzkabanClient.getPhoenixConnection(conf);
    String s = " select exec_id,  log from execution_logs ";
    PreparedStatement pstmt = conn.prepareStatement(s);
    ResultSet rs = pstmt.executeQuery();
    String logString="";
    while(rs.next()){
        int i = rs.getInt("exec_id");
        InputStream inputStream = rs.getBinaryStream("log");
        java.io.BufferedReader in = new BufferedReader(new java.io.InputStreamReader(inputStream));
        String str;
        while ((str = in.readLine()) != null) {
            logString += str;
        }
        inputStream.close();
    }
    conn.close();
}catch(Exception e){
    LOGGER.error("Error =>" + e);
}

这里的问题是:在while循环结束时,我能够读取表中一行的“日志”,但字符串不可读(编码?)

例如:

logString = "‹Å\]Ç•}^ÿ>°]ÕÕÝUzY‰”Uà8Žììbg¦¥..."

我试图像这样修改查询:

“从 execution_logs 中选择 exec_id,转换(使用 latin1 的日志)”

但仍然是同样的问题。

我尝试了“utf8”,但是当我这样做时,我在 ResultSet 的“log”列中得到 NULL。

如果有人有此经验或知道如何解决此问题,请提供帮助?

与此同时,我会继续尝试。

谢谢


仍在尝试:我现在正在使用 xampp(只是为了进行快速原型制作)。

在 phpmyadmin UI 中,当我单击 blob 时,它会下载一个“.bin”文件。在 Mac 上,我可以按预期打开此文件并查看正确的“英文”单词(或英文日志)。

但是如何以编程方式做到这一点?

4

1 回答 1

0

因此,在深入研究 azkaban 之后,我发现这就是在 azkaban-Database 中查询 LongBlobs 的方式:

public String getErrorLog(){
    String returnString = "";
            try {
                Connection conn = AzkabanClient.getPhoenixConnection(conf);
                String s = " select exec_id, enc_type,  log from execution_logs where exec_id = 3964 and name = 'http-time-series-hourly' ";
                PreparedStatement pstmt = conn.prepareStatement(s);
                ResultSet rs = pstmt.executeQuery();
                while (rs.next()) {
                    int i = rs.getInt("exec_id");
                    ByteArrayOutputStream byteStream = new ByteArrayOutputStream();
                    EncodingType encType = EncodingType.fromInteger(rs.getInt("enc_type"));
                    int debug = 0;
                    byte[] data = rs.getBytes("log");
                    try {
                        byte[] buffer = data;
                        ByteArrayOutputStream byteArrayOutputStream = null;
                        if (encType == EncodingType.GZIP) {
                            byteArrayOutputStream = GZIPUtils.unGzipBytesOutputStream(data);
                        }

                        returnString = new String(byteArrayOutputStream.toByteArray(), "UTF-8");
                    } catch (IOException e) {
                        throw new SQLException(e);
                    }
                }
                conn.close();
            } catch (Exception e) {
                LOGGER.error("Error =>" + e);
            }

            return returnString;
}

在哪里 :

GZIPUtils 是:

public class GZIPUtils {

    public static ByteArrayOutputStream unGzipBytesOutputStream(byte[] bytes) throws IOException {
        ByteArrayInputStream byteInputStream = new ByteArrayInputStream(bytes);
        GZIPInputStream gzipInputStream = new GZIPInputStream(byteInputStream);

        ByteArrayOutputStream byteOutputStream = new ByteArrayOutputStream();
        IOUtils.copy(gzipInputStream, byteOutputStream);

        return byteOutputStream;
}
}

和编码类型:

public static enum EncodingType {
    PLAIN(1), GZIP(2);

    private int numVal;

    EncodingType(int numVal) {
        this.numVal = numVal;
    }

    public int getNumVal() {
        return numVal;
    }

    public static EncodingType fromInteger(int x) {
        switch (x) {
            case 1:
                return PLAIN;
            case 2:
                return GZIP;
            default:
                return PLAIN;
        }
    }
}
于 2015-03-01T23:38:17.960 回答