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

相关推荐
风象南2 分钟前
SpringBoot自定义RestTemplate的拦截器链
java·spring boot·后端
NCHUtianlin3 分钟前
JAVA生成PDF(itextpdf)
java·开发语言·pdf
yiridancan5 分钟前
终极剖析HashMap:数据结构、哈希冲突与解决方案全解
java·数据结构·算法·哈希算法
老周聊架构8 分钟前
大数据领域开山鼻祖组件Hadoop核心架构设计
大数据
拼搏@2 小时前
第十六天,7月10日,八股
java·mybatis
李元豪2 小时前
grpo nl2sql qwen3 模型强化学习训练有效果的成立条件有哪些
数据库·oracle
Hello.Reader5 小时前
RedisJSON 路径语法深度解析与实战
数据库·redis·缓存
Sylvia-girl6 小时前
Java——抽象类
java·开发语言
TDengine (老段)6 小时前
TDengine 使用最佳实践(2)
大数据·数据库·物联网·时序数据库·iot·tdengine·涛思数据
Deng9452013147 小时前
基于大数据的电力系统故障诊断技术研究
大数据·matplotlib·深度特征提取·随机森林分类算法·标签编码