IDEA+Hadoop2.10.1+Zookeeper3.4.10+Hbase 2.3.5 操作JavaAPI

2021/7/13 22:08:43

本文主要是介绍IDEA+Hadoop2.10.1+Zookeeper3.4.10+Hbase 2.3.5 操作JavaAPI,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

在此之前要配置好三节点的hadoop集群,zookeeper集群,并启动它们,然后再配置好HBase环境

本文只是HBase2.3.5API操作作相应说明,如果前面环境还没有配置好,可以翻看我之前的博客,欢迎留言交流

节点hadoop01

 

 节点hadoop02

 

节点hadoop03

1 maven依赖

<dependency>
            <groupId>org.apache.hbase</groupId>
            <artifactId>hbase-server</artifactId>
            <version>2.3.5</version>
        </dependency>

        <dependency>
            <groupId>org.apache.hbase</groupId>
            <artifactId>hbase-client</artifactId>
            <version>2.3.5</version>
        </dependency>

        <dependency>
            <groupId>jdk.tools</groupId>
            <artifactId>jdk.tools</artifactId>
            <version>1.8</version>
            <scope>system</scope>
            <systemPath>C:/Program Files/Java/jdk1.8.0_261/lib/tools.jar</systemPath>
        </dependency>

2 API操作

package com.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 org.stringtemplate.v4.ST;
import scala.util.control.Exception;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class ConfigulationHBase {

    private static Configuration configuration;

    private static Connection connection;

    private static Admin admin;

    static {
        configuration = HBaseConfiguration.create();
        configuration.set("hbase.zookeeper.quorum", "192.168.161.141");
        configuration.set("hbase.zookeeper.property.clientPort", "2181");
        try {
            connection = ConnectionFactory.createConnection(configuration);
            admin = connection.getAdmin();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 判斷表是否存在
     * <br>存在則返回true
     * @param tableName
     * @return
     * @throws IOException
     */
    public static boolean isTableExist(String tableName) throws IOException {
        boolean b = admin.tableExists(TableName.valueOf(tableName));
        return b;
    }

    /**
     * 創建表
     *
     * 参数tableName为表的名称,字符串数组fields为存储记录各个域名称的数组。<br>
     * 要求当HBase已经存在名为tableName的表时,先删除原有的表,然后再<br>
     * 创建新的表  field:列族<br>
     * @param tableName 表名
     * @param fields    列族名
     * @throws IOException
     */
    public static void createTable(String tableName,String[] fields) throws IOException {
        if(isTableExist(tableName)){
            System.out.println(tableName + " table is alreadly exist...");
            admin.disableTable(TableName.valueOf(tableName));
            admin.deleteTable(TableName.valueOf(tableName));
            System.out.println(tableName + " table is deleted...");
        }
        HTableDescriptor hTableDescriptor = new HTableDescriptor(TableName.valueOf(tableName));
        for(String str:fields){
            HColumnDescriptor hColumnDescriptor = new HColumnDescriptor(str);
            hTableDescriptor.addFamily(hColumnDescriptor);
        }
        admin.createTable(hTableDescriptor);
        System.out.println(tableName + " table is created!");
        admin.close();
    }

    /**
     * 添加数据
     *
     * 向表tableName,行键rowKey和fields字段指定的单元格中添加对应的值values<br>
     * 例如:表名:student,行键:1001,添加的字段:info:name,添加的值:Janna<br>
     * put 'student','1001','info:name','Janna'<br>
     *
     * @param tableName 表名
     * @param rowKey    行键
     * @param family    列族
     * @param qualifier 列族值
     * @param value     值
     * @throws IOException
     */
    public static void addRow(String tableName, String rowKey, String family, String qualifier, String value) throws IOException {
        Table table = connection.getTable(TableName.valueOf(tableName));
        byte[] rowKeyAsBytes = rowKey.getBytes();
        Put put = new Put(rowKeyAsBytes);
        put.addColumn(family.getBytes(), qualifier.getBytes(), value.getBytes());
        table.put(put);
        table.close();
        admin.close();
    }

    /**
     * 删除一行或者多行数据
     * @param tableName
     * @param rows
     * @throws IOException
     */
    public static void deleteMultiRow(String tableName, String... rows) throws IOException {
        Table table = connection.getTable(TableName.valueOf(tableName));
        List<Delete> deleteList = new ArrayList<Delete>();
        for (int i = 0; i < rows.length; i++) {
            Delete delete = new Delete(rows[i].getBytes());
            deleteList.add(delete);
        }
        table.delete(deleteList);
        table.close();
        admin.close();
    }

    /**
     *
     * 查询某tableName所有的数据
     * @param tableName 表名
     * @throws IOException
     */
    public static void getAllRows(String tableName) throws IOException {
        Table table = connection.getTable(TableName.valueOf(tableName));
        Scan scan = new Scan();
        ResultScanner scanner = table.getScanner(scan);
        for (Result result:scanner){
            Cell[] cells = result.rawCells();
            for (Cell cell:cells){
                System.out.println("RowKey:" + Bytes.toString(CellUtil.cloneRow(cell)));
                System.out.println("Famliy:" + Bytes.toString(CellUtil.cloneFamily(cell)));
                System.out.println("qualifier:" + Bytes.toString(CellUtil.cloneQualifier(cell)));
                System.out.println("value:" + Bytes.toString(CellUtil.cloneValue(cell)));
                System.out.println("--------------------------------------");
            }
            System.out.println("=======================================");
        }
        table.close();
    }

    /**
     * 查询tableName表的rowKey行键的数据
     * get 'student','1001'
     * @param tableName 表名
     * @param rowKey    行键
     */
    public static void getRow(String tableName, String rowKey) throws IOException {
        Table table = connection.getTable(TableName.valueOf(tableName));
        Get get = new Get(rowKey.getBytes());
        Result result = table.get(get);
        for (Cell cell:result.rawCells()){
            System.out.println("RowKey:" + Bytes.toString(CellUtil.cloneRow(cell)));
            System.out.println("Famliy:" + Bytes.toString(CellUtil.cloneFamily(cell)));
            System.out.println("qualifier:" + Bytes.toString(CellUtil.cloneQualifier(cell)));
            System.out.println("value:" + Bytes.toString(CellUtil.cloneValue(cell)));
            System.out.println("--------------------------------------");
        }
    }

    /**
     * 获取某表某行键某列族的值
     *
     * @param tableName 表名
     * @param rowKey    行键
     * @param famliy    列族
     * @param qualifier 列族值
     * @throws IOException
     */
    public static void getRowQualifier(String tableName, String rowKey, String famliy, String qualifier) throws IOException {
        Table table = connection.getTable(TableName.valueOf(tableName));
        Get get = new Get(rowKey.getBytes());
        get.addColumn(famliy.getBytes(), qualifier.getBytes());
        Result result = table.get(get);
        for (Cell cell:result.rawCells()){
            System.out.println("RowKey:" + Bytes.toString(CellUtil.cloneRow(cell)));
            System.out.println("Famliy:" + Bytes.toString(CellUtil.cloneFamily(cell)));
            System.out.println("qualifier:" + Bytes.toString(CellUtil.cloneQualifier(cell)));
            System.out.println("value:" + Bytes.toString(CellUtil.cloneValue(cell)));
            System.out.println("timestamp:" + cell.getTimestamp());
            System.out.println("--------------------------------------");
        }
    }

    /**
     * main 程序入口
     * @param args
     * @throws IOException
     */
    public static void main(String[] args) throws IOException {
        // 創建表
//        String[] fileds = {"base_info","other_info"};
//        createTable("football", fileds);
        // 添加数据
//        addRow("football", "11002", "base_info", "name", "lin");
//        addRow("football", "11002", "base_info", "sex", "female");
//        addRow("football", "11003", "base_info", "sex", "male");
        //  删除数据
//        deleteMultiRow("football", new String[]{"11002"});
        //  得到所有的数据
//        getAllRows("student");
        //  查询某表某行键数据
//        getRow("student","1001");
        //  查询某表某行键某列族数据
         getRowQualifier("student", "1001", "info", "name");

    }
}

3 部分结果截图

 

 

 

 仅供参考,有错误还请指出!

有什么想法,评论区留言,互相指教指教。

 


这篇关于IDEA+Hadoop2.10.1+Zookeeper3.4.10+Hbase 2.3.5 操作JavaAPI的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程