hbase客户端api实例
来源:互联网 发布:礼仪之邦 知乎 编辑:程序博客网 时间:2024/04/29 20:52
需要的jar包:
hbase-0.96.2-hadoop2\lib\所有jar包
main():创建一张表
testPut():一次往一行中插入数据
testPuts():一次往多行中插入数据
testDel():删除数据
testUpdate():修改数据
testScan():数据查询
testGet():数据查询
testFilter():过滤器查询
代码:
public class HbaseDaoDemo {HTable t_buy_cart = null;@Beforepublic void getConn() throws Exception {Configuration conf = HBaseConfiguration.create();conf.set("hbase.zookeeper.quorum", "master:2181, slave01:2181");// 往表中插入数据,首先要拿到一个表的客户端连接对象DMLt_buy_cart = new HTable(conf, "t_buy_cart");}/** * 一次往一行中插入数据 */@Testpublic void testPut() throws Exception {// 先将我们需要插入的数据封装成Put对象Put put = new Put(Bytes.toBytes("user_01"));// 指定要插入的列族为"product",插入的数据的字段名为"product_id",字段值为"pd-001-007"put.add(Bytes.toBytes("product"), Bytes.toBytes("product_id"), Bytes.toBytes("pd-001-007"));put.add(Bytes.toBytes("product"), Bytes.toBytes("product_num"), Bytes.toBytes(5));put.add(Bytes.toBytes("recommend"), Bytes.toBytes("product_id"), Bytes.toBytes("pd-001-008"));// 用表的客户端连接对象t_buy_cart插入数据t_buy_cart.put(put);// 关闭客户端连接t_buy_cart.close();}/** * 一次往多行中插入数据 */@Testpublic void testPuts() throws IOException {// 构造一行数据Put put = new Put(Bytes.toBytes("user_02"));put.add(Bytes.toBytes("product"), Bytes.toBytes("product_id_01"), Bytes.toBytes("pd-012-011"));put.add(Bytes.toBytes("product"), Bytes.toBytes("product_num_01"), Bytes.toBytes(4));put.add(Bytes.toBytes("product"), Bytes.toBytes("product_id_02"), Bytes.toBytes("pd-012-013"));put.add(Bytes.toBytes("product"), Bytes.toBytes("product_num_02"), Bytes.toBytes(2));put.add(Bytes.toBytes("recommend"), Bytes.toBytes("product_id"), Bytes.toBytes("pd-012-008"));// 构造第二行数据Put put2 = new Put(Bytes.toBytes("user_03"));put2.add(Bytes.toBytes("product"), Bytes.toBytes("product_id_01"), Bytes.toBytes("pd-112-050"));put2.add(Bytes.toBytes("product"), Bytes.toBytes("product_num_01"), Bytes.toBytes(2));put2.add(Bytes.toBytes("product"), Bytes.toBytes("product_id_02"), Bytes.toBytes("pd-112-060"));put2.add(Bytes.toBytes("product"), Bytes.toBytes("product_num_02"), Bytes.toBytes(8));put2.add(Bytes.toBytes("recommend"), Bytes.toBytes("product_id"), Bytes.toBytes("pd-112-090"));// 构造一个list,用来封装多行数据put对象ArrayList<Put> puts = new ArrayList<Put>();puts.add(put);puts.add(put2);// 将list中的所有数据批量插入到表中t_buy_cart.put(puts);t_buy_cart.close();}/** * 删除数据 */@Testpublic void testDel() throws Exception {// 指定要删除的行Delete delete = new Delete(Bytes.toBytes("user_02"));// 指定要删除的列delete.deleteColumn(Bytes.toBytes("product"), Bytes.toBytes("product_num_01"));// 用客户端把delete所指定的数据删除// 指定要删除的行Delete delete2 = new Delete(Bytes.toBytes("user_03"));// 指定要删除的列delete2.deleteColumn(Bytes.toBytes("product"), Bytes.toBytes("product_num_01"));// 用客户端把delete所指定的数据删除// 将多行要删除的数据delete对象封装到一个list中ArrayList<Delete> deletes = new ArrayList<Delete>();deletes.add(delete);deletes.add(delete2);// 将deletes列表中指定的delete对象所描述的数据从表中删除t_buy_cart.delete(deletes);t_buy_cart.close();}/** * 在hbase中,修改数据就是put数据 */@Testpublic void testUpdate() throws IOException {// 指定要修改的行Put put = new Put(Bytes.toBytes("user_03"));// 指定要修改的列及新的值put.add(Bytes.toBytes("product"), Bytes.toBytes("product_num_02"), Bytes.toBytes(18));// 把要修的数据封装对象put更新(重新插入)到表中t_buy_cart.put(put);t_buy_cart.close();}/** * scan可以对一个指定的行的起止范围进行数据查询 */@Testpublic void testScan() throws IOException {// 封装scan查询的参数// 注意:scan的起止范围,含头不含尾Scan scan = new Scan(Bytes.toBytes("user_02"), Bytes.toBytes("user_03"));ResultScanner scanner = t_buy_cart.getScanner(scan);Iterator<Result> iterator = scanner.iterator();while (iterator.hasNext()) {Result result = iterator.next();byte[] p1bytes = result.getValue(Bytes.toBytes("product"), Bytes.toBytes("product_id_01"));byte[] p2bytes = result.getValue(Bytes.toBytes("product"), Bytes.toBytes("product_id_02"));byte[] n1bytes = result.getValue(Bytes.toBytes("product"), Bytes.toBytes("product_num_01"));byte[] n2bytes = result.getValue(Bytes.toBytes("product"), Bytes.toBytes("product_num_02"));System.out.println(Bytes.toString(p1bytes));System.out.println(Bytes.toInt(n1bytes));System.out.println(Bytes.toString(p2bytes));System.out.println(Bytes.toInt(n2bytes));}t_buy_cart.close();}/** * get查询 */@Testpublic void testGet() throws Exception {// 指定要查询的行Get get = new Get(Bytes.toBytes("user_03"));// 指定要查询的列get.addColumn(Bytes.toBytes("product"), Bytes.toBytes("product_id_01"));get.addColumn(Bytes.toBytes("product"), Bytes.toBytes("product_id_02"));get.addColumn(Bytes.toBytes("product"), Bytes.toBytes("product_num_02"));Result result = t_buy_cart.get(get);byte[] p1bytes = result.getValue(Bytes.toBytes("product"), Bytes.toBytes("product_id_01"));byte[] p2bytes = result.getValue(Bytes.toBytes("product"), Bytes.toBytes("product_id_02"));byte[] n2bytes = result.getValue(Bytes.toBytes("product"), Bytes.toBytes("product_num_02"));System.out.println(Bytes.toString(p1bytes));System.out.println(Bytes.toString(p2bytes));// 注意:product_num_02列的值value类型在插入数据时是intSystem.out.println(Bytes.toInt(n2bytes));t_buy_cart.close();}/** * 过滤器查询 过滤器可以为查询设定更多更复杂的条件 过滤器是通过设置在Get或者Scan查询参数中生效 */@Testpublic void testFilter() throws Exception {Scan scan = new Scan(Bytes.toBytes("user_01"), Bytes.toBytes("user_03"));//指定scan所扫描的列族scan.addFamily(Bytes.toBytes("product"));// 1、构造一个查询条件过滤器//前缀过滤器:针对行键的前缀进行过滤,符合指定前缀的行会返回,否则,被过滤掉Filter filter = new PrefixFilter(Bytes.toBytes("user"));//构造一个比较值(比较器),BinaryComparator这种比较值用来做等值比较ByteArrayComparable rowComparator =new BinaryComparator(Bytes.toBytes("user_01"));//2、返回那些行键等于"user_01"的行RowFilter rowFilter = new RowFilter(CompareOp.EQUAL, rowComparator);//指定比较值SubstringComparator comparator = new SubstringComparator("-012-");//3、构造一个针对特定列的值的过滤器//返回那些product:product_id_01列中值包含子串"-012-"的数据//如果行中不包含过滤条件中所指定的列,默认情况下也会返回SingleColumnValueFilter columnValueFilter = new SingleColumnValueFilter(Bytes.toBytes("product"), Bytes.toBytes("product_id_01"), CompareOp.EQUAL, comparator);//如果要过滤掉那些不包含指定列的行,则要显示指定一个参数columnValueFilter.setFilterIfMissing(true);// 往scan参数对象中添加过滤器//scan.setFilter(filter);//scan.setFilter(rowFilter);scan.setFilter(columnValueFilter);ResultScanner scanner = t_buy_cart.getScanner(scan);Iterator<Result> iterator = scanner.iterator();while (iterator.hasNext()) {System.out.println(iterator.next());}}/* * main方法,创建一张表 */public static void main(String[] args) throws Exception {/* * 通过这种通用的配置对象构造方法来创建一个配置对象 * 这种方法会自动加载classpath下的core-site.xml,hdfs-site.xml,core-default.xml...等这些hadoop的配置文件 * Configuration conf = new Configuration(); */// HBaseConfiguration.create()则会自动加载classpath下的hadoop的配置文件以及hbase-site.xmlConfiguration conf = HBaseConfiguration.create();conf.set("hbase.zookeeper.quorum", "192.168.77.70:2181, 192.168.77.80:2181");// DDL操作用的客户端对象HBaseAdmin hBaseAdmin = new HBaseAdmin(conf);TableName t_buy_cart_name = TableName.valueOf("t_buy_cart");// 构造一个建表用的描述对象HTableDescriptor t_buy_cart = new HTableDescriptor(t_buy_cart_name);// 构造一个列族的描述对象HColumnDescriptor product = new HColumnDescriptor("product");product.setMaxVersions(3);// 再构造一个列族描述对象HColumnDescriptor recommend = new HColumnDescriptor("recommend");product.setMaxVersions(1);// 在表描述对象中封装建表所必须指定的信息,在表描述对象中指定一个列族t_buy_cart.addFamily(product);// 在表描述对象中再增加一个列族t_buy_cart.addFamily(recommend);// 通过hbaseadmin客户端将表描述对象所描述的表在hbase集群中创建出来hBaseAdmin.createTable(t_buy_cart);// 关闭客户端连接hBaseAdmin.close();}}
阅读全文
1 0
- hbase客户端api实例
- Hbase 基础客户端API
- HBase--客户端API(基础知识)
- HBase客户端API使用
- HBase客户端API使用
- Hbase客户端操作api
- hbase客户端api--建表
- hbase客户端查询API
- HBase client API 实例
- HBase--客户端API(基础知识)二
- HBase客户端API基本操作
- hBase客户端API-增删改
- HBase客户端Java API使用总结
- Hbase 笔记(3) 客户端API基础
- Hbase 笔记(4) 客户端API高级性能
- Hbase 笔记(5) 客户端API 管理功能
- HBase 客户端API --写的工具类
- HBase客户端API(整理 修改一下)
- 《机器学习》(周志华)学习笔记(二)模型评估与选择
- java解析json数组
- 计算机英语·S
- spring boot学习3之mybatis+druid+事务+PageHelper分页插件+sql打印插件整合
- 数据分类处理--set、vector、map等容器
- hbase客户端api实例
- 完全卸载VS2013
- 常见对象-统计字符串中大写小写以及数字出现的次数案例
- UD LAN-401 信号处理模块
- 左侧导航栏
- 高精度的加法(c++)
- Java字符串分隔几个特例
- Docker导入导出
- QT 建立透明背景图像QPixmap