HBase API

1. 以root身份启动hadoop集群,启动HBase

start-all.sh  #启动hadoop

cd /opt/modules/hbase/bin
./start-hbase.sh

2. 创建Maven项目

 

 

 3. 在Maven中搜索HBase,添加 HBase-server,和HBase-client找到自己客户端对应的版本,复制到pom文件中,需要在pom文件首先添加<dependencies></dependencies>,然后把下面的内容复制到<dependencies></dependencies>节点下

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.eample.demohbase</groupId>
    <artifactId>hbasedemo</artifactId>
    <version>1.0-SNAPSHOT</version>
    <!-- https://mvnrepository.com/artifact/org.apache.hbase/hbase-client -->
    <dependencies>
        <dependency>
            <groupId>org.apache.hbase</groupId>
            <artifactId>hbase-client</artifactId>
            <version>1.3.6</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.apache.hbase/hbase-server -->
        <dependency>
            <groupId>org.apache.hbase</groupId>
            <artifactId>hbase-server</artifactId>
            <version>1.3.6</version>
        </dependency>

    </dependencies>
</project>

 4. Import Changes导入依赖

5.  将log4j.properrties文件放入resources文件夹

log4j.rootLogger=INFO, stdout  
log4j.appender.stdout=org.apache.log4j.ConsoleAppender  
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout  
log4j.appender.stdout.layout.ConversionPattern=%d %p [%c] - %m%n  
log4j.appender.logfile=org.apache.log4j.FileAppender  
log4j.appender.logfile.File=target/spring.log  
log4j.appender.logfile.layout=org.apache.log4j.PatternLayout  
log4j.appender.logfile.layout.ConversionPattern=%d %p [%c] - %m%n  

 6. 新建包,新建类

 

7.  在C:\Windows\System32\drivers\etc中的hosts文件中加入集群映射,如果不配置会出现错误

 java.net.SocketTimeoutException: callTimeout=60000, callDuration=68451: 

8. 创建连接,并测试tb_user表是否存在

package com.example.hbase;


import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.Admin;
import org.apache.hadoop.hbase.client.Connection;
import org.apache.hadoop.hbase.client.ConnectionFactory;


public class ConnectHBase {
    //查看表是否存在
    public static boolean isTableExists(String tableName) throws Exception {
        //获取配置文件信息
        Configuration conf = HBaseConfiguration.create();
//        conf.set("hbase.zookeeper.quorum","centos01,centos02,centos03");
        conf.set("hbase.zookeeper.quorum","192.168.79.131,192.168.79.132,192.168.79.133");
        //获取管理员对象
        Connection connection = ConnectionFactory.createConnection(conf);
        Admin admin = connection.getAdmin();

        //判断表是否存在
        boolean exists = admin.tableExists(TableName.valueOf(tableName));

        //关闭连接
        admin.close();
        return  exists;
    }
    public static void main(String[] args) throws Exception {
        System.out.println(isTableExists("tb_user"));
    }


}

 9. 运行出现错误,但是并没有影响正常结果的输出

2020-05-11 17:12:38,010 WARN [org.apache.hadoop.util.NativeCodeLoader] - Unable to load native-hadoop library for your platform... using builtin-java classes where applicable
  2020-05-11 17:12:38,095 ERROR [org.apache.hadoop.util.Shell] - Failed to locate the winutils binary in the hadoop binary path
  java.io.IOException: Could not locate executable null\bin\winutils.exe in the Hadoop binaries.

结果

 10. 去掉以上报错,下载适合自己haddop版本的winutil.exe,放到电脑中一个目录,我放的是  D:\Program Files\hadoop ,由于下载下来的只有winutil.exe文件,于是新建bin目录

 配置环境变量

 

链接:https://pan.baidu.com/s/1e4UI094u0ovNqAh8hG5Dxw 
提取码:w9t9 
复制这段内容后打开百度网盘手机App,操作更方便哦

11. HBase 代码


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 DemoHbase {
    private static Admin admin;
    private static Configuration configuration;
    private static Connection connection;

    /**
     * 创建连接
     * @throws IOException
     */
    public static void getConnect() throws IOException {
        //获取配置文件信息
        configuration = HBaseConfiguration.create();
        configuration.set("hbase.zookeeper.quorum","centos01,centos02,centos03");
        //创建连接对象
        connection = ConnectionFactory.createConnection(configuration);
        //获取管理员对象
        admin = connection.getAdmin();

    }

    /**
     * 关闭连接
     */

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

        if (connection != null){
            try{
                admin.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    /**
     *  查看表是否存在
     */

    public static boolean isTableExists(String tableName) throws Exception {
        //判断表是否存在
        boolean exists = admin.tableExists(TableName.valueOf(tableName));
        return  exists;
    }

    /**
     *  创建表
     * @param columnfamilies
     * //String... 是可以有多个列族名
     */
    //
    public static void createTable(String tableName,String... columnfamilies) throws Exception {
        //判断列族名是否为空
        if (columnfamilies.length <= 0){
            System.out.println("请输入列族名");
            return;//如果columnfamilies不存在,方法不再继续往下执行
        }
        //判断表是否存在
        if (isTableExists(tableName)){
            System.out.println(tableName+"已存在");
            return;//如果table存在,方法不再继续往下执行
        }
        //创建表描述器
        HTableDescriptor hTableDescriptor = new HTableDescriptor(TableName.valueOf(tableName));

        //创建列族描述器,由于列族是多个,所以使用for循环
        for (String columnfamily : columnfamilies) {
            HColumnDescriptor hColumnDescriptor = new HColumnDescriptor(columnfamily);
            hTableDescriptor.addFamily(hColumnDescriptor);
        }

        //创建表

        admin.createTable(hTableDescriptor);
    }

    /**
     * 删除表
     * @param
     * @throws Exception
     */
    public static void dropTable(String tableName) throws Exception {
        if (!isTableExists(tableName)){
            System.out.println(tableName+"表不存在");
            return;
        }
        admin.disableTable(TableName.valueOf(tableName));
        admin.deleteTable(TableName.valueOf(tableName));

    }

    /**
     * 创建命名空间
     * @param namespace:命名空间
     * @throws Exception
     */
    public static void createNameSpace(String namespace){
        //创建命名空间描述器
        NamespaceDescriptor namespaceDescriptor = NamespaceDescriptor.create(namespace).build();
        try{
            admin.createNamespace(namespaceDescriptor);
        } catch(NamespaceExistException e){
            System.out.println(namespace+"命名空间以存在!");
        }
        catch (IOException e) {
            e.printStackTrace();
        }

    }

    /**
     * 向表中插入数据
     * @param tableName
     * @param columnfamily
     * @param qualifier
     * @param value
     * @param rowKey
     */

    public static void putData(String tableName,String rowKey,String columnfamily,String qualifier,String value) throws IOException {
        //获取表对象
        Table table = connection.getTable(TableName.valueOf(tableName));
        //创建Put对象
        Put put = new Put(Bytes.toBytes(rowKey));
        //在表中添加名为Columnfamily的列族,名为qualifier的列,名为value的值
        put.addColumn(Bytes.toBytes(columnfamily),Bytes.toBytes(qualifier),Bytes.toBytes(value));
//        //在一个列族中添加多个列.在列族colmnfamily添加,sex列,内容为male
//        put.addColumn(Bytes.toBytes(columnfamily),Bytes.toBytes("sex"),Bytes.toBytes("male"));
        table.put(put);

    }

    public static void getData(String tableName,String rowKey,String columnfamily,String qualifier) throws IOException {
        //创建表连接
        Table table = connection.getTable(TableName.valueOf(tableName));
        //创建get对象
        Get get = new Get(Bytes.toBytes(rowKey));
//        //设置列族
//        get.addFamily(Bytes.toBytes(columnfamily));
        //设置列族和列
        get.addColumn(Bytes.toBytes(columnfamily),Bytes.toBytes(qualifier));
        //设置最大版本数
        get.setMaxVersions();
        //获取数据
        Result result = table.get(get);
        for (Cell cell : result.rawCells()) {
            System.out.println( "RowKey:"+Bytes.toString(CellUtil.cloneRow(cell))+
                    ",ColumnFamily:"+Bytes.toString(CellUtil.cloneFamily(cell)) +
                    ",Qualifier:"+Bytes.toString(CellUtil.cloneQualifier(cell))+
                    ",Value:"+Bytes.toString(CellUtil.cloneValue(cell)));
        }
        //关闭表连接
        table.close();
    }
    //scan表
    public static void scanTable(String tableName) throws IOException {
        //获取表对象
        Table table = connection.getTable(TableName.valueOf(tableName));
        //构建scan对象
        Scan scan = new Scan();
        //扫描表
        ResultScanner resultScanner = table.getScanner(scan);
        //解析ResulScanner
        for (Result result : resultScanner) {
            //解析result并打印
            for (Cell cell : result.rawCells()) {
                System.out.println("RowKey:"+Bytes.toString(CellUtil.cloneRow(cell))
                        +",ColumnFamily:"+Bytes.toString(CellUtil.cloneFamily(cell))
                        +",Qualifier:"+Bytes.toString(CellUtil.cloneQualifier(cell))
                        +",Value:"+Bytes.toString(CellUtil.cloneValue(cell)));
            }

        }
        //关闭表连接
        table.close();

    }

    //删除表数据
    public static void  deleteData(String tableName,String rowKey,String columnFamily,String qualifier) throws IOException {
        //获取表对象
        Table table = connection.getTable(TableName.valueOf(tableName));
        //构建delete对象
        Delete delete = new Delete(Bytes.toBytes(rowKey));
        //设置删除的列
        delete.addColumn(Bytes.toBytes(columnFamily),Bytes.toBytes(qualifier));
        //执行删除操作
        table.delete(delete);
        //关闭连接
        table.close();


    }
    public static void main(String[] args) throws Exception {
        //建立连接
        getConnect();

        //测试表是否存在
        System.out.println("-----------测试student表是否存在--------");
        System.out.println(isTableExists("student"));
//        //测试创建表
        System.out.println("-----------创建student表--------");
        createTable("student","name","grade","course");
        System.out.println("-----------测试student表是否存在--------");
        System.out.println(isTableExists("student"));


        //测试创建命名空间
       //createNameSpace("mynamespace");
        //测试添加数据
        System.out.println("-----------为student表添加数据--------");
        putData("student","ABS","grade","chinese","90");
        putData("student","ABS","grade","math","85");
        putData("student","ABS","grade","english","85");
        putData("student","Tom","grade","chinese","97");
        putData("student","Tom","grade","math","100");
        putData("student","Tom","grade","english","92");
        putData("student","ABS","grade","chinese","40");
        putData("student","ABS","grade","chinese","70");
        putData("student","ABS","grade","chinese","100");

        System.out.println("-----------使用get方法获取student表数据--------");
        //测试获取数据
        getData("student","ABS","grade","chinese");
        System.out.println("-----------scan student表--------");
        scanTable("student");
        System.out.println("-----------delete某列数据操作--------");
        deleteData("student","Tom","grade","chinese");
        System.out.println("-----------scan student表--------");
        scanTable("student");
        System.out.println("-----------删除student表--------");
        //测试删除表
        dropTable("student");
        System.out.println("-----------测试student表是否存在--------");
        System.out.println(isTableExists("student"));
        //关闭连接
        close();
    }
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值