/**
 * 功能:测试Hbase基本的增删改查操作
 * Created by liuhuichao on 2016/12/5.
 */
public class HbaseCRUDTest {
    public static Configuration configuration;

    static{
        configuration= HBaseConfiguration.create();
        configuration.set("hbase.zookeeper.quorum","lhc-centos");
    }

    /**
     * 测试创建student表:测试已通过
     * @throws IOException
     */
    @Test
    public void createTable() throws IOException {
        HBaseAdmin admin=new HBaseAdmin(configuration); //HBaseAdmin负责管理HBase集群,添加和丢弃表
        if(admin.tableExists("studentInfo")){
            System.out.println("student表已经存在");
            return;
        }
        HTableDescriptor descriptor=new HTableDescriptor("studentInfo");
        descriptor.addFamily(new HColumnDescriptor("Name"));//创建列族,名字是Name
        descriptor.addFamily(new HColumnDescriptor("Address"));//创建列族,名字是Address
        admin.createTable(descriptor); //创建表
        System.out.println("student表创建成功!!!");
    }

    /**
     * 功能:想hbase中插入一行记录 --测试已通过
     * @throws IOException
     */
    @Test
    public void insertHbaseStudentTable() throws IOException {
        HTable table=new HTable(configuration, Bytes.toBytes("studentInfo"));
        Put put=new Put(Bytes.toBytes("1"));
        put.addColumn(Bytes.toBytes("Name"),Bytes.toBytes("firstName"),Bytes.toBytes("liu"));
        put.addColumn(Bytes.toBytes("Name"),Bytes.toBytes("secondName"),Bytes.toBytes("huichao"));
        put.addColumn(Bytes.toBytes("Address"),Bytes.toBytes("province"),Bytes.toBytes("hebei"));
        put.addColumn(Bytes.toBytes("Address"),Bytes.toBytes("city"),Bytes.toBytes("baoding"));
        put.addColumn(Bytes.toBytes("Address"),Bytes.toBytes("area"),Bytes.toBytes("qingyuan"));
        table.put(put);
    }

    /**
     * 功能:根据行健获取数据
     * @throws IOException
     */
    @Test
    public void getDataByRowKey() throws IOException {
        HTable table=new HTable(configuration, Bytes.toBytes("studentInfo"));
        Get get=new Get(Bytes.toBytes("1"));
        Result result=table.get(get);
        for(KeyValue kv :result.list()){
            System.out.println("family:"+Bytes.toString(kv.getFamilyArray()));//所属列族名称
            System.out.println("qualifier:"+Bytes.toString(kv.getQualifier()));//列名称
            System.out.println("value:"+Bytes.toString(kv.getValue()));//存储的值
            System.out.println("Timestamp:"+kv.getTimestamp());//获取时间戳
        }
    }

    /**
     * 功能:测试全表扫描
     * @throws IOException
     */
    @Test
    public void selectHBaseScan() throws IOException {
        HTable table=new HTable(configuration, Bytes.toBytes("studentInfo"));
        /*遍历查询*/
        Scan scan=new Scan();
        ResultScanner rs=null;
        try {
            rs=table.getScanner(scan);
            for(Result result : rs){
                for(KeyValue kv : result.list()){
                    System.out.println("family:"+Bytes.toString(kv.getFamilyArray()));//所属列族名称
                    System.out.println("qualifier:"+Bytes.toString(kv.getQualifier()));//列名称
                    System.out.println("value:"+Bytes.toString(kv.getValue()));//存储的值
                    System.out.println("Timestamp:"+kv.getTimestamp());//获取时间戳
                }
            }
        }finally {
            rs.close();
        }
    }

    /**
     * 更新
     * @throws Exception
     */
    @Test
    public void updateHBase() throws  Exception{
        HTable table=new HTable(configuration,Bytes.toBytes("studentInfo"));
        Put put=new Put(Bytes.toBytes("1")); //设置行健
        put.add(Bytes.toBytes("Address"),Bytes.toBytes("city"),Bytes.toBytes("beijing"));///更新的时候找对族名和列名,再给定新的value值就可以了
        table.put(put);
    }

    /**
     * 功能:查询nickname的多个(本示例为2个)版本值.
     * @throws Exception
     */
    @Test
    public void selectSomeVersion() throws  Exception{
        HTable table=new HTable(configuration,Bytes.toBytes("studentInfo"));
        Get get=new Get(Bytes.toBytes("1"));
        get.addColumn(Bytes.toBytes("Address"),Bytes.toBytes("city"));
       // get.setMaxVersions(3);
        List<KeyValue> results=table.get(get).list();
        int total=results.size();
        System.out.println("Address列族中city列的各个版本值");
        for(int i=0;i<total;i++){
            System.out.println(Bytes.toString(results.get(i).getValue()));
        }
    }

    /**
     * 功能:删除指定的某一行
     * @throws Exception
     */
    @Test
    public void deleteColumn() throws  Exception{
        HTable table = new HTable(configuration, Bytes.toBytes("studentInfo"));//HTabel负责跟记录相关的操作如增删改查等
        Delete deleteAll = new Delete(Bytes.toBytes("1"));
        table.delete(deleteAll);
    }

    /**
     * 功能:删除表
     * @throws Exception
     */
    @Test
    public void deleteTable() throws Exception{
        HBaseAdmin admin=new HBaseAdmin(configuration); //HBaseAdmin负责管理HBase集群,添加和丢弃表
        admin.disableTable("student");
        admin.deleteTable("student");
    }

}

使用Java Api 对HBase进行简单操作的更多相关文章

  1. Hbase框架原理及相关的知识点理解、Hbase访问MapReduce、Hbase访问Java API、Hbase shell及Hbase性能优化总结

    转自:http://blog.csdn.net/zhongwen7710/article/details/39577431 本blog的内容包含: 第一部分:Hbase框架原理理解 第二部分:Hbas ...

  2. 通过Java Api与HBase交互(转)

    HBase提供了Java Api的访问接口,掌握这个就跟Java应用使用RDBMS时需要JDBC一样重要,本文将继续前两篇文章中blog表的示例,介绍常用的Api. import java.io.IO ...

  3. Java使用poi对Execl简单操作_总结

    poi是Apache组织给开发者提供一套操作office(Execl,Word,PowerPoint)等Java API,开发者通过Poi API可以快速的操作office办公软件,以上3篇博文只是一 ...

  4. HBase总结(十二)Java API 与HBase交互实例

    HBase提供了Java Api的訪问接口,掌握这个就跟Java应用使用RDBMS时须要JDBC一样重要 import java.io.IOException; import org.apache.h ...

  5. 通过Java Api与HBase交互

    HBase提供了Java Api的访问接口,掌握这个就跟Java应用使用RDBMS时需要JDBC一样重要,本文将继续前两篇文章中blog表的示例,介绍常用的Api. import java.io.IO ...

  6. JAVA API访问Hbase org.apache.hadoop.hbase.client.RetriesExhaustedException: Failed after attempts=32

    Java使用API访问Hbase报错: 我的hbase主节点是spark1   java代码访问hbase的时候写的是ip 结果运行程序报错 不能够识别主机名 修改主机名     修改主机hosts文 ...

  7. 使用JAVA API编程实现简易Habse操作

    使用JAVA API编程实现下面内容: 1.创建<王者荣耀>游戏玩家信息表gamer,包含列族personalInfo(个人信息).recordInfo(战绩信息).assetsInfo( ...

  8. Java Api与HBase交互实例

    import java.io.IOException; import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.hb ...

  9. Java中对session的简单操作

    1.jsp中操作session <% String name=(String)request.getSession().getAttribute("username"); / ...

随机推荐

  1. shouldRasterize 光栅化、(缓存)复用、内存、内容稳定

    使用场景:稳定的视图(layer)被反复使用或进行动画: 本质:牺牲内存解放cpu: 反例:牺牲内存牺牲cpu. 单次使用或者视图有变动,shouldRasterize不会有任何用途,反而会牺牲内存. ...

  2. freemarker模板加载TemplateLoader常见方式

    使用过freemarker的肯定其见过如下情况: java.io.FileNotFoundException: Template xxx.ftl not found. 模板找不到.可能你会认为我明明指 ...

  3. 淡说Linux 的发展史

    ♦ 1  Linux的简单介绍 Linux与Windows一样都是一套OS(操作系统),Windows界面美观 ,普通用户很容易上手,点点鼠标就能搞定许多操作,而Linux生下来就是为程序员的,故精通 ...

  4. Bootstrap--模仿官网写一个页面

    本文参考Bootstrap官方文档写了简单页面来熟悉Bootstrap的栅格系统.常用CSS样.Javascript插件和部分组件. 以下html代码可以直接复制本地运行: BootstrapPage ...

  5. STM8 亮灯程序

    开发环境:ST Visual Develop+STM32 ST-LINK Utility+开发板 原理:定时向指定针脚输出高电平信号 /* MAIN.C file * * Copyright (c) ...

  6. linux挂在新硬盘

    login as: rootroot@192.168.109.128's password:Last login: Fri Mar 22 14:12:08 2019 from 192.168.109. ...

  7. 接口测试Jmeter+Fiddler组合

    接口测试Jmeter+Fiddler组合 在使用完Jmeter在做接口测试之后,个人感觉Jmeter比loadrunner好用,原因是界面操作更加直观,不必像loadrunner在写接口请求函数的时候 ...

  8. java alibaba fastJson 遍历数组json

    import java.util.*; import com.alibaba.fastjson.*; public class Test { public static void main(Strin ...

  9. 小程序 组件 Component

    一.组件模板和样式 类似于页面,自定义组件拥有自己的 wxml 和模板 wxss 样式. 1.组件模板 组件的写法和页面的写法相同,组件模板与组件数据结合后生成的数节点, 将被插入到组件的引用位置.在 ...

  10. Java编程基础知识总结大全(Ⅰ)

    Java是一门面向对象编程语言,不仅吸收了C++语言的各种优点,还摒弃了C++里难以理解的多继承.指针等概念,因此Java语言具有功能强大和简单易用两个特征.Java语言作为静态面向对象编程语言的代表 ...