【问题标题】:mysql + Azkaban: Reading "LongBlob"mysql + Azkaban:阅读“LongBlob”
【发布时间】:2015-03-17 04:51:41
【问题描述】:

我正在尝试在“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¦¥..."

我曾尝试像这样修改查询:

"select exec_id, CONVERT(log using latin1) from execution_logs"

但还是同样的问题。

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

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

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

谢谢


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

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

但是如何以编程方式执行此操作?

【问题讨论】:

    标签: java mysql blob azkaban


    【解决方案1】:

    所以在深入研究了 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;
            }
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2012-02-15
      • 2016-02-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-01-05
      • 2013-04-17
      相关资源
      最近更新 更多