HBase的Java API操作(五)

6 篇文章 0 订阅
5 篇文章 0 订阅

1. 前言

HBase Shell可以基于后台访问HBase,但是不如java访问写起来方便一些,所以下面实现如何基于Java API实现对HBase的远程操作。
——————————————————————————————————————————在开发之前将hbase的lib里面的jar包导入到我们的项目依赖中:
在这里插入图片描述
在这里插入图片描述

2. 创建表

实例代码:

package hbase;

import net.spy.memcached.ConnectionFactoryBuilder;
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;

public class createTable {
    public static void main(String[] args) {
        System.setProperty("HADOOP_USER_NAME", "hadoop");
        Configuration configuration = HBaseConfiguration.create();
        configuration.set("hbase.zookeeper.quorum", "slave001,slave002,slave003");
        try {
            //建立连接
            Connection connection = ConnectionFactory.createConnection(configuration);
            //获取Admin
            Admin admin = connection.getAdmin();
            //添加表的描述
            HTableDescriptor tableDescriptor = new HTableDescriptor(TableName.valueOf("test"));
            //添加列族
            HColumnDescriptor hColumnDescriptor = new HColumnDescriptor("column1");
            tableDescriptor.addFamily(hColumnDescriptor);
            admin.createTable(tableDescriptor);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

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

3. 插入数据

实例代码:

package 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 java.io.IOException;

public class addData {
    public static void main(String[] args) {
        System.setProperty("HADOOP_USER_NAME", "hadoop");
        Configuration configuration = HBaseConfiguration.create();
        configuration.set("hbase.zookeeper.quorum", "slave001,slave002,slave003");
        try {
            Connection connection = ConnectionFactory.createConnection(configuration);
            //确定连接的表
            Table table = connection.getTable(TableName.valueOf("test"));
            //设置Row Key
            Put put = new Put("lhd".getBytes());
            //写入列名及数据
            put.addColumn("column1".getBytes(),"age".getBytes(),"22".getBytes());
            table.put(put);
        } catch (IOException e) {
            e.printStackTrace();
        }

    }
}

在这里插入图片描述

4. 查询数据

查询数据分为两种方式,一种是通过get方法获取指定cell的数据值,另一种是通过Scan()方法遍历查询表中的全部数据,下面分别介绍:

  1. 查询指定cell的值,指定要查询的表名、rowkey、列族、属性。
    实例代码:
package hbase;

import javafx.scene.control.Tab;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.Cell;
import org.apache.hadoop.hbase.CellUtil;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.*;
import org.jruby.RubyGlobal;

import java.io.IOException;

public class getData {
    public static void main(String[] args) {
        System.setProperty("HADOOP_USER_NAME", "hadoop");
        Configuration configuration = HBaseConfiguration.create();
        configuration.set("hbase.zookeeper.quorum", "slave001,slave002,slave003");
        try{
            Connection connection = ConnectionFactory.createConnection(configuration);
            Table table = connection.getTable(TableName.valueOf("test"));
            //通过Row Key指定行
            Get get = new Get("lhd".getBytes());
            Result result = table.get(get);
            //指定列名,确定cell
            Cell cell = result.getColumnLatestCell("column1".getBytes(),"age".getBytes());
            //输出cell的Row Key的值
            System.out.println(new String(CellUtil.cloneRow(cell), "utf-8"));
            System.out.println(new String(CellUtil.cloneValue(cell), "utf-8"));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

效果截图:
在这里插入图片描述

  1. 遍历查询指定表的全部数据,得到结果集后并输出。
    实例代码:
package hbase;

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

import java.io.IOException;
import java.util.Iterator;

public class getDataByScan {
    public static void main(String[] args) {
        System.setProperty("HADOOP_USER_NAME", "hadoop");
        Configuration configuration = HBaseConfiguration.create();
        configuration.set("hbase.zookeeper.quorum", "slave001,slave002,slave003");
        try {
            Connection connection = ConnectionFactory.createConnection(configuration);
            Table table = connection.getTable(TableName.valueOf("test"));
            //调用Scan方法
            Scan scan = new Scan();
            //取得遍历查询的结果
            ResultScanner resultScanner = table.getScanner(scan);
            //对结果集进行迭代
            Iterator iterator = resultScanner.iterator();
            while(iterator.hasNext()){
                Result result = (Result) iterator.next();
                Cell cell = result.getColumnLatestCell("column1".getBytes(),"age".getBytes());
                System.out.println(new String(CellUtil.cloneRow(cell), "utf-8"));
                System.out.println(new String(CellUtil.cloneValue(cell),"utf-8"));

            }

        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

在这里插入图片描述

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值