集团站切换校区

验证码已发送,请查收短信

复制成功
微信号:togogoi
添加微信好友, 详细了解课程
已复制成功,如果自动跳转微信失败,请前往微信添加好友
打开微信
图标

学习文章

当前位置:首页 > >学习文章 > >

{大数据}HBase开发

发布时间: 2018-02-03 01:13:23

​maven配置:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

<modelVersion>4.0.0</modelVersion>


<groupId>net.togogo.hbasedemo</groupId>

<artifactId>hbasedemo</artifactId>

<version>0.0.1-SNAPSHOT</version>

<packaging>jar</packaging>


<name>hbasedemo</name>

<url>http://maven.apache.org</url>


<properties>

<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>

</properties>


<dependencies>

<dependency>

<groupId>junit</groupId>

<artifactId>junit</artifactId>

<version>4.11</version>

<scope>test</scope>

</dependency>


<dependency>

<groupId>org.apache.hbase</groupId>

<artifactId>hbase-client</artifactId>

<version>1.2.4</version>

</dependency>


<dependency>

<groupId>org.apache.hadoop</groupId>

<artifactId>hadoop-auth</artifactId>

<version>2.8.1</version>

</dependency>

</dependencies>

<build>

<plugins>

<plugin>

<groupId>org.apache.maven.plugins</groupId>

<artifactId>maven-compiler-plugin</artifactId>

<version>3.1</version>

<configuration>

<source>1.8</source>

<target>1.8</target>

<compilerVersion>1.8</compilerVersion>

<encoding>UTF-8</encoding>

</configuration>

</plugin>

</plugins>

</build>

</project>


HBaseConfiguration

包:org.apache.hadoop.hbase.HBaseConfiguration

作用:通过此类可以对HBase进行配置

用法实例:

Configuration config = HBaseConfiguration.create();

说明: HBaseConfiguration.create() 默认会从classpath 中查找 hbase-site.xml 中的配置信息,初始化 Configuration。


使用方法:

static Configuration config = null;

static {

    config = HBaseConfiguration.create();

    config.set("hbase.zookeeper.quorum", "slave1,slave2,slave3");

    config.set("hbase.zookeeper.property.clientPort", "2181");

}


​初始化配置​

Configuration conf = HBaseConfiguration.create();


@Before

public void init() {

conf.set("hbase.rootdir", "hdfs://192.168.195.139:9000/hbase");

// 设置Zookeeper,直接设置IP地址

conf.set("hbase.zookeeper.quorum", "192.168.195.138,192.168.195.139,192.168.195.140");

conf.set("hbase.zookeeper.property.clientPort", "2181");

}


表描述类

HTableDescriptor

包:org.apache.hadoop.hbase.HTableDescriptor

作用:HTableDescriptor 类包含了表的名字以及表的列族信息

         表的schema(设计)

用法:

HTableDescriptor htd =new HTableDescriptor(tablename);

htd.addFamily(new HColumnDescriptor(“myFamily”));


列族的描述类

HColumnDescriptor

包:org.apache.hadoop.hbase.HColumnDescriptor

作用:HColumnDescriptor 维护列族的信息


用法:

htd.addFamily(new HColumnDescriptor(“myFamily”));

创建表的操作

CreateTable(一般我们用shell创建表)

@Test

// 创建表

   public  void testCreateTable() throws Exception {

    String tablename = "company";

    String columnFamily1 = "emp";

    String columnFamily2 = "room";

      Connection connection = ConnectionFactory.createConnection(conf);

      Admin admin = connection.getAdmin();


      TableName tableNameObj = TableName.valueOf(tablename);


      if (admin.tableExists(tableNameObj)) {

           System.out.println("Table exists!");

           System.exit(0);

      } else {

           HTableDescriptor tableDesc = new HTableDescriptor(TableName.valueOf(tablename));

           tableDesc.addFamily(new HColumnDescriptor(columnFamily1));

           tableDesc.addFamily(new HColumnDescriptor(columnFamily2));

           admin.createTable(tableDesc);

           System.out.println("create table success!");

       }

       admin.close();

       connection.close();

   }


​删除表

@Test

   // 删除表

   public  void testDeleteTable() {

String tableName = "company";

       try {

           Connection connection = ConnectionFactory.createConnection(conf);

           Admin admin = connection.getAdmin();

           TableName table = TableName.valueOf(tableName);

           admin.disableTable(table);

           admin.deleteTable(table);

           System.out.println("delete table " + tableName + " ok.");

           admin.close();

           connection.close();

       } catch (IOException e) {

           e.printStackTrace();

       }

   }


​清空表

@Test

   public void truncateTable() throws IOException{


    String tableName = "company";

       try {

           Connection connection = ConnectionFactory.createConnection(conf);

           Admin admin = connection.getAdmin();

           TableName table = TableName.valueOf(tableName);

           admin.disableTable(table);

           admin.truncateTable(table, true);

           admin.close();

           connection.close();

       } catch (IOException e) {

           e.printStackTrace();

       }

   }


单条插入数据Put

包:org.apache.hadoop.hbase.client.Put

作用:插入数据

用法:

Put put = new Put(row);

p.add(family,qualifier,value);

说明:向表 tablename 添加 “family,qualifier,value”指定的值。

​示例代码:

// 插入一行记录

   public void addRecord(String tableName, String rowKey, String family, String qualifier, String value){

       try {

           Connection connection = ConnectionFactory.createConnection(conf);

           Table table = connection.getTable(TableName.valueOf(tableName));

           Put put = new Put(Bytes.toBytes(rowKey));

           put.addColumn(Bytes.toBytes(family), Bytes.toBytes(qualifier), Bytes.toBytes(value));

           put.addColumn(Bytes.toBytes(family), Bytes.toBytes(qualifier), Bytes.toBytes(value));

           table.put(put);

           table.close();

           connection.close();

           System.out.println("insert recored " + rowKey + " to table " + tableName + " ok.");

       } catch (IOException e) {

           e.printStackTrace();

       }

   }


​批量插入:

// 批量插入行记录

   public void addRecords(String tableName, String[] rowKeys, String family, String qualifier, String[] values){

       try {

        List<Put> puts = new ArrayList<Put>();

           Connection connection = ConnectionFactory.createConnection(conf);

           Table table = connection.getTable(TableName.valueOf(tableName));

           for(int i=0;i<rowKeys.length;i++){

            Put put = new Put(Bytes.toBytes(rowKeys[i]));

                put.addColumn(Bytes.toBytes(family), Bytes.toBytes(qualifier), Bytes.toBytes(values[i]));

                puts.add(put);

           }

         

           table.put(puts);

           table.close();

           connection.close();

           

       } catch (IOException e) {

           e.printStackTrace();

       }

   }


删除数据Delete

包:org.apache.hadoop.hbase.client.Delete

作用:删除给定rowkey的数据

用法:

Delete del= new Delete(Bytes.toBytes(rowKey));

table.delete(del);

代码实例

@Test

   public  void testDeleteRecord(){

try {

           Connection connection = ConnectionFactory.createConnection(conf);

           Table table = connection.getTable(TableName.valueOf("company"));

           Delete delete = new Delete(Bytes.toBytes("row2"));

           table.delete(delete);

           table.close();

           connection.close();

       } catch (IOException e) {

           e.printStackTrace();

       }

   }


单条查询Get

包:org.apache.hadoop.hbase.client.Get

作用:获取单个行的数据

@Test

   public  void testQueryTableByRowKey(){

try {

           Connection connection = ConnectionFactory.createConnection(conf);

           Table table = connection.getTable(TableName.valueOf("company"));

           Get get = new Get(Bytes.toBytes("row1"));

           Result result = table.get(get);

           byte[] row = result.getRow();

           System.out.println("row key is:" + new String(row));

           List<Cell> listCells = result.listCells();

           for (Cell cell : listCells) {

               byte[] familyArray = CellUtil.cloneFamily(cell);

               byte[] qualifierArray = CellUtil.cloneQualifier(cell);

               byte[] valueArray = CellUtil.cloneValue(cell);

               System.out.println("row value is:" + Bytes.toString(familyArray) +"\t"+Bytes.toString(qualifierArray)

                                                                           +"\t" + Bytes.toString(valueArray));

           }

           

           table.close();

           connection.close();

       } catch (IOException e) {

           e.printStackTrace();

       }

   }


批量查询ResultScanner

包:org.apache.hadoop.hbase.client.ResultScanner

作用:获取值的接口

用法:

ResultScanner scanner = table.getScanner(scan);

For(Result rowResult : scanner){

       Bytes[] str = rowResult.getValue(family,column);

}

说明:循环获取行中列值。


代码示例:

@Test

   public  void testScanTable(){

try {

           Connection connection = ConnectionFactory.createConnection(conf);

           Table table = connection.getTable(TableName.valueOf("company"));

         

           ResultScanner scanner = table.getScanner(new Scan());

           for(Result result:scanner){

            byte[] row = result.getRow();

                System.out.println("row key is:" + new String(row));

                List<Cell> listCells = result.listCells();

                for (Cell cell : listCells) {

                    byte[] familyArray = CellUtil.cloneFamily(cell);

                    byte[] qualifierArray = CellUtil.cloneQualifier(cell);

                    byte[] valueArray = CellUtil.cloneValue(cell);

                    System.out.println("row value is:" + Bytes.toString(familyArray) +"\t"+Bytes.toString(qualifierArray)

                    +"\t" + Bytes.toString(valueArray));

                }

           }    

           table.close();

           connection.close();

       } catch (IOException e) {

           e.printStackTrace();

       }

   }



hbase过滤器:

​​

FilterList

FilterList 代表一个过滤器列表,可以添加多个过滤器进行查询,多个过滤器之间的关系有:

与关系(符合所有):FilterList.Operator.MUST_PASS_ALL  

或关系(符合任一):FilterList.Operator.MUST_PASS_ONE


使用方法:

FilterList filterList = new FilterList(FilterList.Operator.MUST_PASS_ONE);  

Scan s1 = new Scan();  

filterList.addFilter(new SingleColumnValueFilter(Bytes.toBytes(“f1”),  Bytes.toBytes(“c1”),  CompareOp.EQUAL,Bytes.toBytes(“v1”) )  );  

filterList.addFilter(new SingleColumnValueFilter(Bytes.toBytes(“f1”),  Bytes.toBytes(“c2”),  CompareOp.EQUAL,Bytes.toBytes(“v2”) )  );  

// 添加下面这一行后,则只返回指定的cell,同一行中的其他cell不返回  

s1.addColumn(Bytes.toBytes(“f1”), Bytes.toBytes(“c1”));  

s1.setFilter(filterList);  //设置filter

ResultScanner ResultScannerFilterList = table.getScanner(s1);  //返回结果列表

@Test

   public  void queryTableByCondition(){

try {

           Connection connection = ConnectionFactory.createConnection(conf);

           Table table = connection.getTable(TableName.valueOf("company"));

         

           // 创建一个查询过滤器

           Filter filter = new SingleColumnValueFilter(Bytes.toBytes("emp"), Bytes.toBytes("name"),

                                                       CompareOp.EQUAL, Bytes.toBytes("stone"));

           // 创建一个数据表扫描器

           Scan scan = new Scan();

           // 将查询过滤器加入到数据表扫描器对象

           scan.setFilter(filter);


           

           ResultScanner scanner = table.getScanner(scan);

           for(Result result:scanner){

            byte[] row = result.getRow();

                System.out.println("row key is:" + new String(row));

                List<Cell> listCells = result.listCells();

                for (Cell cell : listCells) {

                    byte[] familyArray = CellUtil.cloneFamily(cell);

                    byte[] qualifierArray = CellUtil.cloneQualifier(cell);

                    byte[] valueArray = CellUtil.cloneValue(cell);

                    System.out.println("row value is:" + Bytes.toString(familyArray) +"\t"+Bytes.toString(qualifierArray)

                    +"\t" + Bytes.toString(valueArray));

                }

           }

           table.close();

           connection.close();

       } catch (IOException e) {

           e.printStackTrace();

       }

 }


过滤器的种类过滤器的种类:

列植过滤器—SingleColumnValueFilter

     过滤列植的相等、不等、范围等

列名前缀过滤器—ColumnPrefixFilter

     过滤指定前缀的列名

多个列名前缀过滤器—MultipleColumnPrefixFilter

      过滤多个指定前缀的列名

rowKey过滤器—RowFilter

     通过正则,过滤rowKey值。

列植过滤器—SingleColumnValueFilterSingleColumnValueFilter 列值判断

相等 (CompareOp.EQUAL ),

不等(CompareOp.NOT_EQUAL),

范围 (e.g., CompareOp.GREATER)…………

下面示例检查列值和字符串'values' 相等...

SingleColumnValueFilter f = new  SingleColumnValueFilter(

Bytes.toBytes("cFamily")             Bytes.toBytes("column"), CompareFilter.CompareOp.EQUAL,

       Bytes.toBytes("values"));

s1.setFilter(f);

注意:如果过滤器过滤的列在数据表中有的行中不存在,那么这个过滤器对此行无法过滤。


列名前缀过滤器—ColumnPrefixFilter过滤器—ColumnPrefixFilter

ColumnPrefixFilter 用于指定列名前缀值相等

ColumnPrefixFilter f = new ColumnPrefixFilter(Bytes.toBytes("values"));

s1.setFilter(f);

多个列值前缀过滤器—MultipleColumnPrefixFilterMultipleColumnPrefixFilter 和 ColumnPrefixFilter 行为差不多,但可以指定多个前缀

byte[][] prefixes = new byte[][] {Bytes.toBytes("value1"),Bytes.toBytes("value2")};

Filter f = new MultipleColumnPrefixFilter(prefixes);

s1.setFilter(f);

rowKey过滤器—RowFilterRowFilter 是rowkey过滤器

通常根据rowkey来指定范围时,使用scan扫描器的StartRow和StopRow方法比较好。

Filter f = new RowFilter(CompareFilter.CompareOp.EQUAL, new RegexStringComparator("^1234")); //匹配以1234开头的rowkey

s1.setFilter(f);


上一篇: {大数据}辅助系统

下一篇: {大数据}HBase访问接口

十五年老品牌
微信咨询:togogoi 咨询电话:18922156670 咨询网站客服:在线客服

相关课程推荐

在线咨询 ×

您好,请问有什么可以帮您?我们将竭诚提供最优质服务!