视频1 视频21 视频41 视频61 视频文章1 视频文章21 视频文章41 视频文章61 推荐1 推荐3 推荐5 推荐7 推荐9 推荐11 推荐13 推荐15 推荐17 推荐19 推荐21 推荐23 推荐25 推荐27 推荐29 推荐31 推荐33 推荐35 推荐37 推荐39 推荐41 推荐43 推荐45 推荐47 推荐49 关键词1 关键词101 关键词201 关键词301 关键词401 关键词501 关键词601 关键词701 关键词801 关键词901 关键词1001 关键词1101 关键词1201 关键词1301 关键词1401 关键词1501 关键词1601 关键词1701 关键词1801 关键词1901 视频扩展1 视频扩展6 视频扩展11 视频扩展16 文章1 文章201 文章401 文章601 文章801 文章1001 资讯1 资讯501 资讯1001 资讯1501 标签1 标签501 标签1001 关键词1 关键词501 关键词1001 关键词1501 专题2001
HBase常用的数据库API操作
2020-11-09 09:51:50 责编:小采
文档


HBase常用的数据库API操作Posted on 需要引入Hadoop和Hbase的jar包,我这里HBase用的是hbase-0.90.5版本,所以我这里引入的HBase的jar包是hbase-0.90.5.jar和zookeeper-3.3.2.jar。 一些常用的API操作: package cn.luxh.app.util; import java.io.IOExcepti

HBase常用的数据库API操作 Posted on

  需要引入Hadoop和Hbase的jar包,我这里HBase用的是hbase-0.90.5版本,美国空间,所以我这里引入的HBase的jar包是hbase-0.90.5.jar和zookeeper-3.3.2.jar。

  一些常用的API操作:

package cn.luxh.app.util; import java.io.IOException; import java.util.Arrays; 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.MasterNotRunningException; import org.apache.hadoop.hbase.ZooKeeperConnectionException; 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.filter.CompareFilter.CompareOp; import org.apache.hadoop.hbase.filter.Filter; import org.apache.hadoop.hbase.filter.FilterList; import org.apache.hadoop.hbase.filter.FilterList.Operator; import org.apache.hadoop.hbase.filter.SingleColumnValueFilter; import org.apache.hadoop.hbase.util.Bytes; public class HBaseUtil { /** * 初始化HBase的配置文件 * Configuration getConfiguration(){ Configuration conf = HBaseConfiguration.create(); //和hbase-site.xml中配置的一致 conf.set("hbase.zooker.quorum", "h1,h2,h2"); return conf; } /** * 实例化HBaseAdmin,HBaseAdmin用于对表的元素据进行操作 * @return * @throws MasterNotRunningException * @throws ZooKeeperConnectionException HBaseAdmin getHBaseAdmin() throws MasterNotRunningException, ZooKeeperConnectionException{ return new HBaseAdmin(getConfiguration()); } /** * 创建表 * @param tableName 表名 * @param columnFamilies 列族 * @throws IOException createTable(String tableName,String...columnFamilies) throws IOException { HTableDescriptor htd = new HTableDescriptor(tableName.getBytes());// for(String fc : columnFamilies) { htd.addFamily(new HColumnDescriptor(fc)); } getHBaseAdmin().createTable(htd); } /** * 获取HTableDescriptor * @param tableName * @return * @throws IOException HTableDescriptor getHTableDescriptor(byte[] tableName) throws IOException{ return getHBaseAdmin().getTableDescriptor(tableName); } /** * 获取表 * @param tableName 表名 * @return * @throws IOException HTable getHTable(String tableName) throws IOException{ return new HTable(getConfiguration(),tableName); } /** * 获取Put,Put是插入一行数据的封装格式 * @param tableName * @param row * @param columnFamily * @param qualifier * @param value * @return * @throws IOException Put getPut(String row,String columnFamily,String qualifier,String value) throws IOException{ Put put = new Put(row.getBytes()); if(qualifier==null||"".equals(qualifier)) { put.add(columnFamily.getBytes(), null, value.getBytes()); }else { put.add(columnFamily.getBytes(), qualifier.getBytes(), value.getBytes()); } return put; } /** * 查询某一行的数据 * @param tableName 表名 * @param row 行键 * @return * @throws IOException Result getResult(String tableName,String row) throws IOException { Get get = new Get(row.getBytes()); HTable htable = getHTable(tableName); Result result = htable.get(get); htable.close(); return result; } /** * 条件查询 * @param tableName 表名 * @param columnFamily 列族 * @param queryCondition 查询条件值 * @param begin 查询的起始行 * @param end 查询的终止行 * @return * @throws IOException ResultScanner getResultScanner(String tableName,String columnFamily,String queryCondition,String begin,String end) throws IOException{ Scan scan = new Scan(); //设置起始行 scan.setStartRow(Bytes.toBytes(begin)); //设置终止行 scan.setStopRow(Bytes.toBytes(end)); //指定要查询的列族 scan.addColumn(Bytes.toBytes(columnFamily),null); //查询列族中值等于queryCondition的记录 Filter filter1 = new SingleColumnValueFilter(Bytes.toBytes(columnFamily),null,CompareOp.EQUAL,Bytes.toBytes(queryCondition)); //Filter filter2 = new SingleColumnValueFilter(Bytes.toBytes(columnFamily),null,CompareOp.EQUAL,Bytes.toBytes("chuliuxiang")); FilterList filterList = new FilterList(Operator.MUST_PASS_ONE,Arrays.asList(filter1)); scan.setFilter(filterList); HTable htable = getHTable(tableName); ResultScanner rs = htable.getScanner(scan); htable.close(); return rs; } }

  测试:

下载本文
显示全文
专题