HBase常用的数据库API操作

php中文网
发布: 2016-06-07 17:53:14
原创
1455人浏览过

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操作:

明日B2C电子商务系统源码
明日B2C电子商务系统源码

对于典型的数据库管理系统,尤其是B2C电子商务网站这样数据流量比较大的网络管理系统,必须要满足使用方便、操作灵活等设计需求。

明日B2C电子商务系统源码 0
查看详情 明日B2C电子商务系统源码

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

 

  测试:

最佳 Windows 性能的顶级免费优化软件
最佳 Windows 性能的顶级免费优化软件

每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。

下载
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号