HBase Java API 例子

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileStatus;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.hbase.*;
import org.apache.hadoop.hbase.client.*;
import org.apache.hadoop.hbase.filter.Filter;
import org.apache.hadoop.hbase.filter.PrefixFilter;
import org.apache.hadoop.hbase.util.Bytes;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import java.io.IOException;

/**
 * Created by Administrator on 2017/8/16.
 */
public class TestHBase {

    Configuration configuration = null;

    @Before
    public void setUp() throws IOException {
        configuration = HBaseConfiguration.create();
    }

    @Test
    public void testCreateTable() throws IOException {
        String tableName = "jasontest";
        HBaseAdmin admin = new HBaseAdmin(configuration);
        HTableDescriptor desc = new HTableDescriptor(tableName);
        desc.addFamily(new HColumnDescriptor("basic"));
        if(admin.tableExists(tableName)){
            System.out.println("table exist");
        }else{
            admin.createTable(desc);
            System.out.println("create table successfully.");
        }
        admin.close();
    }

    @Test
    public void testGetByRow() throws IOException {
        String tableName = "user";
        HTable table = new HTable(configuration,tableName);
        byte[] row1 = Bytes.toBytes("1000");
        Get get = new Get(row1);
        Result result = table.get(get);
        Cell[] cells = result.rawCells();
        for(Cell cell:cells){
            System.out.println(//
                    Bytes.toString(CellUtil.cloneFamily(cell))+":" //
                            + Bytes.toString(CellUtil.cloneQualifier(cell))+"->" //
                            + Bytes.toString(CellUtil.cloneValue(cell)) //
            );
        }
        table.close();
    }

    @Test
    public void testGetColumn() throws IOException {
        String tableName = "user";
        HTable table = new HTable(configuration,tableName);
        Get get = new Get(Bytes.toBytes("1000"));
        get.addColumn(Bytes.toBytes("info"),Bytes.toBytes("name"));
        get.addColumn(Bytes.toBytes("info"),Bytes.toBytes("age"));
        Result result = table.get(get);
        Cell[] cells = result.rawCells();
        for(Cell cell:cells) {
            System.out.println(
                    "Column Family is " + Bytes.toString(CellUtil.cloneFamily(cell)) + " | " //
                            + "Column is " + Bytes.toString(CellUtil.cloneQualifier(cell)) + " | " //
                            + "Value is " + Bytes.toString(CellUtil.cloneValue(cell))
            );
        }
        table.close();
    }

    @Test
    public void testScanTable() throws IOException {
        String tableName = "user";
        HTable table = new HTable(configuration,tableName);
        Scan scan = new Scan();
//        scan.setStartRow(Bytes.toBytes("1001"));
//        scan.setStopRow(Bytes.toBytes("1001"));
//        scan.addColumn(Bytes.toBytes("info"),Bytes.toBytes("name"));
        Filter filer = new PrefixFilter(Bytes.toBytes("1000"));
        scan.setFilter(filer);
        ResultScanner resultScanner = table.getScanner(scan);
        for(Result result:resultScanner){
            Cell[] cells = result.rawCells();
            for(Cell cell:cells){
                System.out.println( //
                                Bytes.toString(CellUtil.cloneRow(cell))+":"//
                                + Bytes.toString(CellUtil.cloneFamily(cell))+":" //
                                + Bytes.toString(CellUtil.cloneQualifier(cell))+"->" //
                                + Bytes.toString(CellUtil.cloneValue(cell)) //
                );
            }
        }
        table.close();
    }

    @Test
    public void testPut() throws IOException {
        String tableName = "user";
        HTable table = new HTable(configuration,tableName);
        Put put = new Put(Bytes.toBytes("1004"));
        put.add(Bytes.toBytes("info"),Bytes.toBytes("name"),Bytes.toBytes("Jason Zheng"));
        put.add(Bytes.toBytes("info"),Bytes.toBytes("age"),Bytes.toBytes(28));
        table.put(put);
        table.close();
    }

    @Test
    public void testDelete() throws IOException {
        String tableName = "user";
        HTable table = new HTable(configuration,tableName);
        Delete delete = new Delete(Bytes.toBytes("1004"));
        delete.deleteColumns(Bytes.toBytes("info"),Bytes.toBytes("name"));
        delete.deleteColumns(Bytes.toBytes("info"),Bytes.toBytes("age"));
//        delete.deleteFamily(Bytes.toBytes("info"));
        table.delete(delete);
        table.close();
    }

}
时间: 2024-08-27 20:38:02

HBase Java API 例子的相关文章

Hbase java API 调用详解

Hbase java API 调用 一. hbase的安装 参考:http://blog.csdn.net/mapengbo521521/article/details/41777721 二.hbase访问方式 Native java api:最常规最高效的访问方式. Hbase shell:hbase的命令行工具,最简单的接口,适合管理员使用 Thrift gateway:利用thrift序列化结束支持各种语言,适合异构系统在线访问 Rest gateway:支持rest风格的http api

HBase Java API使用(一)

前言 1. 创建表:(由master完成) 首先需要获取master地址(master启动时会将地址告诉zookeeper)因而客户端首先会访问zookeeper获取master的地址 client和master通信,然后有master来创建表(包括表的列簇,是否cache,设置存储的最大版本数,是否压缩等). 2. 读写删除数据 client与regionserver通信,读写.删除数据 写入和删除数据时讲数据打上不同的标志append,真正的数据删除操作在compact时发生 3. 版本信息

HBase Java API使用

概括 1. 创建.删除及启用禁用表.添加列等都需用到HBaseAdmin,另外需要注意删除,添加列等操作都需要禁用表 2. 表中添加数据,查询等都是和HTable相关,如果是多线程的情况下注意用HTablePool 3.  插入数据使用Put,可以单行添加也可批量添加 4. 查询数据需使用Get,Result,Scan.ResultScanner等 一.HBaseConfiguration org.apache.hadoop.hbase.HBaseConfiguration 对HBase进行配置

hbase java api样例(版本1.3.1,新API)

验证了如下几种java api的使用方法. 1.创建表 2.创建表(预分区) 3.单条插入 4.批量插入 5.批量插入(写缓存) 6.单条get 7.批量get 8.简单scan 具体请参考GitHub. https://github.com/quchunhui/hbase_sample pom.xml文件 <?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://mave

Hbase java api

本文中使用的是最原始的java api, 没有使用spring-data-hbase,只是使用spring管理Hbase的配置 查询操作分为如下几个步骤: 1. 获取Hbase配置,这里的配置主要指hbase的地址.如果是Zookeeper管理的,可以使用Zookeeper的地址和端口 2. 根据配置获取Hbase连接: connection = ConnectionFactory.createConnection(this.hbaseConfig.gethBaseConfiguration()

【Hbase学习之三】Hbase Java API

环境 虚拟机:VMware 10 Linux版本:CentOS-6.5-x86_64 客户端:Xshell4 FTP:Xftp4 jdk8 hadoop-2.6.5 hbase-0.98.12.1-hadoop2 建立一个java工程 导入hadoop 相关jar导入hbase相关jar 使用客户端(java API)操作hbase 示例一 package hbase; import java.text.SimpleDateFormat; import java.util.ArrayList;

HBase学习(十一)hbase Java API 介绍及使用示例

几个相关类与HBase数据模型之间的对应关系 java类 HBase数据模型 HBaseAdmin 数据库(DataBase) HBaseConfiguration HTable 表(Table) HTableDescriptor 列族(Column Family) Put 列修饰符(Column Qualifier) Get Scanner 一.HBaseConfiguration 关系:org.apache.hadoop.hbase.HBaseConfiguration 作用:对HBase进

HBase Java API类介绍

几个相关类与HBase数据模型之间的对应关系 java类 HBase数据模型 HBaseAdmin 数据库(DataBase) HBaseConfiguration HTable 表(Table) HTableDescriptor 列族(Column Family) Put 列修饰符(Column Qualifier) Get Scanner 一.HBaseConfiguration 关系:org.apache.hadoop.hbase.HBaseConfiguration 作用:对HBase进

HBase总结(十一)hbase Java API 介绍及使用示例

几个相关类与HBase数据模型之间的对应关系 java类 HBase数据模型 HBaseAdmin 数据库(DataBase) HBaseConfiguration HTable 表(Table) HTableDescriptor 列族(Column Family) Put 列修饰符(Column Qualifier) Get Scanner 一.HBaseConfiguration 关系:org.apache.hadoop.hbase.HBaseConfiguration 作用:对HBase进