I put one row with a jpg file stored to a column into hbase,but when I get the row from hbase,I find the file size is not the same with the file that I put into hbase.why?
My hbase is in centos 64,my code and the jpg file are in macos.
lovedeMacBook-Pro:~ love$ ls -lh /Users/love/Documents/vpn*
-rw-r-----@ 1 love staff 8.5K 8 24 09:59 /Users/love/Documents/vpn.jpg
lovedeMacBook-Pro:~ love$ ls -lh /Users/love/output.jpg
-rw-r--r--@ 1 love staff 8.0K 8 24 13:41 /Users/love/output.jpg我的代码是:
public class Hadoop_test {
//function to convert image file to bytes.
public static byte[] extractBytes(String ImageName) throws IOException {
File file = new File(ImageName);
BufferedImage originalImage = ImageIO.read(file);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ImageIO.write(originalImage, "jpg", baos);
byte[] imageInByte = baos.toByteArray();
return imageInByte;
}
public static void main(String[] args) throws Exception{
Configuration conf = HBaseConfiguration.create();
String krbstr = Thread.currentThread().getContextClassLoader().getResource("krb.conf").getFile();
System.setProperty("java.security.krb5.conf", krbstr);
UserGroupInformation.setConfiguration(conf);
String keytab = Thread.currentThread().getContextClassLoader().getResource("love.keytab").getFile();
UserGroupInformation.loginUserFromKeytab("love/lovedeMacBook-Pro.local@for_hadoop", keytab);
TableName table_name = null;
Admin admin = null;
Connection connection = null;
connection = ConnectionFactory.createConnection(conf);
admin = connection.getAdmin();
table_name = TableName.valueOf("users");
if(admin.tableExists(table_name)){
System.out.println("table exist");
}
Put put = new Put("row3".getBytes());
put.addColumn("info".getBytes(), "image".getBytes(), extractBytes("/Users/love/Documents/vpn.jpg"));
connection.getTable(TableName.valueOf(table_name.getName())).put(put);
//get image from hbase
Get get = new Get(Bytes.toBytes("row3"));
Result result = connection.getTable(TableName.valueOf(table_name.getName())).get(get);
byte[] arr = result.getValue("info".getBytes(), "image".getBytes());
OutputStream out = new BufferedOutputStream(new FileOutputStream(
"/Users/love/output.jpg"));
out.write(arr);
out.close();
}
}
答案 0 :(得分:1)
尝试将方法extractBytes
替换为:
import org.apache.commons.io.IOUtils;
...
public static byte[] extractBytes(String ImageName) throws IOException {
return IOUtils.toByteArray(new FileInputStream(ImageName));
}
原始图像不会进行解码/编码,因此输出文件的大小应与输入的大小相同。