代码:

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. 对element-ui的table组件的二次封装

    首先,使用过element-ui的table组建的同学都知道,每次使用的时候表头字段都要一个一个的去写,写起来很麻烦,既不美观又浪费时间,基于以上原因,对table组件进行二次封装,使我们在使用的时候 ...

  2. [ Skill ] load 函数优化,识别相对路径

    https://www.cnblogs.com/yeungchie/ 在 cds.lib 文件中定义库的路径,为了规范库定义的管理,经常这样做: . |-- cds.lib ------------- ...

  3. linux作业--第五周

    1.简述osi七层模型和TCP/IP五层模型 一.OSI参考模型 (1) OSI的来源 OSI(Open System Interconnect),即开放式系统互联. 一般都叫OSI参考模型,是ISO ...

  4. 微服务从代码到k8s部署应有尽有大结局(k8s部署)

    我们用一个系列来讲解从需求到上线.从代码到k8s部署.从日志到监控等各个方面的微服务完整实践. 整个项目使用了go-zero开发的微服务,基本包含了go-zero以及相关go-zero作者开发的一些中 ...

  5. 假设检验的python实现命令——Z检验、t检验、F检验

    Z检验 statsmodels.stats.weightstats.ztest() import statsmodels.stats.weightstats as sw 参数详解: x1:待检验数据集 ...

  6. 【工程应用五】 opencv中linemod模板匹配算法诸多疑惑和自我解读。

    研究这个前前后后也有快两三个月了,因为之前也一直在弄模板匹配方面的东西,所以偶尔还是有不少朋友咨询或者问你有没有研究过linemod这个算法啊,那个效率啥的还不错啊,有段时间一直不以为然,觉得我现在用 ...

  7. 部署 Nginx +uwsgi+centos7+django+supervisor 项目

    部署CRM项目 前言 使用软件 nginx 使用nginx是为了它的反向代理功能,项目会通过Django+uWSGI+Nginx进行服务器线上部署. uWSGI python web服务器开发使用WS ...

  8. Ubuntu- 彻底干净卸载MySQL、Apache2、Php的方法

    一.卸载删除 mysql 1 sudo apt-get autoremove --purge mysql-server-5.0 2 sudo apt-get remove mysql-server 3 ...

  9. chili

    靶机准备 首先将靶机ova文件导入 网络模式改为NAT 扫描ip netdiscover -r 192.168.164.0/24 kali:192.168.164.137 渗透测试 扫描端口 nmap ...

  10. ESP8266 NodeMCU引脚说明,CH340和CP2102两款

    开发ESP8266 NodeMCU GPIO功能时,虽然知道ESP8266 NodeMCU丝印引脚编号如下图所示(CP2102款),但是和实际对应的GPIO编号完全不一样.   CP2102款 引脚说 ...