代码:

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. 能动的电脑配件「GitHub 热点速览 v.22.11」

    看到这个标题就知道硬核的 B 站 UP 主稚晖君又更新了,本次带来的是一个造型可爱的小机器人.除了稚晖君这个一贯硬核的软硬件项目之外,本周也有很多有意思的新项目,像 Linux 服务监控小工具 Ray ...

  2. 1251-Client does not support authentication protocol requested by server; consider upgrading MySQL client。

    三:出现的一个错误在安装完MySQL的时候,我们现在一般都使用Navicat来连接数据库,可惜出现下面的错误:1251-Client does not support authentication p ...

  3. Flink消费kafka

    Flink消费Kafka https://blog.csdn.net/boling_cavalry/article/details/85549434 https://www.cnblogs.com/s ...

  4. 简悦+Logseq 搭建本地化个人知识库

    最近在少数派上看到了 简悦 +Logseq 个人知识库搭建 | 从零开始完全指南 - 少数派, 一时间感觉打开了新世界,其实我很早就买了简悦 2.0,但由于一直没有很好的使用场景,外加配置实在过于复杂 ...

  5. [MySQL]MySQL8.0的一些注意事项以及解决方案

    MySQL8.0 注意事项以及解决方案 1. MySQL8.0 修改大小写敏感配置 天坑MySQL8.0! 在安装后, 便无法通过修改配置文件,重启服务,或者执行sql来更改数据库配置, 要想配置的话 ...

  6. crash_for_windows_pkg远程代码执行漏洞

    漏洞详情 crash_for_windows_pkg由 Electron 提供支持.如果 XSS 负载以代理的名义,我们可以远程执行受害者计算机上的任何 JavaScript 代码. 受影响的冲突版本 ...

  7. C++设计模式 - 命令模式(Command)

    行为变化模式 在组件的构建过程中,组件行为的变化经常导致组件本身剧烈的变化."行为变化" 模式将组件的行为和组件本身进行解耦,从而支持组件行为的变化,实现两者之间的松耦合. 典型模 ...

  8. ShellExecuteA加载exe文件指定工作目录找不到文件的问题

    使用ShellExecuteA调用exe文件时,指定工作目录需要注意 函数原型为: HINSTANCE ShellExecuteA( HWND hwnd, LPCTSTR lpOperation, L ...

  9. 在Windows环境下构建Lua 入门

    在Windows环境下构建Lua 一:准备软件 1.C-compiler(TDM GCC)  http://tdm-gcc.tdragon.net/download 2.Lua源代码  http:// ...

  10. 渲染一个react?

    分为首次渲染和更新渲染 生命周期, 建立虚拟DOM, 进行diff算法 对比新旧DOM, 节点对比, 将算法复杂度从O(n^3)降低到O(n) key值优化, 避免用index作为key值, 兄弟节点 ...