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

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

目录

  • (一)练习常用的HBase Shell命令
    • 1、启动HBase
    • 2、练习shell命令
      • create scan list describe alter
      • put
      • get
      • delete
      • drop
    • 关于NoSQL数据库中的列族和列
    • 3、关闭hbase服务
  • (二)HBase 常用的Java API 及应用实例
    • 1、启动hbase服务
    • 2、启动eclipse
    • 3、新建java project
      • 导入jar包
      • 创建类文件
      • 常用Java API:
      • 打开hbase shell
      • 退出hbase shell ——exit
    • 4、关闭 hbase

(一)练习常用的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:

在这里插入图片描述

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


http://www.kler.cn/news/334207.html

相关文章:

  • 资源《Arduino 扩展板4-单游戏摇杆》说明。
  • Prompt技巧总结和示例分享
  • linux网络编程实战
  • Hive数仓操作(九)
  • 算法笔记(十一)——优先级队列(堆)
  • 云原生(四十八) | Nginx软件安装部署
  • Linux基础命令su详解
  • MKV转MP4丨FFmpeg的简单命令使用——视频格式转换
  • VSCode debug模式无法跳转进入内置模块
  • HTB:Mongod[WriteUP]
  • MAC备忘录空白解决方案
  • 通过PHP获取商品详情
  • 微信小程序使用scroll-view 加上enable-flex之后高度变得特别长
  • 《无机杀手》制作团队选择Blender的原因分析
  • 【Yocto 是一个开源项目】
  • Python Kivy 进阶功能教程
  • ES8的Java API client 8.0 简单示例操作 Elasticsearch
  • 【Java】IntelliJ IDEA开发环境安装
  • C++基础---类和对象(上)
  • vue使用高德地图