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

相关推荐
比花花解语几秒前
Kafka在Windows系统使用delete命令删除Topic时出现的问题
windows·分布式·kafka
解决方案工程师2 分钟前
【Kafka】Kafka高性能解读
分布式·kafka
yellowatumn5 分钟前
RocketMq\Kafka如何保障消息不丢失?
分布式·kafka·rocketmq
s_fox_6 分钟前
Nginx Embedded Variables 嵌入式变量解析(4)
java·网络·nginx
小光学长10 分钟前
基于flask+vue框架的的医院预约挂号系统i1616(程序+源码+数据库+调试部署+开发环境)带论文文档1万字以上,文末可获取,系统界面在最后面。
数据库
Jelena1577958579212 分钟前
使用Java爬虫获取1688 item_get_company 接口的公司档案信息
java·开发语言·爬虫
数据小小爬虫15 分钟前
Jsoup解析商品详情时,如何确保数据准确性?
java·爬虫
听封17 分钟前
✨ 索引有哪些缺点以及具体有哪些索引类型
数据库·mysql
利瑞华22 分钟前
数据库索引:缺点与类型全解析
数据库·oracle
python资深爱好者25 分钟前
什么容错性以及Spark Streaming如何保证容错性
大数据·分布式·spark