java 对hbase 数据操作

该博客主要围绕Java对HBase的操作展开,涉及信息技术领域中后端开发与大数据开发相关内容,介绍了如何运用Java语言对HBase进行操作,为相关开发提供了一定的技术参考。
package com.hbase;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.HConstants;
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.Get;
import org.apache.hadoop.hbase.client.Put;
import org.apache.hadoop.hbase.client.Result;
import org.apache.hadoop.hbase.client.ResultScanner;
import org.apache.hadoop.hbase.client.Scan;
import org.apache.hadoop.hbase.client.Table;
import org.apache.hadoop.hbase.filter.CompareFilter;
import org.apache.hadoop.hbase.filter.PageFilter;
import org.apache.hadoop.hbase.filter.SingleColumnValueFilter;
import org.apache.hadoop.hbase.util.Bytes;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.LinkedBlockingDeque;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

import static org.apache.hadoop.hbase.util.Bytes.toBytes;

/**
 * @author qt
 * @description hbase 对数据的基本操作
 * @date 2018/8/10 14:00
 */
public class HbaseDataTest {
    private static Logger LOGGER = LogManager.getLogger(HbaseDataTest. class);
    private static final String ZKADDRESS = "";

    private static Table table = null;
    private static Connection connection = null;

    static {
        Configuration config = HBaseConfiguration.create();
        config.set(HConstants.ZOOKEEPER_QUORUM, ZKADDRESS);
        try {
            ExecutorService executor = new ThreadPoolExecutor(3, 4, 5,
                    TimeUnit.SECONDS, new LinkedBlockingDeque<Runnable>());
            connection = ConnectionFactory.createConnection(config, executor);
            table = connection.getTable(TableName.valueOf("test"));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 添加数据
     */
    public static void put() throws IOException {
        Put p = new Put(toBytes("row1"));
        p.addColumn(toBytes("user_info"), toBytes("name"), toBytes("raju"));
        p.addColumn(toBytes("user_info"), toBytes("city"), toBytes("hyderabad"));
        p.addColumn(toBytes("user_info"), toBytes("designation"), toBytes("manager"));
        p.addColumn(toBytes("user_info"), toBytes("salary"), toBytes("50000"));
        table.put(p);
    }

    /**
     * 批量添加数据
     */
    public static void puts() throws IOException {
        List<Put> list = new ArrayList<>();
        Put p = new Put(toBytes("row1"));
        p.addColumn(toBytes("user_info"), toBytes("name"), toBytes("张三"));
        p.addColumn(toBytes("user_info"), toBytes("city"), toBytes("杭州"));
        p.addColumn(toBytes("user_info"), toBytes("designation"), toBytes("酷酷酷酷酷"));
        p.addColumn(toBytes("user_info"), toBytes("salary"), toBytes("50000"));
        list.add(p);
        Put p1 = new Put(toBytes("row2"));
        p1.addColumn(toBytes("user_info"), toBytes("name"), toBytes("李四"));
        p1.addColumn(toBytes("user_info"), toBytes("city"), toBytes("北京"));
        p1.addColumn(toBytes("user_info"), toBytes("designation"), toBytes("啊啊啊"));
        p1.addColumn(toBytes("user_info"), toBytes("salary"), toBytes("9555"));
        list.add(p1);
        table.put(list);
        LOGGER.warn("数据添加成功");
        table.close();

    }

    /**
     * 查询
     */
    public static void get() throws IOException {
        //获取某行
        Get get = new Get(toBytes("row1"));
        //获取某列族
        get.addFamily(toBytes("user_info"));
        //删出某列族下的某key
        get.addColumn(toBytes("user_info"), toBytes("name"));
        Result result = table.get(get);
        String name = new String(result.getValue(Bytes.toBytes("user_info"), Bytes.toBytes("name")), "UTF-8");
        String city = new String(result.getValue(Bytes.toBytes("user_info"), Bytes.toBytes("city")), "utf-8");
        LOGGER.warn("name:{}; city:{}",name, city);
    }

    /**
     * 删除
     */
    public static void delete() throws IOException {
        //删除某行
        Delete delete = new Delete(Bytes.toBytes("row1"));
        //删除某列族下的某key
        delete.addColumn(toBytes("user_info"), toBytes("name"));
        //删除列族下的所有数据
        delete.addFamily(Bytes.toBytes("user_info"));
        table.delete(delete);
        LOGGER.warn("数据删除成功");
        table.close();
    }

    /**
     * 扫描数据
     */
    public static void scan() throws IOException {
        Scan scan = new Scan();
        //获取列族user_info下的name
        scan.addColumn(Bytes.toBytes("user_info"), Bytes.toBytes("name"));
        //获取列族user_info下的city
        scan.addColumn(Bytes.toBytes("user_info"), Bytes.toBytes("city"));
        //限制起始时间
        scan.setTimeRange(System.currentTimeMillis() - 8 * 24 * 60 * 60 * 1000, System.currentTimeMillis());
        SingleColumnValueFilter scvf = new SingleColumnValueFilter(Bytes.toBytes("user_info"), Bytes.toBytes("name"),
                CompareFilter.CompareOp.EQUAL, "zhangsan".getBytes());
        //实现分页
        PageFilter pf = new PageFilter(10L);
        //添加过滤器
        scan.setFilter(scvf);
        scan.setFilter(pf);
        ResultScanner scanner = table.getScanner(scan);

        for (Result result = scanner.next(); result != null; result = scanner.next()) {
            LOGGER.warn("Found row : {}" , result);
        }
        scanner.close();
        table.close();
    }

    public static void close() throws IOException {
        if(table != null){
            table.close();
        }
        if (connection != null){
            connection.close();
        }
    }


    public static void main(String[] args) throws IOException {
        HbaseDataTest.scan();
        HbaseDataTest.close();
    }
}

原文

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值