Hbase工具类

博客介绍了连接Hbase之后的操作工具类,包含获取表、插入数据、查询数据、删除数据等方法,还提及了config.properties文件、pom文件、Hbase工具类和测试类,使用前提是zookeeper、hdfs、HBase开启且Hbase中有创建的表。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

连接Hbase之后的操作工具类:

方法包含:

获取表、插入单列数据、插入多列数据、根据rowkey查询数据、根据rowkey删除数据、批量数据插入

config.properties文件

#zookeeper
zookeeper.connect=node01:2181

pom文件:

 <!--远程仓库地址-->
    <repositories>
        <repository>
            <id>cloudera</id>
            <url>https://repository.cloudera.com/artifactory/cloudera-repos</url>
        </repository>
    </repositories>
    <!--CDH版本 及Hbase版本-->
    <properties>
        <cdh.version>cdh5.14.0</cdh.version>
        <hbase.version>1.2.0</hbase.version>
    </properties>
    <dependencies>
        <!--单元测试包-->
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.apache.hbase</groupId>
            <artifactId>hbase-client</artifactId>
            <version>${hbase.version}-${cdh.version}</version>
        </dependency>
    </dependencies>
读取properties文件类:ReadConfig
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

/**
 * @Author: sou1yu
 * @Email:sou1yu@aliyun.com
 */
public class ReadConfig {
    public static Properties config = new Properties();
    static {
        //类加载器加载resources下的配置文件
        InputStream rs = ReadConfig.class.getClassLoader().getResourceAsStream("config.properties");
        try {
            config.load(rs);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        System.out.println(config.getProperty("redis.host"));
    }
}

Hbase工具类

/**
 * @Author: sou1yu
 * @Email:sou1yu@aliyun.com
 */

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

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

public class HbaseUtil {

    //1.静态代码块获取连接对象
    static Connection connection = null;

    static {

        //设置zookeeper
        Configuration configuration = HBaseConfiguration.create();
        configuration.set("hbase.zookeeper.quorum", ReadConfig.config.getProperty("zookeeper.connect"));
        //获取连接对象
        try {
            connection = ConnectionFactory.createConnection(configuration);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    //2.获取表
    public static Table getTable(String tableName) {

        Table tblName = null;
        try {
            tblName = connection.getTable(TableName.valueOf(tableName));
        } catch (IOException e) {
            e.printStackTrace();
        }
        return tblName;
    }

    //3.插入单列数据
    public static void putDataByRowkey(String tableName, String family, String colName, String colValue, String rowkey) {
        //获取表
        Table table = getTable(tableName);

        try {
            //新建put对象
            Put put = new Put(rowkey.getBytes());
            //封装数据
            put.addColumn(family.getBytes(), Bytes.toBytes(colName), colValue.getBytes());

            //数据插入
            table.put(put);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                //表关闭
                table.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    //4.插入多列数据
    public static void putMapDataByRowkey(String tableName, String family, Map<String, Object> map, String rowkey) {

        Table table = getTable(tableName);
        try {
            Put put = new Put(rowkey.getBytes());
            //map的key是列名,value是列值
            for (String key : map.keySet()) {

                put.addColumn(family.getBytes(), key.getBytes(), map.get(key).toString().getBytes());
            }
            //插入
            table.put(put);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                table.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    //5.根据rowkey查询数据
    public static String queryByRowkey(String tableName, String family, String colName, String rowkey) {

        Table table = getTable(tableName);
        String str = null;
        try {
            //查询
            Get get = new Get(rowkey.getBytes());
            Result result = table.get(get);
            byte[] value = result.getValue(family.getBytes(), colName.getBytes());
            str = new String(value);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                table.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return str;
    }

    //6.根据rowkey删除数据
    public static void delByRowkey(String tableName, String family, String rowkey) {

        Table table = getTable(tableName);
        try {
            //删除对象
            Delete delete = new Delete(rowkey.getBytes());
            //添加列簇
            delete.addFamily(family.getBytes());
            table.delete(delete);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                table.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    //7.批量数据插入
    public static void putList(String tableName, List<Put> list) {

        Table table = getTable(tableName);
        try {
            table.put(list);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                table.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

}

测试类:

前提:zookeeper开启,hdfs开启 ,HBase开启,Hbase中创建的有表

import org.apache.hadoop.hbase.client.Put;
import org.junit.Test;

import java.util.ArrayList;
import java.util.HashMap;

/**
 * @Author: sou1yu
 * @Email:sou1yu@aliyun.com
 */
public class TestHbase {
    @Test
    public void test(){
        //测试插入单列数据
        HbaseUtil.putDataByRowkey("test", "f1","name","xiaoli","1");

        //插入多列数据
        HashMap<String, Object> map = new HashMap<>();
        map.put("name","xiaozhang");
        map.put("age",20);
        HbaseUtil.putMapDataByRowkey("test","f1",map,"2");

        // 根据rowkey查询数据
        String s = HbaseUtil.queryByRowkey("test", "f1", "name", "1");
        System.out.println(">>>>>>>>>>>"+s+">>>>>>>>>>");

        //根据rowkey删除数据
        HbaseUtil.delByRowkey("test","f1","2");

        //批量数据插入
        ArrayList<Put> list = new ArrayList<>();
        Put put1 = new Put("3".getBytes());
        put1.addColumn("f1".getBytes(),"name".getBytes(),"xiaohong".getBytes());

        Put put2 = new Put("4".getBytes());
        put2.addColumn("f1".getBytes(),"name".getBytes(),"xiaowang".getBytes());

        list.add(put1);
        list.add(put2);

        HbaseUtil.putList("test",list);

    }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值