hbase java操作api - Get,Scan相关的api

news/2023/9/28 17:52:59

代码示例

有很多的代码是重复的,只是为形成记忆,见谅,
另外需要将 hbase-site.xml,hdfs-site.xml,core-site.xml三个文件放到Resources上目录中
 

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.*;
import org.apache.hadoop.hbase.client.*;
import org.apache.hadoop.hbase.client.coprocessor.Batch;
import org.apache.hadoop.hbase.filter.*;
import org.apache.hadoop.hbase.io.compress.Compression;
import org.apache.hadoop.hbase.util.Bytes;
import java.util.ArrayList;
import java.util.List;public class HbaseApi_test_1 {private static Connection hbaseConn;private static Configuration hbaseConf;//HBaseAdmin 提供了一个接口来管理 HBase 数据库的表信息private static Admin hbaseAdmin;/*** 静态构造,在调用静态方法前运行,  初始化连接对象  * */static {hbaseConf = HBaseConfiguration.create();try {hbaseConn = ConnectionFactory.createConnection(hbaseConf);System.out.println("连接上了?" + !hbaseConn.isClosed());hbaseAdmin = hbaseConn.getAdmin();} catch (java.lang.Exception e) {e.printStackTrace();}}//****************************根据rowkey读取一行数据****************************public static void getRow() throws java.lang.Exception {TableName tableName = TableName.valueOf("ns1:mytest_4");//取得一个要操作的表Table table = hbaseConn.getTable(tableName);//设置要查询的行的rowkeyGet wangwu = new Get(Bytes.toBytes("wangwu"));//设置显示多少个版本的数据wangwu.setMaxVersions(3);//取得指定时间戳的数据//wangwu.setTimeStamp(1);//限制要显示的列族//wangwu.addFamily(Bytes.toBytes("grade"));//限制要显示的列//wangwu.addColumn(Bytes.toBytes("course"), Bytes.toBytes("yuwen"));Result result = table.get(wangwu);List<Cell> cells = result.listCells();for (Cell c : cells) {//注意这里 CellUtil类的使用System.out.print("行键:" + Bytes.toString(CellUtil.cloneRow(c)) + " ");System.out.print("列族:" + Bytes.toString(CellUtil.cloneFamily(c)) + " ");System.out.print("列:" + Bytes.toString(CellUtil.cloneQualifier(c)) + " ");System.out.print("值:" + Bytes.toString(CellUtil.cloneValue(c)) + " ");System.out.println();}//关闭资源table.close();//hbaseConn.close();}//****************************根据rowkey,读取多行数据****************************public static void getMultiRows() throws java.lang.Exception {TableName tableName = TableName.valueOf("ns1:mytest_4");//取得一个要操作的表Table table = hbaseConn.getTable(tableName);ArrayList<Get> getArrayList = new ArrayList<>();Get wangwu = new Get(Bytes.toBytes("wangwu"));//限制要显示的列族wangwu.addFamily(Bytes.toBytes("grade"));//限制要显示的列wangwu.addColumn(Bytes.toBytes("course"), Bytes.toBytes("yuwen"));Get lishi = new Get(Bytes.toBytes("lishi"));getArrayList.add(wangwu);getArrayList.add(lishi);Result[] results = table.get(getArrayList);for (int i = 0; i < results.length; i++) {Result result = results[i];List<Cell> cells = result.listCells();for (Cell c : cells) {//注意这里 CellUtil类的使用System.out.print("行键:" + Bytes.toString(CellUtil.cloneRow(c)) + " ");System.out.print("列族:" + Bytes.toString(CellUtil.cloneFamily(c)) + " ");System.out.print("列:" + Bytes.toString(CellUtil.cloneQualifier(c)) + " ");System.out.print("值:" + Bytes.toString(CellUtil.cloneValue(c)) + " ");System.out.println();}}//关闭资源table.close();//hbaseConn.close();}//****************************scan全表扫描查询****************************public static void scan_easy() throws java.lang.Exception {TableName tableName = TableName.valueOf("ns1:mytest_4");//取得一个要操作的表Table table = hbaseConn.getTable(tableName);/*** Scan的构造函数有多种*///例 1//Scan scan = new Scan();//例 2,指定rowkey的开始和结束Scan scan = new Scan(Bytes.toBytes("person-1007"), Bytes.toBytes("person-1021"));ResultScanner scanner = table.getScanner(scan);//源码 interface ResultScanner extends Closeable, Iterable<Result>,所以可以如下操作for (Result result : scanner) {List<Cell> cells = result.listCells();for (Cell c : cells) {//注意这里 CellUtil类的使用System.out.print("行键:" + Bytes.toString(CellUtil.cloneRow(c)) + " ");System.out.print("列族:" + Bytes.toString(CellUtil.cloneFamily(c)) + " ");System.out.print("列:" + Bytes.toString(CellUtil.cloneQualifier(c)) + " ");System.out.print("值:" + Bytes.toString(CellUtil.cloneValue(c)) + " ");System.out.println();}}//关闭资源scanner.close();table.close();//hbaseConn.close();}//****************************scan全表扫描查询,指定列族,列,哪个rowkey开始,哪个rowkey结束****************************public static void scan_startRowKey_endRowKey() throws java.lang.Exception {long begin = System.currentTimeMillis();TableName tableName = TableName.valueOf("ns1:mytest_4");//取得一个要操作的表Table table = hbaseConn.getTable(tableName);Scan scan = new Scan();/*** scan常见的设置*///1、只查询列族 column family为grade的数据scan.addFamily(Bytes.toBytes("grade"));//1、只查询列column为course:yuwen的数据scan.addColumn(Bytes.toBytes("course"), Bytes.toBytes("yuwen"));//2、开始的rowkey行键(开始行包含),结束的rowkey行键(结束行不包含)//scan.setStartRow(Bytes.toBytes("lishi"));//scan.setStopRow(Bytes.toBytes("zhangshan"));//3、batch和caching和hbase table column size共同决定了rpc的次数。//scan可以通过setCaching与setBatch方法提高速度(以空间换时间);//scan.setCaching(500); //每次rpc的请求记录数,默认是1;cache大可以优化性能,但是太大了会花费很长的时间进行一次传输。//scan.setBatch(2); //设置每次取的column size;有些row特别大,所以需要分开传给client,就是一次传一个row的几个column。scan.setCaching(500);scan.setBatch(2);ResultScanner scanner = table.getScanner(scan);//源码 interface ResultScanner extends Closeable, Iterable<Result>,所以可以如下操作for (Result result : scanner) {List<Cell> cells = result.listCells();for (Cell c : cells) {//注意这里 CellUtil类的使用System.out.print("行键:" + Bytes.toString(CellUtil.cloneRow(c)) + " ");System.out.print("列族:" + Bytes.toString(CellUtil.cloneFamily(c)) + " ");System.out.print("列:" + Bytes.toString(CellUtil.cloneQualifier(c)) + " ");System.out.print("值:" + Bytes.toString(CellUtil.cloneValue(c)) + " ");System.out.println();}}long end = System.currentTimeMillis();System.out.println("运行时间:" + (end - begin));//关闭资源scanner.close();table.close();//hbaseConn.close();}}

 


https://dhexx.cn/news/show-18107.html

相关文章

HTML基础(2) 格式标签 文本标签

格式标签&#xff1a; 1.<p></p> 用来显示段落 2.<br> 控制换行 3.<nobr> </nobr> 防止浏览器将过长内容自动换行显示 4 <blockquote></blockquote> 在这个标签队中的文本按照所进的效果进行显示 5、<center></center>…

Oracle增加表空间解决ORACLE ORA-01653: unable to extend table报错

Oracle增加表空间一、查看表空间的名字及文件所在位置二、增加所需表空间大小1、方法一2、方法二3、方法三三、查询表空间使用情况一、查看表空间的名字及文件所在位置 select tablespace_name, file_id, file_name, round(bytes/(1024*1024),0) total_space from dba_data_fi…

C#线程安全使用(一)

关于Task的使用&#xff0c;一直都是半知半解&#xff0c;最近终于有时间详细的看了一遍MSDN&#xff0c;作为备忘录&#xff0c;将心得也记录下来和大家分享。 首先&#xff0c;根据MSDN的描述&#xff0c;Task是FrameWork4引进的新功能&#xff0c;他和ConCurrent命名空间一起…

Hive和Hbase整合使用,注意事项,细节等,没有讲整合的环境搭建

配置 hive 与 hbase 整合的目的是利用 HQL 语法实现对 hbase 数据库的增删改查操作&#xff0c;基本原理就是利用两者本身对外的API接口互相进行通信&#xff0c;两者通信主要是依靠hive_hbase-handler.jar工具类。 但请注意&#xff1a;使用Hive操作HBase中的表&#xff0c;只…

Stream通过findFirst()查找满足条件的一条数据

Stream通过findFirst查找满足条件的一条数据一、Stream通过findFirst()查找满足条件的一条数据1、案例2、其他一、Stream通过findFirst()查找满足条件的一条数据 1、案例 如果取得第一个元素&#xff0c;则用findFirst() 最后提取元素的时候&#xff0c;可以用&#xff1a;ge…

经典算法研究系列:九、图像特征提取与匹配之SIFT算法

经典算法研究系列&#xff1a;九、SIFT算法研究 作者:July、二零一一年二月十五日。 推荐阅读&#xff1a;David G. Lowe, "Distinctive image features from scale-invariant keypoints," International Journal of Computer Vision, 60, 2 (2004), pp. 91-110 ----…

Lua脚本和C++交互(一)

现在&#xff0c;越来越多的C服务器和客户端融入了脚本的支持&#xff0c;尤其在网游领域&#xff0c;脚本语言已经渗透到了方方面面&#xff0c;比如你可以在你的客户端增加一个脚本&#xff0c;这个脚本将会帮你在界面上显示新的数据&#xff0c;亦或帮你完成某些任务&#x…

日志@Slf4j介绍使用及配置等级

日志Slf4j介绍使用及配置等级一、依赖及插件二、设置日志级别三、演示一、依赖及插件 <dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId><optional>true</optional></dependency>安装一个lom…

Mybatis-Plus实现多主键批量保存或更新

Mybatis-Plus实现多主键批量保存或更新一、依赖二、启动类注解三、表结构四、配置文件五、代码1、实体类2、持久层3、服务层4、逻辑层五、测试一、依赖 <dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web&…

Maven 的配置

1.安装maven之前&#xff0c;要先安装jdk及配置JAVA_HOME环境变量。JDK1.4以上 2. 下载maven3&#xff0c;最新版本是Maven3.2.3&#xff0c;下载地址&#xff1a;http://maven.apache.org/download.html 下载apache-maven-3.2.3-bin.zip文件后&#xff0c;并解压到F:\java\apa…