HBase JAVA API 操作

35 篇文章 0 订阅
30 篇文章 0 订阅

Maven pom.xml 添加依赖

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

创建表

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HColumnDescriptor;
import org.apache.hadoop.hbase.HTableDescriptor;
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;

import java.io.IOException;

/**
 * 创建表
 */
public class CreateTable {
    /**
     * 创建表
     *
     * @param tName      表名
     * @param familyName 列族名
     * @throws IOException .
     */
    public void createTable(String tName, String familyName) throws IOException {
        //创建HBase配置对象
        Configuration conf = new Configuration();
        //指定ZooKeeper集群地址
        conf.set("hbase.zookeeper.quorum", "192.168.213.128:2181,192.168.213.129:2181,192.168.213.130:2181");
        //创建连接对象
        Connection connection = ConnectionFactory.createConnection(conf);
        //得到数据库管理员对象
        Admin admin = connection.getAdmin();
        //创建表描述,并指定表名
        TableName tableName = TableName.valueOf(tName);
        HTableDescriptor tableDescriptor = new HTableDescriptor(tableName);
        //创建列族描述
        HColumnDescriptor familyDescriptor = new HColumnDescriptor(familyName);
        //指定列族
        tableDescriptor.addFamily(familyDescriptor);
        //创建表
        admin.createTable(tableDescriptor);
    }

    public static void main(String[] args) throws IOException {
        new CreateTable().createTable("t1", "f1");
    }
}

在这里插入图片描述
在这里插入图片描述

添加数据

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.Connection;
import org.apache.hadoop.hbase.client.ConnectionFactory;
import org.apache.hadoop.hbase.client.Put;
import org.apache.hadoop.hbase.client.Table;
import org.apache.hadoop.hbase.util.Bytes;

import java.io.IOException;

/**
 * 添加数据
 */
public class PutData {
    /**
     * 添加数据
     *
     * @param tName 表名
     * @param familyName 列族名
     * @throws IOException .
     */
    public void putData(String tName, String familyName) throws IOException {
        Configuration conf = new Configuration();
        conf.set("hbase.zookeeper.quorum", "192.168.213.128:2181,192.168.213.129:2181,192.168.213.130:2181");
        Connection connection = ConnectionFactory.createConnection(conf);
        TableName tableName = TableName.valueOf(tName);
        Table table = connection.getTable(tableName);

        Put put = new Put(Bytes.toBytes("row1"));
        put.addColumn(Bytes.toBytes(familyName), Bytes.toBytes("name"), Bytes.toBytes("xiaoming"));
        put.addColumn(Bytes.toBytes(familyName), Bytes.toBytes("age"), Bytes.toBytes("20"));
        put.addColumn(Bytes.toBytes(familyName), Bytes.toBytes("address"), Bytes.toBytes("beijing"));

        Put put2 = new Put(Bytes.toBytes("row2"));
        put2.addColumn(Bytes.toBytes(familyName), Bytes.toBytes("name2"), Bytes.toBytes("xiaoming2"));
        put2.addColumn(Bytes.toBytes(familyName), Bytes.toBytes("age2"), Bytes.toBytes("22"));
        put2.addColumn(Bytes.toBytes(familyName), Bytes.toBytes("address2"), Bytes.toBytes("beijing2"));

        Put put3 = new Put(Bytes.toBytes("row3"));
        put3.addColumn(Bytes.toBytes(familyName), Bytes.toBytes("name"), Bytes.toBytes("xiaoming3"));
        put3.addColumn(Bytes.toBytes(familyName), Bytes.toBytes("age3"), Bytes.toBytes("25"));
        put3.addColumn(Bytes.toBytes(familyName), Bytes.toBytes("address3"), Bytes.toBytes("beijing3"));

        table.put(put);
        table.put(put2);
        table.put(put3);

        table.close();
    }

    public static void main(String[] args) throws IOException {
        new PutData().putData("t1", "f1");
        /***********************************************************/
        new CreateTable().createTable("t2","f2");
        new PutData().putData("t2", "f2");
        /***********************************************************/
    }
}

在这里插入图片描述

在这里插入图片描述

查询数据

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.Cell;
import org.apache.hadoop.hbase.CellUtil;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.*;

import java.io.IOException;
import java.nio.charset.StandardCharsets;

/**
 * 查询数据
 */
public class GetData {
    /**
     * 查询数据
     *
     * @param tName   表名
     * @param rowKeys rowKeys
     * @throws IOException .
     */
    public void getData(String tName, String... rowKeys) throws IOException {
        Configuration conf = new Configuration();
        conf.set("hbase.zookeeper.quorum", "192.168.213.128:2181,192.168.213.129:2181,192.168.213.130:2181");
        Connection connection = ConnectionFactory.createConnection(conf);
        TableName tableName = TableName.valueOf(tName);
        Table table = connection.getTable(tableName);

        for (String rowKey : rowKeys) {
            Get get = new Get(rowKey.getBytes(StandardCharsets.UTF_8));
            Result result = table.get(get);
            for (Cell cell : result.rawCells()) {
                //获得当前单元格所属的列族名称
                String family = new String(CellUtil.cloneFamily(cell));
                //获得当前单元格所属的列名称
                String qualifier = new String(CellUtil.cloneQualifier(cell));
                //获取当前单元格的列值
                String value = new String(CellUtil.cloneValue(cell));
                System.out.println(rowKey + "               column=" + family + ":" + qualifier + ",value=" + value);
            }
        }
    }

    public static void main(String[] args) throws IOException {
        new GetData().getData("t1", "row1", "row2", "row3");
        new GetData().getData("t2", "row1", "row2", "row3");
    }
}

在这里插入图片描述

在这里插入图片描述

删除数据

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.Connection;
import org.apache.hadoop.hbase.client.ConnectionFactory;
import org.apache.hadoop.hbase.client.Delete;
import org.apache.hadoop.hbase.client.Table;
import org.apache.hadoop.hbase.util.Bytes;

import java.io.IOException;

/**
 * 删除数据
 */
public class DeleteData {
    /**
     * 删除数据
     *
     * @param tName   表名
     * @param rowKeys rowKeys
     * @throws IOException .
     */
    public void deleteData(String tName, String... rowKeys) throws IOException {
        Configuration conf = new Configuration();
        conf.set("hbase.zookeeper.quorum", "192.168.213.128:2181,192.168.213.129:2181,192.168.213.130:2181");
        Connection connection = ConnectionFactory.createConnection(conf);
        TableName tableName = TableName.valueOf(tName);
        Table table = connection.getTable(tableName);
        for (String rowKey : rowKeys) {
            Delete delete = new Delete(Bytes.toBytes(rowKey));
            table.delete(delete);
        }
        table.close();
    }

    public static void main(String[] args) throws IOException {
        new DeleteData().deleteData("t1", "row1", "row2", "row3");
    }
}

在这里插入图片描述

在这里插入图片描述

删除表

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HColumnDescriptor;
import org.apache.hadoop.hbase.HTableDescriptor;
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;

import java.io.IOException;

/**
 * 删除表
 */
public class DeleteTable {
    /**
     * 删除表
     * 
     * @param tNames 表名列表
     * @throws IOException .
     */
    public void deleteTable(String... tNames) throws IOException {
        Configuration conf = new Configuration();
        conf.set("hbase.zookeeper.quorum", "192.168.213.128:2181,192.168.213.129:2181,192.168.213.130:2181");
        Connection connection = ConnectionFactory.createConnection(conf);
        Admin admin = connection.getAdmin();
        for (String tName : tNames) {
            TableName tableName = TableName.valueOf(tName);
            admin.disableTable(tableName);
            admin.deleteTable(tableName);
        }
    }

    public static void main(String[] args) throws IOException {
        new DeleteTable().deleteTable("t1", "t2");
    }
}

在这里插入图片描述
在这里插入图片描述

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值