使用Java Api 对HBase进行简单操作

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




/**
 * 功能:测试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");
    }

}







目录
相关文章
|
2月前
|
Java API 数据处理
Java新特性:使用Stream API重构你的数据处理
Java新特性:使用Stream API重构你的数据处理
|
2月前
|
Java 大数据 API
Java Stream API:现代集合处理与函数式编程
Java Stream API:现代集合处理与函数式编程
238 100
|
2月前
|
Java API 数据处理
Java Stream API:现代集合处理新方式
Java Stream API:现代集合处理新方式
267 101
|
2月前
|
并行计算 Java 大数据
Java Stream API:现代数据处理之道
Java Stream API:现代数据处理之道
241 101
|
3月前
|
存储 Java API
Java Stream API:现代数据处理之道
Java Stream API:现代数据处理之道
367 188
|
3月前
|
存储 Java API
Java Stream API:现代数据处理之道
Java Stream API:现代数据处理之道
272 92
|
5月前
|
缓存 安全 网络协议
借助Java,让Cloudflare API为你的网站管理加速
在数字化时代,网站与应用的稳定运行至关重要。Cloudflare API作为得力助手,可高效管理网站功能。本文深入探讨基于Java的Cloudflare API自动化操作,涵盖DNS管理、防火墙配置、缓存清理、SSL/TLS设置及Worker脚本部署等核心功能。同时,提供环境准备、认证配置、最佳实践(如请求重试与批量优化)、错误处理及安全增强措施的详细指导。通过这些步骤,构建稳定高效的自动化管理系统,助力网站稳健前行。
200 0
|
4月前
|
Oracle Java 关系型数据库
掌握Java Stream API:高效集合处理的利器
掌握Java Stream API:高效集合处理的利器
393 80
|
4月前
|
安全 Java API
Java 8 Stream API:高效集合处理的利器
Java 8 Stream API:高效集合处理的利器
290 83
|
2月前
|
安全 Java API
使用 Java 构建强大的 REST API 的四个基本技巧
本文结合探险领域案例,分享Java构建REST API的四大核心策略:统一资源命名、版本控制与自动化文档、安全防护及标准化异常处理,助力开发者打造易用、可维护、安全可靠的稳健API服务。
197 2