当前位置: 首页 > news >正文

【大数据学习 | HBASE高级】hbase的API操作

首先引入hbase的依赖

<dependencies><dependency><groupId>org.apache.hbase</groupId><artifactId>hbase-server</artifactId><version>2.4.13</version></dependency><dependency><groupId>org.slf4j</groupId><artifactId>slf4j-log4j12</artifactId><version>1.7.30</version></dependency><dependency><groupId>log4j</groupId><artifactId>log4j</artifactId><version>1.2.17</version></dependency>
</dependencies>

将hbase-site.xml放入到resouces文件夹中

<?xml version="1.0"?>
<?xml-stylesheet type="text/xsl" href="configuration.xsl"?>
<!--
/** Licensed to the Apache Software Foundation (ASF) under one* or more contributor license agreements.  See the NOTICE file* distributed with this work for additional information* regarding copyright ownership.  The ASF licenses this file* to you under the Apache License, Version 2.0 (the* "License"); you may not use this file except in compliance* with the License.  You may obtain a copy of the License at**     http://www.apache.org/licenses/LICENSE-2.0** Unless required by applicable law or agreed to in writing, software* distributed under the License is distributed on an "AS IS" BASIS,* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.* See the License for the specific language governing permissions and* limitations under the License.*/
-->
<configuration><!--The following properties are set for running HBase as a single process on adeveloper workstation. With this configuration, HBase is running in"stand-alone" mode and without a distributed file system. In this mode, andwithout further configuration, HBase and ZooKeeper data are stored on thelocal filesystem, in a path under the value configured for `hbase.tmp.dir`.This value is overridden from its default value of `/tmp` because manysystems clean `/tmp` on a regular basis. Instead, it points to a path withinthis HBase installation directory.Running against the `LocalFileSystem`, as opposed to a distributedfilesystem, runs the risk of data integrity issues and data loss. NormallyHBase will refuse to run in such an environment. Setting`hbase.unsafe.stream.capability.enforce` to `false` overrides this behavior,permitting operation. This configuration is for the developer workstationonly and __should not be used in production!__See also https://hbase.apache.org/book.html#standalone_dist-->
<property><name>hbase.rootdir</name><value>hdfs://ns1/hbase</value>
</property>
<!-- hbase在hdfs中的存储位置 -->
<property><name>hbase.cluster.distributed</name><value>true</value>
</property>
<!-- 开启hbase的全分布式 -->
<property><name>hbase.zookeeper.property.clientPort</name><value>2181</value>
</property>
<!-- zookeeper的端口号 -->
<property><name>hbase.zookeeper.quorum</name><value>nn1,nn2,s1</value>
</property>
<!-- zookeeper集群的主机名 -->
<property><name>hbase.tmp.dir</name><value>./tmp</value>
</property>
<!-- hbase的临时文件存储路径 -->
<property><name>hbase.unsafe.stream.capability.enforce</name><value>false</value>
</property>
<!-- 开启配置防止hmaster启动问题 -->
<property><name>hbase.master.info.port</name><value>60010</value>
</property>
<!-- 监控页面端口 -->
</configuration>

整体代码如下:

package com.hainiu.hbase;import org.apache.hadoop.hbase.CompareOperator;
import org.apache.hadoop.hbase.NamespaceDescriptor;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.*;
import org.apache.hadoop.hbase.filter.ColumnValueFilter;
import org.apache.hadoop.hbase.filter.SingleColumnValueFilter;
import org.apache.hadoop.hbase.util.Bytes;import java.io.IOException;
import java.util.ArrayList;
import java.util.List;public class TestHbase {public static Connection connection;static{try {connection = ConnectionFactory.createConnection();//创建链接} catch (IOException e) {throw new RuntimeException(e);}}public static void TestCreateNameSpace() throws IOException {Admin admin = connection.getAdmin();//获取管理员对象NamespaceDescriptor desc = NamespaceDescriptor.create("test").build();//创建命名空间描述admin.createNamespace(desc);}public static void TestSearchNameSpace()throws Exception{Admin admin = connection.getAdmin();//获取管理员对象String[] spaces = admin.listNamespaces();for (String space : spaces) {System.out.println(space);}}public static void TestCreateTable()throws Exception{Admin admin = connection.getAdmin();TableDescriptorBuilder build = TableDescriptorBuilder.newBuilder(TableName.valueOf("test:student"));//创建表描述对象ColumnFamilyDescriptor info = ColumnFamilyDescriptorBuilder.newBuilder(Bytes.toBytes("info")).build();//创建列描述对象TableDescriptor desc = build.setColumnFamily(info).build();//将列和表融合admin.createTable(desc);}public static void TestListTable() throws Exception{Admin admin = connection.getAdmin();List<TableDescriptor> tableDescriptors = admin.listTableDescriptors();//创建表查询对象for (TableDescriptor tableDescriptor : tableDescriptors) {TableName name = tableDescriptor.getTableName();System.out.println(name);}}public static void TestDeleteTable()throws Exception{Admin admin = connection.getAdmin();admin.disableTable(TableName.valueOf("test:student"));admin.deleteTable(TableName.valueOf("test:student"));}public static void TestInsertData() throws Exception{Table table = connection.getTable(TableName.valueOf("test:student"));Put put = new Put(Bytes.toBytes("001"));//创建插入对象put.addColumn(Bytes.toBytes("info"),Bytes.toBytes("name"),Bytes.toBytes("zhangsan"));put.addColumn(Bytes.toBytes("info"),Bytes.toBytes("age"),Bytes.toBytes("20"));//增加列值table.put(put);}public static void TestInsertDataBatch() throws Exception{Table table = connection.getTable(TableName.valueOf("test:student"));List<Put> list = new ArrayList<Put>();for(int i=0;i<100;i++){Put put = new Put(Bytes.toBytes(i));put.addColumn(Bytes.toBytes("info"),Bytes.toBytes("name"),Bytes.toBytes("zhangsan"+i));put.addColumn(Bytes.toBytes("info"),Bytes.toBytes("age"),Bytes.toBytes(i));list.add(put);}table.put(list);}public static void TestGetData()throws Exception{Table table = connection.getTable(TableName.valueOf("test:student"));Get get = new Get(Bytes.toBytes(1));Result result = table.get(get);//获取一行内容数据byte[] name = result.getValue(Bytes.toBytes("info"), Bytes.toBytes("name"));byte[] age = result.getValue(Bytes.toBytes("info"), Bytes.toBytes("age"));//列和列族的数据必须是字节数组String name_str = Bytes.toString(name);int age_int = Bytes.toInt(age);//查询完毕的数据要转换为string或者int的原类型System.out.println(name_str+","+age_int);}public static void TestScan()throws Exception{Table table = connection.getTable(TableName.valueOf("test:student"));Scan scan = new Scan();ResultScanner res = table.getScanner(scan);//创建扫面对象for(Result r:res){byte[] name = r.getValue(Bytes.toBytes("info"), Bytes.toBytes("name"));byte[] age = r.getValue(Bytes.toBytes("info"), Bytes.toBytes("age"));String name_str = Bytes.toString(name);int age_int = Bytes.toInt(age);System.out.println(name_str+","+age_int);}}public static void TestScanLimit()throws Exception{Table table = connection.getTable(TableName.valueOf("test:student"));Scan scan = new Scan();scan.withStartRow(Bytes.toBytes(10));scan.withStopRow(Bytes.toBytes(30));//增加rowkey的扫描范围ResultScanner res = table.getScanner(scan);for(Result r:res){byte[] name = r.getValue(Bytes.toBytes("info"), Bytes.toBytes("name"));byte[] age = r.getValue(Bytes.toBytes("info"), Bytes.toBytes("age"));String name_str = Bytes.toString(name);int age_int = Bytes.toInt(age);System.out.println(name_str+","+age_int);}}public static void TestScanWithFilter()throws Exception{Table table = connection.getTable(TableName.valueOf("test:student"));Scan scan = new Scan();
//        ColumnValueFilter filter = new ColumnValueFilter(Bytes.toBytes("info"), Bytes.toBytes("age"), CompareOperator.EQUAL, Bytes.toBytes(30));SingleColumnValueFilter filter = new SingleColumnValueFilter(Bytes.toBytes("info"), Bytes.toBytes("age"), 
//增加过滤器,ColumnValueFilter只能显示出一列,SingleColumnValueFilter能够显示出来所有的列CompareOperator.EQUAL, Bytes.toBytes(20));scan.setFilter(filter);ResultScanner res = table.getScanner(scan);for(Result r:res){byte[] name = r.getValue(Bytes.toBytes("info"), Bytes.toBytes("name"));byte[] age = r.getValue(Bytes.toBytes("info"), Bytes.toBytes("age"));String name_str = Bytes.toString(name);int age_int = Bytes.toInt(age);System.out.println(name_str+","+age_int);}}public static void deleteData() throws Exception{Table table = connection.getTable(TableName.valueOf("test:student"));Delete delete = new Delete(Bytes.toBytes(20));table.delete(delete);}public static void main(String[] args) throws Exception{
//        TestCreateNameSpace();
//        TestSearchNameSpace();
//        TestCreateTable();
//        TestListTable();
//        TestDeleteTable();
//        TestInsertData();
//        TestInsertDataBatch();
//        TestGetData();
//        TestScan();
//        TestScanLimit();
//        TestScanWithFilter();
//        deleteData();connection.close();}
}

相关文章:

【大数据学习 | HBASE高级】hbase的API操作

首先引入hbase的依赖 <dependencies><dependency><groupId>org.apache.hbase</groupId><artifactId>hbase-server</artifactId><version>2.4.13</version></dependency><dependency><groupId>org.slf4j<…...

C++(Qt)软件调试---内存泄漏分析工具MTuner (25)

C(Qt)软件调试—内存泄漏分析工具MTuner &#xff08;25&#xff09; 文章目录 C(Qt)软件调试---内存泄漏分析工具MTuner &#xff08;25&#xff09;[toc]1、概述&#x1f41c;2、下载MTuner&#x1fab2;3、使用MTuner分析qt程序内存泄漏&#x1f9a7;4、相关地址&#x1f41…...

python核心语法

目录 核⼼语法第⼀节 变量0.变量名规则1.下⾯这些都是不合法的变量名2.关键字3.变量赋值4.变量的销毁 第⼆节 数据类型0.数值1.字符串2.布尔值(boolean, bool)3.空值 None 核⼼语法 第⼀节 变量 变量的定义变量就是可变的量&#xff0c;对于⼀些有可能会经常变化的数据&#…...

MATLAB用CNN-LSTM神经网络的语音情感分类深度学习研究

全文链接&#xff1a;https://tecdat.cn/?p38258 在语音处理领域&#xff0c;对语音情感的分类是一个重要的研究方向。本文将介绍如何通过结合二维卷积神经网络&#xff08;2 - D CNN&#xff09;和长短期记忆网络&#xff08;LSTM&#xff09;构建一个用于语音分类任务的网络…...

智能网页内容截图工具:AI助力内容提取与可视化

我们每天都会接触到大量的网页内容。然而&#xff0c;如何从这些内容中快速提取关键信息&#xff0c;并有效地进行整理和分享&#xff0c;一直是困扰我们的问题。本文将介绍一款我近期完成的基于AI技术的智能网页内容截图工具&#xff0c;它能够自动分析网页内容&#xff0c;截…...

Axure设计之文本编辑器制作教程

文本编辑器是一个功能强大的工具&#xff0c;允许用户在图形界面中创建和编辑文本的格式和布局&#xff0c;如字体样式、大小、颜色、对齐方式等&#xff0c;在Web端实际项目中&#xff0c;文本编辑器的使用非常频繁。以下是在Axure中模拟web端富文本编辑器&#xff0c;来制作文…...

【MyBatis源码】深入分析TypeHandler原理和源码

&#x1f3ae; 作者主页&#xff1a;点击 &#x1f381; 完整专栏和代码&#xff1a;点击 &#x1f3e1; 博客主页&#xff1a;点击 文章目录 原始 JDBC 存在的问题自定义 TypeHandler 实现TypeHandler详解BaseTypeHandler类TypeReference类型参考器43个类型处理器类型注册表&a…...

号卡分销系统,号卡系统,物联网卡系统源码安装教程

号卡分销系统&#xff0c;号卡系统&#xff0c;物联网卡系统&#xff0c;&#xff0c;实现的高性能(PHP协程、PHP微服务)、高灵活性、前后端分离(后台)&#xff0c;PHP 持久化框架&#xff0c;助力管理系统敏捷开发&#xff0c;长期持续更新中。 主要特性 基于Auth验证的权限…...

常用命令之LinuxOracleHivePython

1. 用户改密 passwd app_adm chage -l app_adm passwd -x 90 app_adm -> 执行操作后&#xff0c;app_adm用户的密码时间改为90天有效期--查看该euser用户过期信息使用chage命令 --chage的参数包括 ---m 密码可更改的最小天数。为零时代表任何时候都可以更改密码。 ---M 密码…...

从dos上传shell脚本文件到Linux、麒麟执行报错“/bin/bash^M:解释器错误:没有那个文件或目录”

[rootkylin tmp]#./online_update_wars-1.3.0.sh ba51:./online_update_wars-1.3.0.sh:/bin/bash^M:解释器错误:没有那个文件或目录 使用scp命令上传文件到麒麟系统&#xff0c;执行shell脚本时报错 “/bin/bash^M:解释器错误:没有那个文件或目录” 解决方法&#xff1a; 执行…...

使用 Go 实现将任何网页转化为 PDF

在许多应用场景中&#xff0c;可能需要将网页内容转化为 PDF 格式&#xff0c;比如保存网页内容、生成报告、或者创建网站截图。使用 Go 编程语言&#xff0c;结合一些现有的库&#xff0c;可以非常方便地实现这一功能。本文将带你一步一步地介绍如何使用 Go 语言将任何网页转换…...

文件操作和IO

目录 一. 文件预备知识 1. 硬盘 2. 文件 (1) 概念 (2) 文件路径 (3) 文件类型 二. 文件操作 1. 文件系统操作 [1] File常见的构造方法 [2] File的常用方法 [3] 查看某目录下所有的目录和文件 2. 文件内容操作 (1) 打开文件 (2) 关闭文件 (3) 读文件 (4) 写文件 …...

【C++滑动窗口】1248. 统计「优美子数组」|1623

本文涉及的基础知识点 C算法&#xff1a;滑动窗口及双指针总结 LeetCode1248. 统计「优美子数组」 给你一个整数数组 nums 和一个整数 k。如果某个连续子数组中恰好有 k 个奇数数字&#xff0c;我们就认为这个子数组是「优美子数组」。 请返回这个数组中 「优美子数组」 的数…...

C语言导航 4.1语法基础

第四章 顺序结构程序设计 第一节 语法基础 语句概念 语句详解 程序详解 4.1.1语句概念 说明&#xff1a;构成高级语言源程序的基本单位。 特征&#xff1a;在C语言中语句以分号作为结束标志。 分类&#xff1a; &#xff08;1&#xff09;简单语句&#xff1a;空语句、…...

使用 Python 和 Py2Neo 构建 Neo4j 管理脚本

Neo4j 是一个强大的图数据库&#xff0c;适合处理复杂的关系型数据。借助 Python 的 py2neo 库&#xff0c;我们可以快速实现对 Neo4j 数据库的管理和操作。本文介绍一个功能丰富的 Python 脚本&#xff0c;帮助用户轻松管理 Neo4j 数据库&#xff0c;包含启动/停止服务、清空数…...

Centos 7 安装wget

Centos 7 安装wget 最小化安装Centos 7 的话需要上传wget rpm包之后再路径下安装一下。rpm包下载地址&#xff08;http://mirrors.163.com/centos/7/os/x86_64/Packages/&#xff09; 1、使用X-ftp 或者WinSCP等可以连接上传的软件都可以首先连接服务器&#xff0c;这里我用的…...

定时器的小应用

第一个项目 第一步&#xff0c;RCC开启时钟&#xff0c;这个基本上每个代码都是第一步&#xff0c;不用多想&#xff0c;在这里打开时钟后&#xff0c;定时器的基准时钟和整个外设的工作时钟就都会同时打开了 RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);第二步&…...

linux企业中常用NFS、ftp服务

1.静态ip配置 修改ip地址为静态vim /etc/sysconfig/network-scripts/ifcfg-enxxx BOOTPROTO"static" IPADDR192.168.73.10 GATEWAY192.168.73.2 # 该配置与虚拟机网关一致 NETMASK255.255.255.0重启网卡&#xff1a;systemctl restart network.service ping不通域名…...

数据结构与算法分析模拟试题及答案5

模拟试题&#xff08;五&#xff09; 一、单项选择题&#xff08;每小题 2 分&#xff0c;共20分&#xff09; &#xff08;1&#xff09;队列的特点是&#xff08;   &#xff09;。 A&#xff09;先进后出 B&#xff09;先进先出 C&#xff09;任意位置进出 D&#xff0…...

.NET 9.0 中 System.Text.Json 的全面使用指南

以下是一些 System.Text.Json 在 .NET 9.0 中的使用方式&#xff0c;包括序列化、反序列化、配置选项等&#xff0c;并附上输出结果。 基本序列化和反序列化 using System; using System.Text.Json; public class Program {public class Person{public string Name { get; se…...

LumiPixel优化升级:如何利用Z-Image模型生成更细腻的像素人像

LumiPixel优化升级&#xff1a;如何利用Z-Image模型生成更细腻的像素人像 1. 引言&#xff1a;像素艺术的复兴与挑战 像素艺术作为一种独特的数字艺术形式&#xff0c;近年来在游戏、NFT和数字设计领域迎来复兴。然而传统像素创作面临两大核心挑战&#xff1a; 细节表现力不…...

5大理由让你立即选择Argos Translate:开源离线翻译的终极解决方案

5大理由让你立即选择Argos Translate&#xff1a;开源离线翻译的终极解决方案 【免费下载链接】argos-translate Open-source offline translation library written in Python 项目地址: https://gitcode.com/GitHub_Trending/ar/argos-translate Argos Translate是一款…...

SecGPT-14B部署教程:模型热更新机制设计,不中断服务切换安全知识版本

SecGPT-14B部署教程&#xff1a;模型热更新机制设计&#xff0c;不中断服务切换安全知识版本 1. SecGPT-14B简介 SecGPT是由云起无垠推出的开源大语言模型&#xff0c;专门针对网络安全领域设计。这个模型融合了自然语言理解、代码生成和安全知识推理等核心能力&#xff0c;能…...

win11 WSL ubuntu24.04 安装两个、重命名

导出&#xff1a; wsl --export Ubuntu-24.04 D:\Ubuntu-24.04.tar导入新镜像&#xff1a; wsl --import Ubuntu-24.04-2 D:\Ubuntu-24.04-2\Ubuntu-24.04-2 D:\Ubuntu-24.04.tar...

基于comsol的三相电力变压器电磁场与电路耦合计算的电压电流及磁通密度分布分析

comsol三相电力变压器电磁场和电路耦合计算&#xff0c;可以得到变压器高低压绕组电压电流分布以及变压器磁通密度分布三相电力变压器建模这事儿&#xff0c;说难不难说简单也不简单。前两天用COMSOL折腾了个带电路耦合的模型&#xff0c;顺手把绕组电流分布和铁芯磁通都摸清楚…...

酷狗音乐API实战指南:解决音乐应用开发的三大核心痛点

酷狗音乐API实战指南&#xff1a;解决音乐应用开发的三大核心痛点 【免费下载链接】KuGouMusicApi 酷狗音乐 Node.js API service 项目地址: https://gitcode.com/gh_mirrors/ku/KuGouMusicApi 在构建现代音乐应用时&#xff0c;开发者常常面临歌词同步不精准、API接口分…...

AI 开发实战:给团队定一套能落地的 AI 使用规范

AI 开发实战&#xff1a;给团队定一套能落地的 AI 使用规范 一、为什么团队用了 AI 反而容易更乱&#xff1f; 因为每个人都在各自试&#xff1a; 有人用来写代码有人用来写文档有人用来查错有人输出直接复制上线 如果没有基本规范&#xff0c;效率可能提升了&#xff0c;但风险…...

零基础手写大模型

从零搭建大模型&#xff1a;零基础学习实现职业经济跃迁指南 引言 在人工智能重塑全球产业格局的今天&#xff0c;“大模型”已不再仅仅是科技巨头的专利&#xff0c;而是成为了数字经济时代新的“电力”与“石油”。对于广大职场人士、创业者及寻求转型的个体而言&#xff0…...

LeagueAkari终极指南:智能游戏辅助工具快速上手与深度配置

LeagueAkari终极指南&#xff1a;智能游戏辅助工具快速上手与深度配置 【免费下载链接】League-Toolkit 兴趣使然的、简单易用的英雄联盟工具集。支持战绩查询、自动秒选等功能。基于 LCU API。 项目地址: https://gitcode.com/gh_mirrors/le/League-Toolkit 你是否曾在…...

好用还专业!2026 降AIGC平台测评:工具对比+最好用AI推荐

2026年真正好用的AI论文降重与改写工具&#xff0c;核心看降重效果、去AI味、格式保留、学术适配四大指标。综合实测&#xff0c;千笔AI、ThouPen、豆包、DeepSeek、Grammarly 是当前最值得推荐的梯队&#xff0c;覆盖从免费到付费、从中文到英文、从文科到理工的全场景需求。 …...