hbase存取图片,
package hbase.test;
import java.io.IOException;
import org.apache.commons.io.FileUtils;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.HColumnDescriptor;
import org.apache.hadoop.hbase.HTableDescriptor;
import org.apache.hadoop.hbase.KeyValue;
import org.apache.hadoop.hbase.MasterNotRunningException;
import org.apache.hadoop.hbase.ZooKeeperConnectionException;
import org.apache.hadoop.hbase.client.Delete;
import org.apache.hadoop.hbase.client.Get;
import org.apache.hadoop.hbase.client.HBaseAdmin;
import org.apache.hadoop.hbase.client.HTable;
import org.apache.hadoop.hbase.client.Put;
import org.apache.hadoop.hbase.client.Result;
import org.apache.hadoop.hbase.client.ResultScanner;
import org.apache.hadoop.hbase.client.Scan;
import org.apache.hadoop.hbase.util.Bytes;
public class HbaseTest {
static Configuration configuration = null;
static {
configuration = HBaseConfiguration.create();
}
//存储图片
/**
* @param tableName 表名
* @param rowkey 行号
* @param colFamily 列簇
* @param colName 列名
* @param bs 列值,此处为图片
* @param imgType
* 图片类型,读取图片时使用,图片类型的列名指定为imageType,imgType这里指的是列名为imageType的列值
*/
public static void storeImage(String tableName, String rowkey, String colFamily, String colName, byte[] bs, String imgType) throws IOException {
HTable table = new HTable(configuration, tableName);
java.util.List<Put> puts = new java.util.ArrayList<Put>();
//存二进制图片
Put put = new Put(Bytes.toBytes(rowkey));
put.add(Bytes.toBytes(colFamily), Bytes.toBytes(colName), bs);
//存图片类型
Put putx = new Put(Bytes.toBytes(rowkey));
putx.add(Bytes.toBytes(colFamily), Bytes.toBytes("imageType"), Bytes.toBytes(imgType));
puts.add(put);
puts.add(putx);
//put.a
//table.put(put);
table.put(puts);
table.close();
}
//读取图片
public static void getImage(String tableName, String rowkey) throws IOException {
HTable table = new HTable(configuration, tableName);
Get get = new Get(Bytes.toBytes(rowkey));
Result result = table.get(get);
for (KeyValue k : result.raw()) {
String col = Bytes.toStringBinary(k.getQualifier());
System.out.println("===k:String===>>:" + col);
if (col.equals("imgContent")) {
byte[] bs = k.getValue();
FileUtils.writeByteArrayToFile(new java.io.File("/home/zcwangjb/document/hbase.jpg"), bs);
}
}
table.close();
}
//创建表并指定列簇
public static void createTable(String tableName, String cols[]) throws MasterNotRunningException, ZooKeeperConnectionException, IOException {
HBaseAdmin baseAdmin = new HBaseAdmin(configuration);
if (baseAdmin.tableExists(tableName)) {
System.out.println("表【" + tableName + "】存在");
} else {
HTableDescriptor tableDescriptor = new HTableDescriptor(tableName);
for (String s : cols) {
HColumnDescriptor columnDescriptor = new HColumnDescriptor(s);
tableDescriptor.addFamily(columnDescriptor);
}
baseAdmin.createTable(tableDescriptor);
baseAdmin.close();
}
}
//删除表
public static void deleteTable(String tableName) throws MasterNotRunningException, ZooKeeperConnectionException, IOException {
HBaseAdmin admin = new HBaseAdmin(configuration);
if (admin.tableExists(tableName)) {
admin.disableTable(tableName);
admin.deleteTable(tableName);
}
}
//添加数据
/**
* @param tableName 表名
* @param rowkey 行号
* @param colFamily 列簇
* @param column 列
* @param value 列值
*/
public static void insertData(String tableName, String rowkey, String colFamily, String column, String value) throws IOException {
HTable table = new HTable(configuration, tableName);
Put put = new Put(Bytes.toBytes(rowkey));
put.add(Bytes.toBytes(colFamily), Bytes.toBytes(column), Bytes.toBytes(value));
table.put(put);
table.close();
}
//查询一条数据
public static void getOneByeRowkey(String tableName, String rowkey) throws IOException {
HTable table = new HTable(configuration, tableName);
Get get = new Get(Bytes.toBytes(rowkey));
Result result = table.get(get);
for (KeyValue k : result.raw()) {
System.out.println("行号:" + Bytes.toStringBinary(k.getRow()));
System.out.println("时间戳:" + k.getTimestamp());
System.out.println("列簇:" + Bytes.toStringBinary(k.getFamily()));
System.out.println("列:" + Bytes.toStringBinary(k.getQualifier()));
System.out.println("值:" + Bytes.toString(k.getValue()));
}
table.close();
}
//查询指定数据
public static void scanData(String tableName) throws IOException {
HTable table = new HTable(configuration, tableName);
Scan scan = new Scan();
ResultScanner rs = table.getScanner(scan);
for (Result result : rs) {
for (KeyValue k : result.raw()) {
System.out.println("行号:" + Bytes.toStringBinary(k.getRow()));
System.out.println("时间戳:" + k.getTimestamp());
System.out.println("列簇:" + Bytes.toStringBinary(k.getFamily()));
System.out.println("列:" + Bytes.toStringBinary(k.getQualifier()));
System.out.println("值:" + Bytes.toString(k.getValue()));
}
}
table.close();
}
//删除一条数据
@SuppressWarnings("empty-statement")
public static void deleteByRow(String tableName, String rowkey) throws IOException {
HTable table = new HTable(configuration, tableName);
//删除一条数据
Delete delete = new Delete(Bytes.toBytes(rowkey));
table.delete(delete);
//删除多条数据
String[] rowkeys = {};
java.util.List<Delete> list = new java.util.ArrayList<Delete>();
for (String rk : rowkeys) {
Delete d = new Delete(Bytes.toBytes(rk));
list.add(d);
}
table.delete(list);
//
table.close();
}
public static void main(String[] args) throws ZooKeeperConnectionException, IOException {
String tableName = "mytest";
String cols[] = {"a", "b", "c"};
// createTable(tableName, cols);
// insertData(tableName, "1", "a", "abc", "11111");
// getOneByeRowkey(tableName, "1");
//scanData(tableName);
// deleteByRow(tableName, "1");
// scanData(tableName);
//getImage(tableName, "1");
// String imgType = ".jpg";
// byte[] bs = null;
// java.io.InputStream is = new java.io.FileInputStream(new File("/home/zcwangjb/document/solr.jpg"));
// bs = IOUtils.toByteArray(is);
// storeImage(tableName, "1", "a", "imgContent", bs, imgType);
getImage(tableName, "1");
}
}
本站文章为和通数据库网友分享或者投稿,欢迎任何形式的转载,但请务必注明出处.
同时文章内容如有侵犯了您的权益,请联系QQ:970679559,我们会在尽快处理。