HBase Java API操作(增删改查)

本文详细介绍如何使用Maven管理HBase项目,包括创建表、插入数据、查询与删除操作,以及运用多种过滤器进行数据筛选的方法。

maven进行项目的管理

      引入下面依赖的jar包  

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

<!-- 指定JDK工具包的位置,需要本地配置好环境变量JAVA_HOME-->
<dependency>
    <groupId>jdk.tools</groupId>
    <artifactId>jdk.tools</artifactId>
    <version>1.8</version>
    <scope>system</scope>
    <systemPath>${JAVA_HOME}/lib/tools.jar</systemPath>
</dependency>

 

   一:创建表t2, 列族f2,代码如下:

package org.jy.data.yh.bigdata.drools.hadoop.hbase;

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

/**
 * 创建表:在HBase中创建一张表
 * 1.4.11版本
 *
 */
public class HBaseCreateTable {
    public static void main( String[] args ) throws IOException {
        //  创建HBase配置对象
        Configuration configuration = HBaseConfiguration.create();
        // 指定zookeeper集群地址
        //configuration.set("hbase.zookeeper,quorum","node-1:1281,node-2:1281,node-3:1281"); // 使用域名一直不见创建成功输出
        configuration.set("hbase.zookeeper.quorum",
                "192.168.227.128:2181,192.168.227.129:2181,192.168.227.130:2181");
        // 创建连接对象
        Connection connection = ConnectionFactory.createConnection(configuration);
        // 得到数据库管理员对象
        Admin admin = connection.getAdmin();
        System.out.println("=========================");
        // 创建描述,并指定表名
        TableName tableName = TableName.valueOf("t2");
        HTableDescriptor hTableDescriptor = new HTableDescriptor(tableName);
        // 创建列族描述
        HColumnDescriptor family = new HColumnDescriptor("f2");
        // 指定列族
        hTableDescriptor.addFamily(family);
        // 创建表
        admin.createTable(hTableDescriptor);
        System.out.println("create table success");
        admin.close();
        connection.close();
    }
}

二:  向表t2中添加七条数据,代码如下:

package org.jy.data.yh.bigdata.drools.hadoop.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.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;

/**
 * 向t2表中添加三条数据
 */
public class HBasePutData {
    public static void main(String[] args) throws IOException {
        //  创建HBase配置对象
        Configuration configuration = HBaseConfiguration.create();
        // 指定zookeeper集群地址
        configuration.set("hbase.zookeeper.quorum",
                "192.168.227.128:2181,192.168.227.129:2181,192.168.227.130:2181");
        // 创建数据库连接对象
        Connection connection = ConnectionFactory.createConnection(configuration);
        // Table负责与记录相关的操作,如增删改查等
        TableName tableName = TableName.valueOf("t2");
        Table table = connection.getTable(tableName);
        // 设置rowKey
        Put put1 = new Put(Bytes.toBytes("row1"));
        // 添加列数据,指定列族,列名与列值
        put1.addColumn(Bytes.toBytes("f2"),Bytes.toBytes("name"),Bytes.toBytes("小明"));
        put1.addColumn(Bytes.toBytes("f2"),Bytes.toBytes("age"),Bytes.toBytes("30"));
        put1.addColumn(Bytes.toBytes("f2"),Bytes.toBytes("college"),Bytes.toBytes("北京大学"));
        put1.addColumn(Bytes.toBytes("f2"),Bytes.toBytes("address"),Bytes.toBytes("北京市海淀区"));

        Put put2 = new Put(Bytes.toBytes("row2"));
        // 添加列数据,指定列族,列名与列值
        put2.addColumn(Bytes.toBytes("f2"),Bytes.toBytes("name"),Bytes.toBytes("张三"));
        put2.addColumn(Bytes.toBytes("f2"),Bytes.toBytes("age"),Bytes.toBytes("20"));
        put2.addColumn(Bytes.toBytes("f2"),Bytes.toBytes("college"),Bytes.toBytes("贵州大学"));
        put2.addColumn(Bytes.toBytes("f2"),Bytes.toBytes("address"),Bytes.toBytes("贵州贵阳"));


        Put put3 = new Put(Bytes.toBytes("row3"));
        // 添加列数据,指定列族,列名与列值
        put3.addColumn(Bytes.toBytes("f2"),Bytes.toBytes("name"),Bytes.toBytes("李四"));
        put3.addColumn(Bytes.toBytes("f2"),Bytes.toBytes("age"),Bytes.toBytes("50"));
        put3.addColumn(Bytes.toBytes("f2"),Bytes.toBytes("college"),Bytes.toBytes("北京外国语大学"));
        put3.a
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值