代码:

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. JAVA 异常和异常处理

    目录 一.异常 1.基本概念 2.异常体系图 3.五大运行时异常 4.编译异常 二.异常处理 1.异常处理的方式 1.1try-catch异常处理 注意事项 课堂练习题 1.2throws异常处理 注 ...

  2. Git更新本地仓库及冲突"Commit your changes or stash them before you can merge"解决

    Git中从远程的分支获取最新的版本到本地有这样2个命令: 1. git fetch:相当于是从远程获取最新版本到本地,不会自动merge git fetch origin mastergit log  ...

  3. 【转载】SQL复杂实例

    from:http://blog.csdn.net/basycia/article/details/52134320       OR       from:http://www.wzsky.NET/ ...

  4. Linux命令大全(查看日志)

    1.查看日志常用命令     tail:          -n  是显示行号:相当于nl命令:例子如下:             tail -100f test.log      实时监控100行日 ...

  5. 伪静态 伪装成静态的网址,只是改变URL的表现形式,实际上还是动态页面

    路由 Route::get('/list_{id}.html','newsController@listoNewone'); 页面详情 <table class="table" ...

  6. SuperEdge: 使用WebAssembly扩展边缘计算场景

    作者 SuperEdge 开发者团队 概要 SuperEdge 是 一个开源的分布式边缘计算容器管理系统,用于管理多个云边区域中的计算资源和容器应用. 在当前架构中,这些资源和应用能够作为一个 Kub ...

  7. 问题排查利器:Linux 原生跟踪工具 Ftrace 必知必会

    本文地址:https://www.ebpf.top/post/ftrace_tools TLDR,建议收藏,需要时查阅. 如果你只是需要快速使用工具来进行问题排查,包括但不限于函数调用栈跟踪.函数调用 ...

  8. 记一次解决关机蓝屏 | MULTIPLE_IRP_COMPLETE_REQUESTS | klflt.sys

    已经解决蓝屏问题,原因是卡巴斯基安全软件驱动导致,需要卸载卡巴斯基安全软件,详细过程如下. 一.关机时蓝屏 Win10系统,在关机动画快结束时突然蓝屏,提示:你的设备遇到问题,需要重启,终止代码:MU ...

  9. Java的jstack命令使用详解

    jstack命令简介 jstack(Java Virtual Machine Stack Trace)是JDK提供的一个可以生成Java虚拟机当前时刻的线程快照信息的命令行工具.线程快照一般被称为th ...

  10. Docker 容器、镜像、日志相关操作

    一. 容器操作 新建并启动 命令:docker run 查看容器 命令:docker ps 查看终止状态的容器 命令:docker ps -a 启动已终止容器 命令:docker start 终止容器 ...