Java教程

HBase API--DDL

本文主要是介绍HBase API--DDL,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

1.环境准备

新建项目后在pom.xml中添加依赖:

注意:可能会报错javax.el包不存在,不用管,是一个测试用的依赖,不影响使用

<dependencies>

    <dependency>

        <groupId>org.apache.hbase</groupId>

        <artifactId>hbase-server</artifactId>

        <version>2.0.5</version>

        <exclusions>

            <exclusion>

                <groupId>org.glassfish</groupId>

                <artifactId>javax.el</artifactId>

            </exclusion>

        </exclusions>

    </dependency>

    <dependency>

        <groupId>org.apache.hbase</groupId>

        <artifactId>hbase-client</artifactId>

        <version>2.0.5</version>

    </dependency>

    <dependency>

        <groupId>org.glassfish</groupId>

        <artifactId>javax.el</artifactId>

        <version>3.0.1-b06</version>

    </dependency>

</dependencies>

package com.wn.hbasedemo;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.Cell;
import org.apache.hadoop.hbase.CellUtil;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.*;
import org.apache.hadoop.hbase.client.Connection;
import org.apache.hadoop.hbase.util.Bytes;

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

/**
*
*
{@link null}
* @return {@link }
* @throws
* @author wn
* @date 2021/7/12 23:51
*/

public class HBaseUtilDML {
    //声明静态属性
    public static Connection connection;

    static {
        //初始化单例连接
        //1.获取配置对象
        Configuration conf = HBaseConfiguration.create();
        //2.给配置对象增加参数
        conf.set("hbase.zookeeper.quorum","hadoop102,hadoop103,hadoop104");
        //3.获取连接
        try {
            connection = ConnectionFactory.createConnection(conf);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    public static void close() throws IOException {
        connection.close();
    }

    /**
     * 插入或修改数据
     * @param namespace
     * @param tableName
     * @param rowKey
     * @param family
     * @param column
     * @param value
     */
    public static void putCell(String namespace,String tableName,String rowKey,String family,String column,String value) throws IOException {
        //获取table
        Table table = connection.getTable(TableName.valueOf(namespace,tableName));
        //调用table的put方法
        Put put = new Put(Bytes.toBytes(rowKey));
        put.addColumn(Bytes.toBytes(family),Bytes.toBytes(column),Bytes.toBytes(value));
        try {
            table.put(put);
        } catch (IOException e) {
            e.printStackTrace();
        }
        table.close();
    }

    /**
     * 获取数据
     * @param namespace
     * @param tableName
     * @param rowKey
     * @param family
     * @param column
     * @return
     * @throws IOException
     */
    public static String[] getCell(String namespace,String tableName,String rowKey,String family,String column) throws IOException {
        //获取table
        Table table = connection.getTable(TableName.valueOf(namespace, tableName));
        //获取get对象
        Get get = new Get(Bytes.toBytes(rowKey));
        //添加get属性
        get.addColumn(Bytes.toBytes(family),Bytes.toBytes(column));
        //get.readAllVersions();
        get.readVersions(5);
        //get数据
        //简便用法  只能读最新的一个版本
      /*  String s=null;
        try {
            s = new String(table.get(get).value());
        } catch (IOException e) {
            e.printStackTrace();
        }*/
        //复杂用法
        //获取result
        Result result = table.get(get);
        Cell[] cells = result.rawCells();
        //遍历cells
        int len=cells.length;
        String[] values=new String[len];
        len=0;
        for (Cell cell : cells) {
            values[len]=new String(CellUtil.cloneRow(cell)) + "-" +new String(CellUtil.cloneFamily(cell)) +
                    "-" + new String(CellUtil.cloneQualifier(cell))+ "-" + new String(CellUtil.cloneValue(cell));
            len++;
        }
        for (String value : values) {
            System.out.println(value);
        }
        //关闭资源
        table.close();
        return values;
    }

    /**
     * 扫描数据
     * @param namespace
     * @param tableName
     * @param startRow
     * @param stopRow
     * @return
     * @throws IOException
     */
    public static List<String> scanRows(String namespace, String tableName,String startRow,String stopRow) throws IOException {
        //获取table
        Table table = connection.getTable(TableName.valueOf(namespace,tableName));
        //创建scan对象
        Scan scan = new Scan().withStartRow(Bytes.toBytes(startRow)).withStopRow(Bytes.toBytes(stopRow));
        //扫描数据
        ResultScanner scanner = table.getScanner(scan);
        //获取数据
        ArrayList<String> arrayList= new ArrayList<>();
        for (Result result : scanner) {
            arrayList.add(Bytes.toString(result.value()));
        }
        //关闭资源
        scanner.close();
        table.close();
        return arrayList;
    }

    public static void deleteColumn(String namespace,String tableName,String rowKey,String family,String column) throws IOException {
        //获取table
        Table table = connection.getTable(TableName.valueOf(namespace,tableName));
        //创建delete对象
        Delete delete = new Delete(Bytes.toBytes(rowKey));
        //添加删除信息
        //删除单个版本
        delete.addColumn(Bytes.toBytes(family),Bytes.toBytes(column));
        //删除所有版本
        delete.addColumns(Bytes.toBytes(family),Bytes.toBytes(column));
        //删除列族
        delete.addFamily(Bytes.toBytes(family));
        //删除数据
        table.delete(delete);
        //关闭资源
        table.close();
    }
//测试
    public static void main(String[] args) throws IOException {
         //putCell("bigdate","student","1002","info","age","18");
        /*String cell[] = getCell("bigdate", "student", "1001", "info", "name");
        System.out.println(cell);*/
        /*List<String> strings = scanRows("bigdate", "student", "1001", "1003");
        for (String string : strings) {
            System.out.println(string);
        }*/
        deleteColumn("bigdate","student","1002","info","age");
    }
}
这篇关于HBase API--DDL的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!