java - mysql + 阿兹卡类 : Reading "LongBlob"

标签 java mysql blob azkaban

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

这是我正在运行的查询:

select exec_id, CONVERT(log USING latin1)from execution_logs

“log”是“longblob”类型的列

这是我用于读取“log”的 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¦¥..."

我尝试像这样修改查询:

“从执行日志中选择 exec_id,CONVERT(使用 latin1 进行日志记录)”

但还是同样的问题。

我尝试了“utf8”,但是当我这样做时,结果集的“log”列中出现 NULL。

如果有人经历过此问题或知道如何解决此问题,请帮忙?

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

谢谢

<小时/>

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

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

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

最佳答案

经过深入研究 azkaban,我发现这就是在 azkaban-Database 中查询 LongBlob 的方式:

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;
        }
    }
}

关于java - mysql + 阿兹卡类 : Reading "LongBlob",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28791938/

相关文章:

php - 根据时间或两个日期从 Php/Mysql 中的表中选择行?

.net - 将 blob 存储在 SQL Server 中而不将 blob 读入内存

MySQL 检查 BLOB 是否为有效的 UTF-8

java - 进程 'command '/Applications/Android Studio 3.0.1.app/Contents/jre/jdk/Contents/Home/bin/java'' 以非零退出值 1 完成

MySQL在 'WHERE'子句中加入多个范围

mysql - SQL:从最后 N(两个)条目中获取平均结果

mysql - 使用asp.net和c#将图像存储到byte[]到Mysql中

java - 从抓取中删除 header

java - 使用模式在 Java 中解析字符串

java - JAVA 中 Streaming API 的最佳搜索算法