hbase常用命令及使用方法 查看hbase表结构的命令

首先,通过docker安装Hbasedocker search hbase docker pull harisekhon/hbase启动hbase镜像docker run -d -p 2181:2181 -p 8080:8080 -p 8085:8085 -p 9090:9090 -p 9095:9095 -p 16000:16000 -p 16010:16010 -p 16201:16201 -p 16301:16301 -p 16030:16030 -p 16020:16020...

首先,通过docker安装Hbase

docker search hbase docker pull harisekhon/hbase

启动hbase镜像

docker run -d -p 2181:2181 -p 8080:8080 -p 8085:8085 -p 9090:9090 -p 9095:9095 -p 16000:16000 -p 16010:16010 -p 16201:16201 -p 16301:16301 -p 16030:16030 -p 16020:16020 --name hbase001 harisekhon/hbase

访问hbase

访问localhost:16010

Hbase命令行操作

docker exec -it hbase001 bash hbase shell

经过上面的命令操作,我们进入了hbase的命令行操作模式:

接下来,我们创建一个表,将名称空间作为缺省值,将表名作为test,将列族作为cf:create ‘测试’,’cf & # 8217您可以通过list命令查看所有的表:list

Delete table: disable first,然后dropdisable ' test ' drop ' test 'Add data operationput ' test ',' 1 ',' cf: name ',' flush' put' test ',' cf:name ',' hbase'put 'test ',' 2 ',' cf:age ',' 20'put 'test ',' cf:name ',' hadoop'put 'test ',' 3 ',' cf:age ',' 22'put 'test ',' 11 ',' cf:name ',' spark'put 'test ',1扫描命令,用于查看某个表的所有数据;2.get命令,用于查看表中的某一行数据。修改数据也是一个put操作。比如把行键为2015003的学生年龄改成25:放'学生',' 2015003 ','年龄',' 25 '。这些是命令行的一些基本操作。详细操作请参考官网:

http://abloz.com/hbase/book.html

下面是如何通过java api操作hbase。

添加***ven依赖

<dependency> <groupId>org.apache.hbase</groupId> <artifactId>hbase-client</artifactId> <version>2.1.3</version></dependency>

测试工具栏如下:

package com.example.redisiondemo.utils;import org.apache.hadoop.conf.Configuration;import org.apache.hadoop.hbase.*;import org.apache.hadoop.hbase.client.*;import org.apache.hadoop.hbase.client.Admin;import org.apache.hadoop.hbase.util.Bytes;import java.io.IOException;import java.util.ArrayList;import java.util.List;public class HbaseTest { private static Admin admin; private static final String COLUMNS_FAMILY_1 = "cf1"; private static final String COLUMNS_FAMILY_2 = "cf2"; public static Connection initHbase() throws IOException { Configuration configuration = HBaseConfiguration.create(); configuration.set("hbase.zookeeper.quorum", "127.0.0.1"); configuration.set("hbase.zookeeper.property.clientPort", "2181"); configuration.set("hbase.***ster", "127.0.0.1:16010"); Connection connection = ConnectionFactory.createConnection(configuration); return connection; }//创建表 create public static void createTable(TableName tableName, String[] cols) throws IOException { admin = initHbase().getAdmin(); if (admin.tableExists(tableName)) { System.out.println("Table Already Exists!"); } else { HTableDescriptor hTableDescriptor = new HTableDescriptor(tableName); for (String col : cols) { HColumnDescriptor hColumnDescriptor = new HColumnDescriptor(col); hTableDescriptor.addFamily(hColumnDescriptor); } admin.createTable(hTableDescriptor); System.out.println("Table Create Successful"); } } public static TableName getTbName(String tableName) { return TableName.valueOf(tableName); } // 删除表 drop public static void deleteTable(TableName tableName) throws IOException { admin = initHbase().getAdmin(); if (admin.tableExists(tableName)) { admin.disableTable(tableName); admin.deleteTable(tableName); System.out.println("Table Delete Successful"); } else { System.out.println("Table does not exist!"); } } //put 插入数据 public static void insertData(TableName tableName, Student student) throws IOException { Put put = new Put(Bytes.toBytes(student.getId())); put.addColumn(Bytes.toBytes(COLUMNS_FAMILY_1), Bytes.toBytes("name"), Bytes.toBytes(student.getName())); put.addColumn(Bytes.toBytes(COLUMNS_FAMILY_1), Bytes.toBytes("age"), Bytes.toBytes(student.getAge())); initHbase().getTable(tableName).put(put); System.out.println("Data insert success:" + student.toString()); } // delete 删除数据 public static void deleteData(TableName tableName, String rowKey) throws IOException { Delete delete = new Delete(Bytes.toBytes(rowKey)); // 指定rowKey// delete = delete.addColumn(Bytes.toBytes(COLUMNS_FAMILY_1), Bytes.toBytes("name")); // 指定column,也可以不指定,删除该rowKey的所有column initHbase().getTable(tableName).delete(delete); System.out.println("Delete Success"); } // scan数据 public static List<Student> allScan(TableName tableName) throws IOException { ResultScanner results = initHbase().getTable(tableName).getScanner(new Scan().addFamily(Bytes.toBytes("cf1"))); List<String> list = new ArrayList<>(); for (Result result : results) { Student student = new Student(); for (Cell cell : result.rawCells()) { String colName = Bytes.toString(cell.getQualifierArray(), cell.getQualifierOffset(), cell.getQualifierLength()); String value = Bytes.toString(cell.getValueArray(), cell.getValueOffset(), cell.getValueLength()); } } return null; } // 根据rowkey get数据 public static Student singleGet(TableName tableName, String rowKey) throws IOException { Student student = new Student(); student.setId(rowKey); Get get = new Get(Bytes.toBytes(rowKey)); if (!get.isCheckExistenceOnly()) { Result result = initHbase().getTable(tableName).get(get); for (Cell cell : result.rawCells()) { String colName = Bytes.toString(cell.getQualifierArray(), cell.getQualifierOffset(), cell.getQualifierLength()); String value = Bytes.toString(cell.getValueArray(), cell.getValueOffset(), cell.getValueLength()); switch (colName) { case "name": student.setName(value); break; case "age": student.setAge(value); break; default: System.out.println("unknown columns"); } } } System.out.println(student.toString()); return student; } // 查询指定Cell数据 public static String getCell(TableName tableName, String rowKey, String cf, String column) throws IOException { Get get = new Get(Bytes.toBytes(rowKey)); String rst = null; if (!get.isCheckExistenceOnly()) { get = get.addColumn(Bytes.toBytes(cf), Bytes.toBytes(column)); try { Result result = initHbase().getTable(tableName).get(get); byte[] resByte = result.getValue(Bytes.toBytes(cf), Bytes.toBytes(column)); rst = Bytes.toString(resByte); } catch (Exception exception) { System.out.printf("columnFamily or column does not exists"); } } System.out.println("Value is: " + rst); return rst; } public static void ***in(String[] args) throws IOException{ Student student = new Student(); student.setId("1"); student.setName("hzp"); student.setAge("18"); String table = "student"; // createTable(getTbName(table), new String[]{COLUMNS_FAMILY_1, COLUMNS_FAMILY_2}); // deleteTable(getTbName(table)); insertData(getTbName(table), student);// deleteData(getTbName(table), "1"); // singleGet(getTbName(table), "2"); // getCell(getTbName(table), "1", "cf1", "name"); }}student实体package com.example.redisiondemo.utils;public class Student { private String id; private String name; private String age; public String getId() { return id; } public void setId(String id) { this.id = id; } public String getName() { return name; } @Override public String toString() { return "Student{" + "id='" + id + ''' + ", name='" + name + ''' + ", age='" + age + ''' + '}'; } public void setName(String name) { this.name = name; } public String getAge() { return age; } public void setAge(String age) { this.age = age; }}

本文来自半邊陽光投稿,不代表舒华文档立场,如若转载,请注明出处:https://www.chinashuhua.cn/24/598572.html

打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
() 0
上一篇 06-19
下一篇 06-19

相关推荐

  • hbase查询命令及使用方法 hbase如何根据列查询

    1.1 什么是HBaseHBASE是一个高可靠性、高性能、面向列、可伸缩的分布式存储系统,利用HBASE技术可在廉价PC Server上搭建起大规模结构化存储集群。HBASE的目标是存储并处理大型的数据,更具体来说是仅需使用普通的硬件配置,就能够处理由成千上万的行和列所组成的大型数据。HB

    2023-07-19 04:21:01
    748 0
  • hbase删除数据的几种方式 hbase真正删除数据

    写作过程1)1)客户端向HregionServer发送写请求;2)HregionServer将数据写入HLog(预写日志)。用于数据的持久化和恢复;3)HregionServer将数据写入内存(memstore);4)反馈客户端编写成功。数据刷新过程1)当MemStore数据达到阈值(默认值为128M,旧版为***M)时,将数据刷到硬盘,

    2023-06-22 20:05:01
    330 0
  • hbase常用命令及使用方法 查看hbase表结构的命令

    首先,通过docker安装Hbasedocker search hbase docker pull harisekhon/hbase启动hbase镜像docker run -d -p 2181:2181 -p 8080:8080 -p 8085:8085 -p 9090:9090 -p 9095:9095 -p 16000:16000 -p 16010:16010 -p 16201:16201 -p 16301:16301 -p 16030:16030 -p 16020:16020

    2023-06-19 19:26:01
    614 0
  • spark读取hbase数据速度 spark读取hbase提速的方法

    简介POLARDB数据库是阿里云开发的下一代关系型云数据库。100%兼容MySQL,性能高达MySQL的6倍。但是随着数据量的不断增加,面临着单个SQL无法分析结果的现状。X-Pack Spark为数据库提供了分析引擎,旨在创建一个闭环数据库。在X-Pack Spark的帮助下,POLARDB数据可以归档到以列

    2023-06-14 14:24:01
    989 0

评论列表

联系我们

在线咨询: QQ交谈

邮件:admin@qq.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信