Hbase Java API简单实践(附源代码解释)

详细代码及链接

  • maven依赖:hbase-client,slf4j-api,slf4j-nop(不需要hbase-server包)
  • resource中加入hdfs-site.xml配置文件(不需要core-site.xml)
  • resource中放置log4j.properties文件(HBase安装目录下conf文件中的log4j.properties)
  • 完整项目链接 ————(https://github.com/AtTops/Practice-Item/tree/per-taotao/HbaseTest)
  • 完整代码如下(SomeHbaseAPI类与APITest类)


    SomeHbaseAPI.java
import org.apache.hadoop.hbase.*;
import org.apache.hadoop.hbase.client.*;
import org.apache.hadoop.hbase.util.Bytes;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

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

/**
 * @author 王海
 * @version V1.0
 * @package per.wanghai
 * @Description
 * @Date 2017/10/29 23:23
 */
public class SomeHbaseAPI {
   
   
    private final Logger logger = LoggerFactory.getLogger(SomeHbaseAPI.class);

    protected void listTable(Admin admin) throws IOException {
        // 获得HTableDescriptors
        // (所有namespace的表,相当于scan META)
        HTableDescriptor[] tableDescriptor = admin.listTables();
        System.out.println("您的HBase有以下表:");
        for (int i = 0; i < tableDescriptor.length; i++) {
            System.out.println("表" + i + ":" + tableDescriptor[i].getNameAsString());
        }
    }

    /**
     * @param columnFamilies(这是一个变长参数,“Varargs”机制只允许一个变长参数,且必须放在最后)详见参考2
     * @throws IOException
     * @Description 该方法创建一个table实例
     */
    protected void createTable(Admin admin, TableName tableName, String... columnFamilies) throws IOException {
        try {
            if (admin.tableExists(tableName)) {
                // "{}"是slf4j的占位符(其一大特色)
                // DEBUG < INFO < WARN < ERROR < FATAL
                logger.warn("表:{}已经存在!", tableName.getNameAsString());
            } else {
                // 标注2:关于HTableDescriptor:
                HTableDescriptor tableDescriptor = new HTableDescriptor(tableName);
                for (String columnFamily : columnFamilies) {
                    tableDescriptor.addFamily(new HColumnDescriptor(columnFamily));
                }
                admin.createTable(tableDescriptor);
                logger.info("表:{}创建成功!", tableName.getNameAsString());
            }
        } finally {
            if (admin != null) {
                admin.close();
            }
        }
    }

    /**
     * @throws IOException
     * @Description 一行一行的插入数据
     * TODO:批量插入可以使用 Table.put(List<Put> list)
     */
    protected void putOneByOne(Connection connection, TableName tableName,
                               byte[] rowKey, String columnFamily, String column, String data) throws IOException {
        Table table = null;
        try {
            // 创建一个table实例
            table = connection.getTable(tableName);
            // HBase中所有的数据最终都被转化为byte[]
            // (rowKey已经在testCurd方法中转换为byte[])
            Put p = new Put(rowKey);
            // 查看源码知:put的add方法已经被弃用
            p.addColumn(Bytes.toBytes(columnFamily), Bytes.toBytes(column), Bytes.toBytes(data));
            table.put(p);
            logger.info("表:{}已更新!!!", tableName.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值