3、练习常用的HBase Shell命令+HBase 常用的Java API 及应用实例

目录

(一)练习常用的HBase Shell命令

1、启动HBase

先启动HDFS 再启动HBase

进入shell交互式执行环境

2、练习shell命令

create scan list describe alter



put



get

delete

drop

删除表前要disable它

关于NoSQL数据库中的列族和列

这些数据库允许你以非常灵活的方式存储和检索数据

3、关闭hbase服务

(二)HBase 常用的Java API 及应用实例

1、启动hbase服务

2、启动eclipse

3、新建java project

导入jar包

导入/usr/local/hbase/lib中的所有jar包

再导入/usr/local/hbase/lib/client-facing-thirdparty中的所有jar 包

创建类文件

常用Java API:

java 复制代码
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.*;
import org.apache.hadoop.hbase.client.*;
import org.apache.hadoop.hbase.util.Bytes;
import java.io.IOException;

public class ExampleForHBase {
    public static Configuration configuration;
    public static Connection connection;
    public static Admin admin;

    public static void main(String[] args) throws IOException {
        init();
        createTable("student", new String[]{"score"});
        insertData("student", "zhangsan", "score", "English", "69");
        insertData("student", "zhangsan", "score", "Math", "86");
        insertData("student", "zhangsan", "score", "Computer", "77");
        getData("student", "zhangsan", "score", "English");
        close();
    }

    public static void init() {
        configuration = HBaseConfiguration.create();
        configuration.set("hbase.rootdir", "hdfs://localhost:9000/hbase");
        try {
            connection = ConnectionFactory.createConnection(configuration);
            admin = connection.getAdmin();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void close() {
        try {
            if (admin != null) {
                admin.close();
            }
            if (connection != null) {
                connection.close();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void createTable(String myTableName, String[] colFamily) throws IOException {
        TableName tableName = TableName.valueOf(myTableName);
        if (admin.tableExists(tableName)) {
            System.out.println("table is exists!");
        } else {
            TableDescriptorBuilder tableDescriptor = TableDescriptorBuilder.newBuilder(tableName);
            for (String str : colFamily) {
                ColumnFamilyDescriptor family = ColumnFamilyDescriptorBuilder.newBuilder(Bytes.toBytes(str)).build();
                tableDescriptor.setColumnFamily(family);
            }
            admin.createTable(tableDescriptor.build());
        }
    }

    public static void insertData(String tableName, String rowKey, String colFamily, String col, String val) throws IOException {
        Table table = connection.getTable(TableName.valueOf(tableName));
        Put put = new Put(rowKey.getBytes());
        put.addColumn(Bytes.toBytes(colFamily), Bytes.toBytes(col), Bytes.toBytes(val));
        table.put(put);
        table.close();
    }

    public static void getData(String tableName, String rowKey, String colFamily, String col) throws IOException {
        Table table = connection.getTable(TableName.valueOf(tableName));
        Get get = new Get(rowKey.getBytes());
        get.addColumn(Bytes.toBytes(colFamily), Bytes.toBytes(col));
        Result result = table.get(get);
        System.out.println(new String(result.getValue(Bytes.toBytes(colFamily), Bytes.toBytes(col))));
        table.close();
    }
}

run------>run as application

打开hbase shell

list scan

退出hbase shell ------exit

4、关闭 hbase

相关推荐
一水鉴天几秒前
整体设计 之 绪 思维导图引擎 之 引 认知系统 之8 之 序 认知元架构 之4 统筹:范畴/分类/目录/条目 之2 (豆包助手 之6)
大数据·架构·认知科学
a587691 分钟前
消息队列(MQ)高级特性深度剖析:详解RabbitMQ与Kafka
java·分布式·面试·kafka·rabbitmq·linq
hmb↑4 分钟前
Kafka 3.9.x 安装、鉴权、配置详解
分布式·kafka·linq
代码雕刻家6 分钟前
3.1.Maven-课程介绍
java·maven
li357412 分钟前
深入理解:MQ监听类 vs Spring事件监听类 —— 区别、用法与适用场景全解析
java·数据库·spring
灵感蛙25 分钟前
《苍穹外卖》项目日记_Day7
java·spring boot·redis
lifallen27 分钟前
KafkaStreams 计算图节点设计:ProcessorNode、SourceNode、SinkNode
java·数据结构·算法·kafka·apache
索迪迈科技30 分钟前
java后端工程师进修ing(研一版‖day42)
java·开发语言·学习·算法
半桔35 分钟前
【Linux手册】消息队列从原理到模式:底层逻辑、接口实战与责任链模式的设计艺术
java·linux·运维·服务器
安然~~~38 分钟前
mysql的索引
数据库·mysql