热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

hbaseJavaAPI操作实例

DDL(创建及删除表格)如何在Hbase中创建表格以及删除表格。可通过Java和HbaseShell两种方法实现。创建表格HBase中表格的创建是通过对操作HBaseAdmin这一对象使其调用createTable()这一方法来实现。其中HTableDescriptor描述了表的schema,可在其上通过

DDL(创建及删除表格) 如何在Hbase中创建表格以及删除表格。可通过Java和Hbase Shell两种方法实现。 创建表格 HBase中表格的创建是通过对操作HBaseAdmin这一对象使其调用createTable()这一方法来实现。 其中HTableDescriptor描述了表的schema,可在其上通过

DDL(创建及删除表格)

如何在Hbase中创建表格以及删除表格。可通过Java和Hbase Shell两种方法实现。

创建表格

HBase中表格的创建是通过对操作HBaseAdmin这一对象使其调用createTable()这一方法来实现。

其中HTableDescriptor描述了表的schema,可在其上通过addFamily()这一方法增加列族。

以下Java代码实现了建立一张简易的Hbase表格‘table1’,该表有两个列族,分别为f1和f2。

public class createTable{
    private static Configuration config;
    private static HBaseAdmin ha;
    public static void main(String[] args){ 
        try{
            cOnfig= HBaseConfiguration.create();
            config.addResource("core-site.xml");
            config.addResource("hdfs-site.xml");
            config.addResource("yarn-site.xml");
            config.addResource("mapred-site.xml");
            ha = new HBaseAdmin(config);
            //create table descriptor
            String tableName = "table1";
            HTableDescriptor htd = new HTableDescriptor(Bytes.toBytes(tableName));
            //create and configure column families
            HColumnDescriptor hcd1 = new HColumnDescriptor(Bytes.toBytes("family1"));
            hcd1.setBlocksize(65536);  
            hcd1.setMaxVersions(1); 
            hcd1.setBloomFilterType(BloomType.ROW); 
            hcd1.setCompressionType(Algorithm.SNAPPY);          
            hcd1.setDataBlockEncoding(DataBlockEncoding.PREFIX); 
            hcd1.setTimeToLive(36000);
            hcd1.setInMemory(false);
            HColumnDescriptor hcd2 = new HColumnDescriptor(Bytes.toBytes("family2"));
            hcd2.setBlocksize(65536);
            hcd2.setMaxVersions(1); 
            hcd2.setBloomFilterType(BloomType.ROW); 
            hcd2.setCompressionType(Algorithm.SNAPPY);          
            hcd2.setDataBlockEncoding(DataBlockEncoding.PREFIX); 
            hcd2.setTimeToLive(36000);
            hcd2.setInMemory(false);
            //add column families to table descriptor
            htd.addFamily(hcd1);
            htd.addFamily(hcd2);
            //create table
            ha.createTable(htd); 
            System.out.println("Hbase table created.");
        }catch (TableExistsException e){
            System.out.println("ERROR: attempting to create existing table!");
        }catch (IOException e){
            e.printStackTrace();
        }finally{
            try{
                ha.close();
            }catch(IOException e){
                e.printStackTrace();
            }
        }
    }
}

在Hbase Shell中,创建表格功能由create ‘Hbase表名’,[‘列族名’...]来实现。

例如,create ‘table1’,‘family1’,‘family2’同样可创建上述表格。

删除表格

删除表也是通过HBaseAdmin来操作,删除表之前首先要disable表。这是一个比较耗时的操作,所以不建议频繁删除表。

以下Java代码实现了对表格“table1”的删除操作:

public class deleteTable{
    private static Configuration config;
    private static HBaseAdmin ha;
    public static void main(String[] args){
        try{
            cOnfig= HBaseConfiguration.create(); 
            config.addResource("core-site.xml");
            config.addResource("hdfs-site.xml");
            config.addResource("yarn-site.xml");
            config.addResource("mapred-site.xml");           
            ha = new HBaseAdmin(config);
            String tableName = "table1";
            //Only an existing table can be dropped
            if (ha.tableExists(tableName)){
                //read&write denied
                ha.disableTable(tableName);
                ha.deleteTable(tableName);
                System.out.println("Hbase table dropped!");
            }
        }catch(IOException e){
            e.printStackTrace();
        }finally{
            try{
                ha.close();
            }catch(IOException e){
                e.printStackTrace();
            }
        }
    }
}

在Hbase Shell中,删除表格功能由drop ‘Hbase表名’来实现。

例如,先disable ‘table1’再drop ‘table1’同样可删除上述表格。

数据插入

在Java操作中,put方法被用做插入数据。

put方法可以传递单个Put对象: public void put(Put put) throws IOException,也可以对很多Put对象进行批量插入: public void put(List puts) throws IOException

以下Java代码实现了对表格"table1"的批量数据插入操作。插入数据后,表格有10000行,列族“family1”,“family2”中都包含“q1”,“q2”两个列,其中列族“family1”储存整型数据(int),列族“family2”储存字符串(string)。

ATTENTION:虽然Hbase支持多种类型储存,但为了应用高性能优化的hbase,表格值的储存类型建议一致使用为String。如上例所示,“family1:q1”中原为整数类型,须转制成string后再录入表中

public class insertTable{
    private static Configuration config;
    public static void main(String[] args) throws IOException{
        cOnfig= HBaseConfiguration.create();
        config.addResource("core-site.xml");
        config.addResource("hdfs-site.xml");
        config.addResource("yarn-site.xml");
        config.addResource("mapred-site.xml");
        String tableName = "table1";
        HTable table = new HTable(config, tableName);
        //set AutoFlush
        table.setAutoFlush(true);
        int count = 10000;
        String familyName1 = "family1";
        String familyName2 = "family2";
        String qualifier1 = "q1";
        String qualifier2 = "q2";
        //data to be inserted
        String[] f1q1 = new String[count];
        String[] f1q2 = new String[count];
        String[] f2q1 = new String[count];
        String[] f2q2 = new String[count];
        for(int i = 0; i 

在Hbase Shell中,单条数据插入功能由put ‘Hbase表名’,‘rowKey’,‘列族名:列名’,‘数据值’来实现。

数据查询

Hbase表格的数据查询可分为单条查询与批量查询。

单条查询

单条查询是通过匹配rowkey在表格中查询某一行的数据。在Java中可通过get()这一方法来实现。
下列Java代码实现了在表格“table1”中取出指定rowkey一行的所有列的数据:

public class getFromTable{
    private static Configuration config;
    public static void main(String[] args) throws IOException{
        String tableName = "table1";
        cOnfig= HBaseConfiguration.create();
        config.addResource("core-site.xml");
        config.addResource("hdfs-site.xml");
        config.addResource("yarn-site.xml");
        config.addResource("mapred-site.xml"); 
        HTable table = new HTable(config, tableName);
        Get get = new Get(Bytes.toBytes("Row01230"));
        //add target columns for get
        get.addColumn(Bytes.toBytes("family1"), Bytes.toBytes("q1"));
        get.addColumn(Bytes.toBytes("family1"), Bytes.toBytes("q2")); 
        get.addColumn(Bytes.toBytes("family2"), Bytes.toBytes("q1"));
        get.addColumn(Bytes.toBytes("family2"), Bytes.toBytes("q2")); 
        Result result =  table.get(get);
        //get results
        byte[] rowKey = result.getRow();
        byte[] val1 = result.getValue(Bytes.toBytes("family1"), Bytes.toBytes("q1"));            
        byte[] val2 = result.getValue(Bytes.toBytes("family1"),Bytes.toBytes("q2"));
        byte[] val3 = result.getValue(Bytes.toBytes("family2"), Bytes.toBytes("q1"));
        byte[] val4 = result.getValue(Bytes.toBytes("family2"), Bytes.toBytes("q2")); 
        System.out.println("Row key: " + Bytes.toString(rowKey));
        System.out.println("value1: " + Bytes.toString(val1));               
        System.out.println("value2: " + Bytes.toString(val2)); 
        System.out.println("value3: " + Bytes.toString(val3));               
        System.out.println("value4: " + Bytes.toString(val4));
        table.close();
    }
}

在Hbase Shell中,单条数据查找功能由get ‘Hbase表名’,‘rowKey’,‘列族名:列名’来实现。

批量查询

批量查询是通过制定一段rowkey的范围来查询。可通过Java中getScanner()这一方法来实现。
下列Java代码实现了在表格“table1”中取出指定一段rowkey范围的所有列的数据:

public class scanFromTable {
    private static Configuration config;
    public static void main(String[] args) throws IOException{
        cOnfig= HBaseConfiguration.create();
        config.addResource("core-site.xml");
        config.addResource("hdfs-site.xml");
        config.addResource("yarn-site.xml");
        config.addResource("mapred-site.xml");
        String tableName = "table1";
        HTable table = new HTable(config, tableName);
        //Scan according to rowkey range
        Scan scan = new Scan();
        //set starting row(included), if not set, start from the first row
        scan.setStartRow(Bytes.toBytes("Row01000"));
        //set stopping row(excluded), if not set, stop at the last row 
        scan.setStopRow(Bytes.toBytes("Row01100"));
        //specify columns to scan, if not specified, return all columns; 
        scan.addColumn(Bytes.toBytes("family1"), Bytes.toBytes("q1"));
        scan.addColumn(Bytes.toBytes("family1"), Bytes.toBytes("q2"));
        scan.addColumn(Bytes.toBytes("family2"), Bytes.toBytes("q1"));
        scan.addColumn(Bytes.toBytes("family2"), Bytes.toBytes("q2"));
        //specify maximum versions for one cell, if called without arguments, get all versions, if not called, get only the latest version
        scan.setMaxVersions();
        //specify maximum number of cells to avoid OutOfMemory error caused by huge amount of data in a single row
        scan.setBatch(10000);
        ResultScanner rs = table.getScanner(scan);
        for(Result r:rs){
            byte[] rowKey = r.getRow();
            byte[] val1 = r.getValue(Bytes.toBytes("family1"), Bytes.toBytes("q1"));
            byte[] val2 = r.getValue(Bytes.toBytes("family1"), Bytes.toBytes("q2"));
            byte[] val3 = r.getValue(Bytes.toBytes("family2"), Bytes.toBytes("q1"));
            byte[] val4 = r.getValue(Bytes.toBytes("family2"), Bytes.toBytes("q2"));
            System.out.print(Bytes.toString(rowKey)+": ");
            System.out.print(Bytes.toString(val1)+" ");
            System.out.print(Bytes.toString(val2)+" ");
            System.out.print(Bytes.toString(val3)+" ");
            System.out.println(Bytes.toString(val4));
        }
        rs.close();
        table.close();
    }
}   

在Hbase Shell中,批量数据查找功能由scan ‘Hbase表名’,{COLUMNS=>‘列族名:列名’,STARTROW=>‘起始rowkey’,STOPROW=>‘终止rowkey’}来实现。

利用过滤器筛选

过滤器是在Hbase服务器端上执行筛选操作,可以应用到行键(RowFilter),列限定符(QualifierFilter)以及数据值(ValueFilter)。

这里列举了两个常用的过滤器:RowFilter和SingleColumnValueFilter。

RowFilter

RowFilter通过行键(rowkey)来筛选数据。

其中BinaryComparator直接比较两个byte array,可选的比较符(CompareOp)有EQUAL,NOT_EQUAL,GREATER,GREATER_OR_EQUAL,LESS,LESS_OR_EQUAL。

public class rowFilter{
    public static void main(String[] args) throws IOException{
        String tableName = "table1";
        Configuration cOnfig= HBaseConfiguration.create();
        config.addResource("core-site.xml");
        config.addResource("hdfs-site.xml");
        config.addResource("yarn-site.xml");
        config.addResource("mapred-site.xml");
        HTable table = new HTable(config, tableName);
        Scan scan = new Scan();
        scan.addColumn(Bytes.toBytes("family1"), Bytes.toBytes("q1"));
        Filter filter = new RowFilter(CompareFilter.CompareOp.EQUAL, new BinaryComparator(Bytes.toBytes("Row01234")));
        scan.setFilter(filter);
        ResultScanner scanner = table.getScanner(scan);
        for(Result res:scanner){
            byte[] value = res.getValue(Bytes.toBytes("family1"),Bytes.toBytes("q1"));         
            System.out.println(new String(res.getRow())+" value is: "+Bytes.toString(value));
        }
        scanner.close();
        table.close();    
    }
}

SingleColumnValueFilter

SingleColumnValueFilter对某一具体列的值进行筛选。

其中SubstringComparator检查给定的字符串是否是列值的子字符串,可选的比较符(CompareOp)有EQUAL和NOT_EQUAL。

public class singleColumnValueFilter{
    public static void main(String[] args) throws IOException{
        Configuration cOnfig= HBaseConfiguration.create();
        config.addResource("core-site.xml");
        config.addResource("hdfs-site.xml");
        config.addResource("yarn-site.xml");
        config.addResource("mapred-site.xml"); 
        String tableName = "table1";
        HTable table = new HTable(config,tableName);     
        SingleColumnValueFilter filter = new SingleColumnValueFilter(
                Bytes.toBytes("family2"),
                Bytes.toBytes("q1"),
                CompareFilter.CompareOp.NOT_EQUAL,
                new SubstringComparator("45"));
        //when setting setFilterIfMissing(true), rows with "null" values are filtered
        filter.setFilterIfMissing(true);
        Scan scan = new Scan();
        scan.setFilter(filter);
        ResultScanner scanner = table.getScanner(scan);
        for (Result res:scanner){
            byte[] val = res.getValue(Bytes.toBytes("family1"), Bytes.toBytes("q1"));
            System.out.println(new String(res.getRow()));
            System.out.println("value: " + Bytes.toString(val)); 
        }
        scanner.close();
        table.close();
    }
}
推荐阅读
  • Kylin 单节点安装
    软件环境Hadoop:2.7,3.1(sincev2.5)Hive:0.13-1.2.1HBase:1.1,2.0(sincev2.5)Spark(optional)2.3.0K ... [详细]
  • MR程序的几种提交运行模式本地模型运行1在windows的eclipse里面直接运行main方法,就会将job提交给本地执行器localjobrunner执行-- ... [详细]
  • 本文介绍了使用AJAX的POST请求实现数据修改功能的方法。通过ajax-post技术,可以实现在输入某个id后,通过ajax技术调用post.jsp修改具有该id记录的姓名的值。文章还提到了AJAX的概念和作用,以及使用async参数和open()方法的注意事项。同时强调了不推荐使用async=false的情况,并解释了JavaScript等待服务器响应的机制。 ... [详细]
  • 本文介绍了Hyperledger Fabric外部链码构建与运行的相关知识,包括在Hyperledger Fabric 2.0版本之前链码构建和运行的困难性,外部构建模式的实现原理以及外部构建和运行API的使用方法。通过本文的介绍,读者可以了解到如何利用外部构建和运行的方式来实现链码的构建和运行,并且不再受限于特定的语言和部署环境。 ... [详细]
  • HDFS2.x新特性
    一、集群间数据拷贝scp实现两个远程主机之间的文件复制scp-rhello.txtroothadoop103:useratguiguhello.txt推pushscp-rr ... [详细]
  • Android开发实现的计时器功能示例
    本文分享了Android开发实现的计时器功能示例,包括效果图、布局和按钮的使用。通过使用Chronometer控件,可以实现计时器功能。该示例适用于Android平台,供开发者参考。 ... [详细]
  • 本文介绍了使用cacti监控mssql 2005运行资源情况的操作步骤,包括安装必要的工具和驱动,测试mssql的连接,配置监控脚本等。通过php连接mssql来获取SQL 2005性能计算器的值,实现对mssql的监控。详细的操作步骤和代码请参考附件。 ... [详细]
  • iOS超签签名服务器搭建及其优劣势
    本文介绍了搭建iOS超签签名服务器的原因和优势,包括不掉签、用户可以直接安装不需要信任、体验好等。同时也提到了超签的劣势,即一个证书只能安装100个,成本较高。文章还详细介绍了超签的实现原理,包括用户请求服务器安装mobileconfig文件、服务器调用苹果接口添加udid等步骤。最后,还提到了生成mobileconfig文件和导出AppleWorldwideDeveloperRelationsCertificationAuthority证书的方法。 ... [详细]
  • 本文讨论了如何使用Web.Config进行自定义配置节的配置转换。作者提到,他将msbuild设置为详细模式,但转换却忽略了带有替换转换的自定义部分的存在。 ... [详细]
  • Hadoop源码解析1Hadoop工程包架构解析
    1 Hadoop中各工程包依赖简述   Google的核心竞争技术是它的计算平台。Google的大牛们用了下面5篇文章,介绍了它们的计算设施。   GoogleCluster:ht ... [详细]
  • mapreduce源码分析总结
    这篇文章总结的非常到位,故而转之一MapReduce概述MapReduce是一个用于大规模数据处理的分布式计算模型,它最初是由Google工程师设计并实现的ÿ ... [详细]
  • 对于开源的东东,尤其是刚出来不久,我认为最好的学习方式就是能够看源代码和doc,測试它的样例为了方便查看源代码,关联导入源代 ... [详细]
  • 伸缩性|发生_分布式文件系统设计,该从哪些方面考虑?
    篇首语:本文由编程笔记#小编为大家整理,主要介绍了分布式文件系统设计,该从哪些方面考虑?相关的知识,希望对你有一定的参考价值。点击上方关注“ ... [详细]
  •        在搭建Hadoop环境之前,请先阅读如下博文,把搭建Hadoop环境之前的准备工作做好,博文如下:       1、CentOS6.7下安装JDK,地址:http:b ... [详细]
  • 前言折腾了一段时间hadoop的部署管理,写下此系列博客记录一下。为了避免各位做部署这种重复性的劳动,我已经把部署的步骤写成脚本,各位只需要按着本文把脚本执行完,整个环境基本就部署 ... [详细]
author-avatar
忠讧_136
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有