HBase demo最简单的实现

包的结构

在这里插入图片描述
这里注意一定要用JDK 1.8 否则会出现不兼容,报错的问题。

pom.xml

<?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>org.example</groupId>
    <artifactId>HbaeDemo</artifactId>
    <version>1.0-SNAPSHOT</version>
    <dependencies>
        <dependency>
            <groupId>org.apache.hbase</groupId>
            <artifactId>hbase-client</artifactId>
            <version>1.3.1</version>
        </dependency>
        <dependency>
            <groupId>org.apache.hbase</groupId>
            <artifactId>hbase-server</artifactId>
            <version>1.3.1</version>
        </dependency>
        <dependency>
            <groupId>org.apache.hbase</groupId>
            <artifactId>hbase-common</artifactId>
            <version>1.3.1</version>
        </dependency>
        <dependency>
            <groupId>commons-logging</groupId>
            <artifactId>commons-logging</artifactId>
            <version>1.2</version>
        </dependency>
        <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
            <version>1.2.17</version>
        </dependency>
    </dependencies>
</project>


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 HbaseDemo {
    
    

    private static Admin admin = null;
    private static Connection connection = null;

    //创建链接
    static {
    
    
        Configuration configuration = HBaseConfiguration.create();
        //配置zookeeper的地址
        configuration.set("hbase.zookeeper.quorum", "这里填写的是zookeeper集群的地址");
        try {
    
    
            //通过工厂类获取管理员
            connection = ConnectionFactory.createConnection(configuration);
            admin = connection.getAdmin();
        } catch (IOException e) {
    
    
            e.printStackTrace();
        }
    }

    /**
     * 关闭链接
     *
     * @param conn
     * @param admin
     */
    private void close(Connection conn, Admin admin) {
    
    
        if (conn != null) {
    
    
            try {
    
    
                conn.close();
            } catch (IOException e) {
    
    
                e.printStackTrace();
            }

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

    /**
     * 创建表
     *
     * @param tableName 需要创建的表名字
     * @param cfs       列族的名字,可以指定多个
     */
    public static void createTable(String tableName, String... cfs) throws IOException {
    
    

        if (ExistTable(tableName)) {
    
    
            System.out.println("表名已经存在!");
            return;
        }
        HTableDescriptor hTableDescriptor = new HTableDescriptor(TableName.valueOf(tableName));
        for (String cf : cfs) {
    
    
            HColumnDescriptor hColumnDescriptor = new HColumnDescriptor(cf);
            hTableDescriptor.addFamily(hColumnDescriptor);
        }
        admin.createTable(hTableDescriptor);

        System.out.println("表名创建成功!");
    }

    /**
     * 创建表是否存在
     *
     * @param tableName 表名
     * @return 存在返回true,不存在返回false
     * @throws IOException
     */
    public static boolean ExistTable(String tableName) throws IOException {
    
    
        boolean exists = admin.tableExists(TableName.valueOf(tableName));
        return exists;
    }

    /**
     * 删除表
     *
     * @param tableName 表名
     */
    public static void deleteTable(String tableName) throws IOException {
    
    
        if (!ExistTable(tableName)) {
    
    
            System.out.println("表不存在!");
            return;
        }
        admin.disableTable(TableName.valueOf(tableName));
        admin.deleteTable(TableName.valueOf(tableName));
        System.out.println("表已经被删除!");
    }

    /**
     * 添加/更新数据
     *
     * @param tableName 表名
     * @param rowKey    行键
     * @param cf        列族名字
     * @param cn        字段名字
     * @param value     值
     */
    public static void putData(String tableName, String rowKey, String cf, String cn, String value) throws IOException {
    
    
        Table table = connection.getTable(TableName.valueOf(tableName));
        Put put = new Put(Bytes.toBytes(rowKey));
        put.addColumn(Bytes.toBytes(cf), Bytes.toBytes(cn), Bytes.toBytes(value));
        table.put(put);
        table.close();
        System.out.println("添加成功!");
    }

    /**
     * 删除数据
     *
     * @param tableName 表名
     * @param rowKey    行键
     * @param cf        列族
     * @param cn        字段
     */
    public static void deleteData(String tableName, String rowKey, String cf, String cn) throws IOException {
    
    
        Table table = connection.getTable(TableName.valueOf(tableName));
        Delete delete = new Delete(Bytes.toBytes(rowKey));
        delete.addColumns(Bytes.toBytes(cf), Bytes.toBytes(cn));
        table.delete(delete);
        table.close();
        System.out.println("删除成功!");
    }

    /**
     * 全表扫描
     *
     * @param tableName 表名字
     * @throws IOException
     */
    public static void scanTable(String tableName) throws IOException {
    
    
        Table table = connection.getTable(TableName.valueOf(tableName));
        Scan scan = new Scan();
        ResultScanner results = table.getScanner(scan);
        for (Result result : results) {
    
    
            Cell[] cells = result.rawCells();
            for (Cell cell : cells) {
    
    
                System.out.println("RK" + Bytes.toString(CellUtil.cloneRow(cell)));
                System.out.println("CF" + Bytes.toString(CellUtil.cloneFamily(cell)));
                System.out.println("CN" + Bytes.toString(CellUtil.cloneQualifier(cell)));
                System.out.println("VALUE" + Bytes.toString(CellUtil.cloneValue(cell)));
            }
        }
    }

    /**
     * 查询内容
     *
     * @param tableName
     * @param rowKey
     */
    public static void getData(String tableName, String rowKey) throws IOException {
    
    

        Table table = connection.getTable(TableName.valueOf(tableName));
        Get get = new Get(Bytes.toBytes(rowKey));
        Result result = table.get(get);
        Cell[] cells = result.rawCells();

        for (Cell cell : cells) {
    
    
            System.out.println(Bytes.toString(CellUtil.cloneValue(cell)));
        }
    }


    public static void main(String[] args) {
    
    
        try {
    
    
            scanTable("ecdefault:EmailLog");
        } catch (IOException e) {
    
    
            e.printStackTrace();
        }
    }

}

猜你喜欢

转载自blog.csdn.net/gps666666/article/details/121402028
今日推荐