代码:

package cn.idcast.hbase;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.Cell;
import org.apache.hadoop.hbase.CellScanner;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.*;
import org.apache.hadoop.hbase.util.Bytes;
import org.apache.hbase.thirdparty.org.eclipse.jetty.util.Scanner;
import org.junit.Before;
import org.junit.Test; import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator; public class HbaseClientDML {
private Connection connection=null;
@Before
public void getConnection() throws IOException {
//构建连接对象
Configuration configuration = HBaseConfiguration.create();
configuration.set("hbase.zookeeper.quorum","node1:2181,node2:2181,node3:2181");
connection = ConnectionFactory.createConnection(configuration);
}
//插入数据
@Test
public void testPut() throws IOException {
TableName tb=TableName.valueOf("user_info");
//获取一个指定表的table对象,执行DML操作
Table table = connection.getTable(tb);
//增加数据
Put put = new Put(Bytes.toBytes("1"));
put.addColumn(Bytes.toBytes("base_info"),Bytes.toBytes("username"),Bytes.toBytes("张三"));
put.addColumn(Bytes.toBytes("base_info"),Bytes.toBytes("age"),Bytes.toBytes("19"));
put.addColumn(Bytes.toBytes("extra_info"),Bytes.toBytes("address"),Bytes.toBytes("河北")); Put put2 = new Put(Bytes.toBytes("2"));
put2.addColumn(Bytes.toBytes("base_info"),Bytes.toBytes("username"),Bytes.toBytes("李四"));
put2.addColumn(Bytes.toBytes("base_info"),Bytes.toBytes("age"),Bytes.toBytes("19"));
put2.addColumn(Bytes.toBytes("extra_info"),Bytes.toBytes("address"),Bytes.toBytes("邢台")); ArrayList<Put> puts = new ArrayList<>();
puts.add(put);
puts.add(put2);
//插数据
table.put(puts);
connection.close();
table.close();
}
//循环插入大量数据
@Test
public void testManyPut() throws IOException {
TableName tb=TableName.valueOf("user_info");
//获取一个指定表的table对象,执行DML操作
Table table = connection.getTable(tb);
ArrayList<Put> puts = new ArrayList<>();
for(int i=0;i<1000;i++){
//增加数据
Put put = new Put(Bytes.toBytes(""+i));
put.addColumn(Bytes.toBytes("base_info"),Bytes.toBytes("username"),Bytes.toBytes("张三"+i));
put.addColumn(Bytes.toBytes("base_info"),Bytes.toBytes("age"),Bytes.toBytes((19+i)+""));
put.addColumn(Bytes.toBytes("extra_info"),Bytes.toBytes("address"),Bytes.toBytes("河北"));
puts.add(put);
}
table.put(puts); connection.close();
table.close();
}
//删除数据
@Test
public void testDelete() throws IOException {
TableName tb=TableName.valueOf("user_info");
//获取一个指定表的table对象,执行DML操作
Table table = connection.getTable(tb); Delete delete = new Delete(Bytes.toBytes(1));
Delete delete2 = new Delete(Bytes.toBytes("2"));
delete2.addColumn(Bytes.toBytes("extra_info"),Bytes.toBytes("address")); ArrayList<Delete> dels = new ArrayList<>();
dels.add(delete);
dels.add(delete2);
table.delete(dels); table.close();
connection.close();
}
@Test
public void testGet() throws IOException {
TableName tb=TableName.valueOf("user_info");
//获取一个指定表的table对象,执行DML操作
Table table = connection.getTable(tb);
Get get = new Get("1".getBytes());
Result result = table.get(get);
byte[] row = result.getRow();
CellScanner cellScanner = result.cellScanner();
while(cellScanner.advance()){
Cell cell = cellScanner.current(); byte[] rowArray = cell.getRowArray();
byte[] familyArray = cell.getFamilyArray();
byte[] qualifierArray = cell.getQualifierArray();
byte[] valueArray = cell.getValueArray(); System.out.println("行键:"+new String(rowArray,cell.getRowOffset(),cell.getRowLength()));
System.out.println("列族名:"+new String(familyArray,cell.getFamilyOffset(),cell.getFamilyLength()));
System.out.println("列名:"+new String(qualifierArray,cell.getQualifierOffset(),cell.getQualifierLength()));
System.out.println("value:"+new String(valueArray,cell.getValueOffset(),cell.getValueLength())); }
table.close();
connection.close();
}
//按行键查询数据
@Test
public void testScan() throws IOException {
TableName tb=TableName.valueOf("user_info");
//获取一个指定表的table对象,执行DML操作
Table table = connection.getTable(tb);
Scan scan = new Scan("10".getBytes(),"100\000".getBytes());
ResultScanner scanner = table.getScanner(scan);
Iterator<Result> iterator = scanner.iterator();
while(iterator.hasNext()){
Result result = iterator.next();
CellScanner cellScanner = result.cellScanner();
while(cellScanner.advance()){
Cell cell = cellScanner.current(); byte[] rowArray = cell.getRowArray();
byte[] familyArray = cell.getFamilyArray();
byte[] qualifierArray = cell.getQualifierArray();
byte[] valueArray = cell.getValueArray(); System.out.println("行键:"+new String(rowArray,cell.getRowOffset(),cell.getRowLength()));
System.out.println("列族名:"+new String(familyArray,cell.getFamilyOffset(),cell.getFamilyLength()));
System.out.println("列名:"+new String(qualifierArray,cell.getQualifierOffset(),cell.getQualifierLength()));
System.out.println("value:"+new String(valueArray,cell.getValueOffset(),cell.getValueLength())); }
System.out.println("-----------------------------");
} }
}

hbase增删查的更多相关文章

  1. 6.在MVC中使用泛型仓储模式和依赖注入实现增删查改

    原文链接:http://www.c-sharpcorner.com/UploadFile/3d39b4/crud-operations-using-the-generic-repository-pat ...

  2. 3.EF 6.0 Code-First实现增删查改

    原文链接:http://www.c-sharpcorner.com/UploadFile/3d39b4/crud-operations-using-entity-framework-5-0-code- ...

  3. 4.在MVC中使用仓储模式进行增删查改

    原文链接:http://www.c-sharpcorner.com/UploadFile/3d39b4/crud-using-the-repository-pattern-in-mvc/ 系列目录: ...

  4. 5.在MVC中使用泛型仓储模式和工作单元来进行增删查改

    原文链接:http://www.c-sharpcorner.com/UploadFile/3d39b4/crud-operations-using-the-generic-repository-pat ...

  5. jdbc的实例应用:增删查改实现

    //在jdbc中进行增删查改 //查看所有 public static void findAll() { String url = "jdbc:mysql://localhost:3306/ ...

  6. 用javascript实现html元素的增删查改[xyytit]

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  7. hibernate基础增删查改简单实例

    hibernate 基础理论知识网上很多,可以百度和google.这里不做多的介绍,以一个User表来开展例子 建一个web-project 我这里用了junit单元测试环境来进行增删查改的测试,别的 ...

  8. Entity FrameWork 增删查改的本质

    之前的文章里面已经说了,EF的增删查改.那时候的修改,删除,只能是先查询出来要修改的数据,再修改,删除...现在来一个改进版的,增删查改. 1.Add static void Add() { //1. ...

  9. nodejs连接mysql并进行简单的增删查改

    最近在入门nodejs,正好学习到了如何使用nodejs进行数据库的连接,觉得比较重要,便写一下随笔,简单地记录一下 使用在安装好node之后,我们可以使用npm命令,在项目的根目录,安装nodejs ...

随机推荐

  1. netty系列之:EventLoop,EventLoopGroup和netty的默认实现

    目录 简介 EventLoopGroup和EventLoop EventLoopGroup在netty中的默认实现 EventLoop在netty中的默认实现 总结 简介 在netty中不管是服务器端 ...

  2. redirect route 路由传参

    return redirect()->route('exams.sign',['token'=>$token,'id'=>$result['id']]); // 签到页面 Route ...

  3. php 数组相关的函数?

    array()----创建数组array_combine()----通过合并两个数组来创建一个新数组range()----创建并返回一个包含指定范围的元素的数组compact()----建立一个数组a ...

  4. C++ 接口的实现方式

    一.接口的定义 有时候,我们得提供一些接口给别人使用.接口的作用,就是提供一个与其他系统交互的方法.其他系统无需了解你内部细节,并且也无法了解内部细节,只能通过你提供 给外部的接口来与你进行通信.根据 ...

  5. ssm 关于mybatis启动报Result Maps collection already contains value for ...的问题总结

    Result Maps collection already contains value for com.zhaike.mapping.ChapterMapper.BaseResultMap Err ...

  6. centOS 7 离线安装 MySQL 5.6 完美安装

    centOS 7 离线安装 MySQL 5.6 centOS 7 离线安装 MySQL 5.6 准备环境 1.离线 centOS 7(此处为 centOS 7 最小安装) 2.nginx 安装文件 ( ...

  7. session 会话机制以及变量覆盖

    session会话机制介绍如下 http是无状态协议.服务器靠cookie和session来记住用户.$_SESSION 和 $_GET等一样,是超全局变量. 后台脚本里面会写: session() ...

  8. 如何在vscode中编写.net core 项目(vscode)

    1.下载拓展  .NET Core Extension Pack  (作者:保哥) 这个里面将需要的插件都打包了小白一键下载就好了 2.下载扩展   vscode-solution-explorer ...

  9. java面向对象思想之继承

    一.什么是继承 继承就是子类继承父类的特征和行为,使得子类对象(实例)具有父类的实例域和方法,或子类从父类继承方法,使得子类具有父类相同的行为.可以联系生活进行理解,相当于父亲和儿子的关系.父亲有的属 ...

  10. 编写 Java 程序时, 如何在 Java 中创建死锁并修复它?

    经典但核心Java面试问题之一.如果你没有参与过多线程并发 Java 应用程序的编码,你可能会失败.