查询Hbase数据分为Get方式查询,Scan方式查询,Scan配合Filter过滤查询
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.Cell;
import org.apache.hadoop.hbase.CellUtil;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.*;
import org.apache.hadoop.hbase.util.Bytes;
public class SelectGetHbaseData {
public static void main(String[] args) throws Exception{
Configuration conf = new Configuration();
conf.set("hbase.zookeeper.quorum","192.168.1.10:2181");
Connection conn = ConnectionFactory.createConnection(conf);
Table gadaite = conn.getTable(TableName.valueOf("Gadaite"));
Result result = gadaite.get(new Get(Bytes.toBytes("002")));
Cell[] cells = result.rawCells();
for(Cell cell:cells){
System.out.println("INFO: "+cell);
System.out.println(new String(CellUtil.cloneRow(cell))); // 行键
System.out.println(new String(CellUtil.cloneFamily(cell))); // 列簇
System.out.println(new String(CellUtil.cloneQualifier(cell))); // 字段
System.out.println(new String(CellUtil.cloneValue(cell))); // 值
System.out.println(cell.getTimestamp()); // 时间戳
}
gadaite.close();
conn.close();
}
}
输出结果:
下面查询全部,不进行过滤
import org.apache.hadoop.hbase.Cell;
import org.apache.hadoop.hbase.CellUtil;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.*;
public class SelectScanAllHbaseData {
public static void main(String[] args) throws Exception{
// 连接信息,连接对象
org.apache.hadoop.conf.Configuration conf = HBaseConfiguration.create();
conf.set("hbase.zookeeper.quorum","192.168.1.10:2181");
Connection conn = ConnectionFactory.createConnection(conf);
Admin admin = conn.getAdmin();
// 连接Hbase数据库中的表
Table gadaite = conn.getTable(TableName.valueOf("Gadaite"));
Scan scan = new Scan();
ResultScanner scanner = gadaite.getScanner(scan);
// 读取Hbase表中的数据
for(Result sc:scanner){
// sc:对应cell集合,Hbase的一个列簇
for (Cell c:sc.rawCells()){
// c:sc的对象,对应一条数据
String rowkey = new String(CellUtil.cloneRow(c));//行键
String column = new String(CellUtil.cloneFamily(c));//列簇
String field = new String(CellUtil.cloneQualifier(c));//字段
String values = new String(CellUtil.cloneValue(c));//值
// String s = new String(CellUtil.copyRow(c));//行键
System.out.println(rowkey+" "+column+" "+field+" "+values);
// System.out.println(s);
}
}
}
}
输出结果:
关于CompareOperator.EQUAL方法类似的还有:
// LESS <
// LESS_OR_EQUAL <=
// EQUAL =
// NOT_EQUAL <>
// GREATER_OR_EQUAL >=
// GREATER >
// NO_OP
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.Cell;
import org.apache.hadoop.hbase.CellUtil;
import org.apache.hadoop.hbase.CompareOperator;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.*;
import org.apache.hadoop.hbase.filter.RegexStringComparator;
import org.apache.hadoop.hbase.filter.RowFilter;
public class SelectScanRowkeyHbaseData {
public static void main(String[] args) throws Exception{
Configuration conf = new Configuration();
conf.set("hbase.zookeeper.quorum","192.168.1.10");
Connection conn = ConnectionFactory.createConnection(conf);
Table gadaite = conn.getTable(TableName.valueOf("Gadaite"));
Scan scanrowkey = new Scan();
// 匹配以2结尾的行键
RowFilter rowFilter = new RowFilter(CompareOperator.EQUAL, new RegexStringComparator("2$"));
scanrowkey.setFilter(rowFilter);
ResultScanner scanner = gadaite.getScanner(scanrowkey);
for(Result sc:scanner){
// sc:对应cell集合,Hbase的一个列簇
for (Cell c:sc.rawCells()){
// c:sc的对象,对应一条数据
String rowkey = new String(CellUtil.cloneRow(c));//行键
String column = new String(CellUtil.cloneFamily(c));//列簇
String field = new String(CellUtil.cloneQualifier(c));//字段
String values = new String(CellUtil.cloneValue(c));//值
// String s = new String(CellUtil.copyRow(c));//行键
System.out.println(rowkey+" "+column+" "+field+" "+values);
// System.out.println(s);
}
}
}
}
输出结果:
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.Cell;
import org.apache.hadoop.hbase.CellUtil;
import org.apache.hadoop.hbase.CompareOperator;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.*;
import org.apache.hadoop.hbase.filter.SingleColumnValueFilter;
import org.apache.hadoop.hbase.util.Bytes;
public class SelectScanColumnHbaseData {
public static void main(String[] args) throws Exception{
Configuration conf = new Configuration();
conf.set("hbase.zookeeper.quorum","192.168.1.10");
Connection conn = ConnectionFactory.createConnection(conf);
Table gadaite = conn.getTable(TableName.valueOf("Gadaite"));
Scan columnscan = new Scan();
// 这里如果为没有满足筛选条件的值的时候会将空值所在的数据进行返回
SingleColumnValueFilter columnfilter = new SingleColumnValueFilter(Bytes.toBytes("F2"), Bytes.toBytes("F21"),
CompareOperator.EQUAL,Bytes.toBytes("G2.G21"));
columnscan.setFilter(columnfilter);
ResultScanner scanner = gadaite.getScanner(columnscan);
for (Result sc:scanner){
for (Cell c:sc.rawCells()){
System.out.println(c);
System.out.println(new String(CellUtil.cloneValue(c)));
}
}
}
}
输出结果:
可能会匹配为空,并将对应为null的这条数据进行返回
如原数据:
我们过滤F1下,F12值为 “tttttttt”
3.2上对应更改部分:
SingleColumnValueFilter columnfilter = new SingleColumnValueFilter(Bytes.toBytes("F1"), Bytes.toBytes("F12"),
CompareOperator.EQUAL,Bytes.toBytes("tttt"));
输出结果:
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.Cell;
import org.apache.hadoop.hbase.CellUtil;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.*;
import org.apache.hadoop.hbase.filter.ColumnPrefixFilter;
import org.apache.hadoop.hbase.util.Bytes;
// 列名前缀过滤器
public class SelectColumnPrefixFilter {
public static void main(String[] args) throws Exception{
Configuration conf = new Configuration();
conf.set("hbase.zookeeper.quorum","192.168.1.10:2181");
Connection conn = ConnectionFactory.createConnection(conf);
Table gadaite = conn.getTable(TableName.valueOf("Gadaite"));
Scan scan = new Scan();
// 值返回指定列簇的数据
ColumnPrefixFilter f2 = new ColumnPrefixFilter(Bytes.toBytes("F2"));
scan.setFilter(f2);
ResultScanner scanner = gadaite.getScanner(scan);
for (Result sc:scanner){
for (Cell c:sc.rawCells()){
System.out.println(c);
System.out.println(new String(CellUtil.cloneValue(c)));
}
}
}
}
输出结果:
如下为F2列簇
如下,只查询行键为002,且只查询F2列簇下的数据
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.Cell;
import org.apache.hadoop.hbase.CellUtil;
import org.apache.hadoop.hbase.CompareOperator;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.*;
import org.apache.hadoop.hbase.filter.ColumnPrefixFilter;
import org.apache.hadoop.hbase.filter.FilterList;
import org.apache.hadoop.hbase.filter.RegexStringComparator;
import org.apache.hadoop.hbase.filter.RowFilter;
import org.apache.hadoop.hbase.util.Bytes;
// 多条件过滤集合
public class SelectSetFilter {
public static void main(String[] args) throws Exception{
Configuration conf = new Configuration();
conf.set("hbase.zookeeper.quorum","192.168.1.10");
Connection conn = ConnectionFactory.createConnection(conf);
Table gadaite = conn.getTable(TableName.valueOf("Gadaite"));
FilterList list = new FilterList();
Scan scan = new Scan();
RowFilter rowFilter = new RowFilter(CompareOperator.EQUAL, new RegexStringComparator("002"));
ColumnPrefixFilter f2 = new ColumnPrefixFilter(Bytes.toBytes("F2"));
list.addFilter(rowFilter);
list.addFilter(f2);
scan.setFilter(list);
ResultScanner scanner = gadaite.getScanner(scan);
for (Result sc:scanner){
for (Cell c:sc.rawCells()){
System.out.println(c);
System.out.println(new String(CellUtil.cloneValue(c)));
}
}
}
}
输出结果:
04.在spark和flink上提供了别的更丰富的API,有机会以后再更。。。。
文章浏览阅读382次。arch 画面撕裂_archlinux nvidia 画面撕裂
文章浏览阅读1.1k次。第一章 课程简介第1课 课程入门简介第二章 软件基础学习第1课 软件工作界面第2课 基本的鼠标操作第3课 模型创建的一般过程第4课 草图简介第5课 草图的绘制第6课 草图的编辑第7课 草图的约束第8课 草图绘制的一般过程第9课 草图绘制综合案例第10课 拉伸特征01第11课 拉伸特征02第12课 拉伸特征03第13课 拉伸..._solidworks课程简介
文章浏览阅读86次。 由点积的几何意义(即投影)可以发现答案一定在凸壳上,并且投影的变化是一个单峰函数,可以三分。现在需要处理的只有删除操作,线段树分治即可。#include<iostream> #include<cstdio>#include<cmath>#include<cstdlib>#include<cstring>#i..._bzoj 线段树分治
文章浏览阅读586次。准备工作:打开官网创建自己的微信公众号官网创建公众号完成打开基本配置->填写服务器配置1.说明:URL: 填写自己的服务器的地址,可以接收到微信服务器发送的信息 笔者使用腾讯云的云服务器,新手还挺便宜的腾讯云官网Token: 用来验证微信发送的信息和获得公众号的Access_TokenEncodingAESKey:加密密钥,微信回通过这个密钥把数据加密后,把数据传送给..._c# 验证消息的确来自微信服务器
文章浏览阅读5.8k次,点赞2次,收藏11次。代码下载https://download.csdn.net/download/u010168781/10373174头文件#ifndef CAMERATEST_H#define CAMERATEST_H#include <QMainWindow>#include <QGraphicsView>#include <QKeyEvent>..._qcamera qgraphicsvideoitem
文章浏览阅读2.8k次。基于MNIST数据集的空间Transformer网络示意。参考文献:pytorch中文版。_mnist transformer
文章浏览阅读8.2k次。背景设置 background-color:背景颜色 background-image:背景图片 background-repeat:背景图片的重复方式 background-position:背景图片的位置 background-attachement:附着方式 scroll:相对于元素固定 fixed:相对于窗体固定 ..._h5如何设置背景
文章浏览阅读78次。react基础学习_react 持续更新
文章浏览阅读396次。首先,大家QAQQAQQAQ可能会问,arfaarfaarfa小姐姐不是已经写过一篇关于sublimesublimesublime的日报吗?为什么我还要写呢?据文文新闻报道(有咩?),arfaarfaarfa小姐姐是PPP党的大姐头?所以有关C++C++C++的编译运行都没讲QAQQAQQAQ.说真的,那些sublimesublimesublime转vscodevscodevscode的童鞋..._base16 color schemes sublime
文章浏览阅读774次。if(){}else{}层数太多,提出来另写一个方法 有的时候要判断2个变量的值,返回不同的结果; 但是if(){}else{}层数可能太多,就可以把判断提出来另写一个方法。【修改前】//假设$x的值可能为1,2//假设$y的值可能为1,2,3public f..._对if else提取
文章浏览阅读1w次,点赞6次,收藏36次。Python每日一练——字符串与正则表达式第三关:python字符串支持的基本操作_python字符串与正则表达式试题
文章浏览阅读483次。转自:ModelSim中Altera仿真库的添加最近,做一个IP核的调试,但是里面调用了Altera的syncram,这样ModelSim就不能直接进行仿真,而QuartusII又不支持Tesbbench的仿真,所以为了在ModelSim中仿真就必须将Altera的这些仿真库添加到ModelSim中。通常,在ModelSim中进行仿真需要加入Quartus提供的仿真库,原因是下面..._modelsim altera安装