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

server/2024/11/17 18:40:31/

首先引入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();}
}


http://www.ppmy.cn/server/142709.html

相关文章

Java重点---值传递和引用传递

目录 ⭐Java中只有值传递&#xff0c;没有引用传递&#xff1b;C里才有引用传递⭐ ⭐对象初始化顺序&#xff08;对象的每个部位在内存中申请空间以及赋值的顺序&#xff09; ⭐代码块的优先级高于构造方法&#xff0c;而代码块&#xff08;方法块&#xff09;之间的优先级相…

算法训练(leetcode)二刷第二十七天 | *56. 合并区间、*738. 单调递增的数字、*968. 监控二叉树

刷题记录 *56. 合并区间*738. 单调递增的数字*968. 监控二叉树 *56. 合并区间 leetcode题目地址 重叠区间&#xff0c;若前一个区间的右边界大于等于当前区间的左边界&#xff0c;则有重叠&#xff0c;合并两个区间。 时间复杂度&#xff1a; O ( n l o g n ) O(nlogn) O(n…

2019计挑赛c语言

1.下列选项中,说法正确的是(D)。 a.函数的形参可以是常量、变量或表达式 实参可以是任何类型(可以是常量,变量或表达式),但是形参却不能是表达式 形参不能是表达式 b.函数返回值的类型是由return语句中表达式类型决定 函数返回值的类型是由函数定义时指定的类型决定的,…

工业相机选取

1.相机分类&#xff1a; 1.1 在相机曝光方式中&#xff0c;全局曝光和卷帘曝光是两种主流技术。CCD相机通常采用全局曝光方式&#xff0c;而CMOS相机则可能采用卷帘曝光。 面阵相机与全局曝光关联与区别 关联&#xff1a;面阵相机可以使用全局曝光作为曝光方式&#xff0c;但…

github 以及 huggingface下载模型和数据

runningcheese/MirrorSite: 镜像网站合集 (github.com) huggingface 下载模型和数据使用snapshot_download的方法 不会修改HuggingFace模型下载默认缓存路径&#xff1f;一篇教会你!_huggingface默认下载路径-CSDN博客 下载模型 使用snapshot_download 使用snapshot_down…

当kafka消费的数据滞后1000条时,打印告警信息

要在 Kafka 消费者中实现当数据滞后1000条时打印告警信息&#xff0c;你需要在消费循环中添加逻辑来检查当前消费者的偏移量与主题中的最新偏移量之间的差异。如果这个差异大于1000&#xff0c;就打印告警信息。以下是修改后的代码示例&#xff1a; package com.mita.web.core.…

第二十一周机器学习笔记:动手深度学习之——数据操作、数据预处理

第二十周周报 摘要Abstract一、动手深度学习1. 数据操作1.1 数据基本操作1.2 数据运算1.2.1 广播机制 1.3 索引和切片 2. 数据预处理 二、复习RNN与LSTM1. Recurrent Neural Network&#xff08;RNN&#xff0c;循环神经网络&#xff09;1.1 词汇vector的编码方式1.2 RNN的变形…

常见git命令记录

记录一些常见的git操作 下载代码 下载 git clone [代码连接] 切分支 git branch -b [分支名] 提交代码 添加 git add [需要提交的代码路径] 提交 git commit -m "一些骚话" push git push origin HEAD:refs/for/[仓名称] 通过diff文件&#xff0c;同步修…