我试图在'azkaban‘数据库上构建一个查询层。(所使用的语言: Java)我遇到的是一个简单的问题(但结果却令人恼火)。
这是我正在运行的查询:
select exec_id, CONVERT(log USING latin1)from execution_logs"log“是'longblob‘类型的列
这是我阅读“log”s的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,从execution_logs转换(使用latin1的日志)”
但问题仍然是一样的。
我尝试了"utf8",但是当我这样做时,我在ResultSet的"log“列中获得NULL。
如果有人有这方面的经验或知道如何解决这个问题,请提供帮助?
在此期间,我将继续尝试。
谢谢
仍然在尝试:我现在正在使用xampp (只是做快速原型)。
在phpmyadmin UI中,当我单击blob时,它会下载一个'.bin‘文件。在mac上,我可以像预期的那样打开这个文件并看到正确的"English“单词(或英文日志)。
但如何通过编程实现这一点呢?
发布于 2015-03-01 23:38:17
因此,在深入研究了azkaban之后,我发现在azkaban数据库中应该如何查询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;
}
}和EncodingType:
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;
}
}
}https://stackoverflow.com/questions/28791938
复制相似问题