hbase java API操作

pom.xml

        <dependency>
            <groupId>org.apache.hadoop</groupId>
            <artifactId>hadoop-client</artifactId>
            <version>2.6.0</version>
        </dependency>
        <dependency>
            <groupId>org.apache.hadoop</groupId>
            <artifactId>hadoop-mapreduce-client-app</artifactId>
            <version>2.6.0</version>
        </dependency>
        <dependency>
            <groupId>org.apache.zookeeper</groupId>
            <artifactId>zookeeper</artifactId>
            <version>3.4.6</version>
        </dependency>
        <dependency>
            <groupId>org.apache.hbase</groupId>
            <artifactId>hbase-client</artifactId>
            <version>1.2.0</version>
        </dependency>
        <dependency>
            <groupId>org.apache.hbase</groupId>
            <artifactId>hbase-server</artifactId>
            <version>1.2.0</version>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.11</version>
            <scope>test</scope>
        </dependency>

 HBaseApp.java (在测试目录里test)

package com.lhjava.hbase;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.*;
import org.apache.hadoop.hbase.client.*;
import org.apache.hadoop.hbase.filter.*;
import org.apache.hadoop.hbase.util.Bytes;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import java.util.ArrayList;
import java.util.List;

public class HBaseApp {
    Configuration configuration = null;
    Connection connection = null;
    Admin admin = null;

    @Before
    public void setUp()throws Exception{
        configuration = new Configuration();
        configuration.set("hbase.rootdir", "hdfs://192.168.35.128:8082/hbase");
        configuration.set("hbase.zookeeper.quorum", "192.168.35.128:2181");
        connection = ConnectionFactory.createConnection(configuration);
        admin = connection.getAdmin();
    }
    //查询表
    @Test
    public void queryAllTables() throws Exception{
        HTableDescriptor[] hTableDescriptors = admin.listTables();
        for (HTableDescriptor hTableDescriptor : hTableDescriptors) {
            String name = hTableDescriptor.getNameAsString();
            System.out.println("表名:" + name);

            HColumnDescriptor[] hColumnDescriptors = hTableDescriptor.getColumnFamilies();
            for(HColumnDescriptor hColumnDescriptor : hColumnDescriptors){
                String columnFamilyName = hColumnDescriptor.getNameAsString();
                System.out.println("列族名:" + columnFamilyName);
            }
        }
    }

    //创建表
    @Test
    public void createTable()throws Exception{
        String tableName = "table_one";
        String columnFamilyName0 = "column_family_one";
        String columnFamilyName1 = "column_family_two";

        //测试某表是否存在
        if (admin.tableExists(TableName.valueOf(tableName))){
            System.out.println(tableName + "已存在");
        } else{
            //创建HTableDescriptor
            HTableDescriptor hTableDescriptor = new HTableDescriptor(TableName.valueOf(tableName));
            //添加列族
            HColumnDescriptor hColumnDescriptor0 = new HColumnDescriptor(columnFamilyName0);
            hTableDescriptor.addFamily(hColumnDescriptor0);
            HColumnDescriptor hColumnDescriptor1 = new HColumnDescriptor(columnFamilyName1);
            hTableDescriptor.addFamily(hColumnDescriptor1);
            admin.createTable(hTableDescriptor);
            System.out.println(tableName + "创建成功");
        }
    }

    //插入数据(修改数据和添加数据用的是一样的操作)
    @Test
    public void put()throws Exception{
        //获取table对象
        Table table = connection.getTable(TableName.valueOf("member"));
        //创建put对象 "lisi"是rowkey
        Put put = new Put(Bytes.toBytes("lisi"));
        //第一个参数:列族名, 第二个参数:列名, 第三个参数:值
        put.addColumn(Bytes.toBytes("info"), Bytes.toBytes("gender"), Bytes.toBytes("female"));
        put.addColumn(Bytes.toBytes("info"), Bytes.toBytes("age"), Bytes.toBytes("20"));

        table.put(put);
    }

    //批量插入数据
    @Test
    public void putBatch()throws Exception{
        //获取table对象
        Table table = connection.getTable(TableName.valueOf("member"));
        //创建put对象 "lisi"是rowkey
        Put put = new Put(Bytes.toBytes("tom"));
        //第一个参数:列族名, 第二个参数:列名, 第三个参数:值
        put.addColumn(Bytes.toBytes("info"), Bytes.toBytes("gender"), Bytes.toBytes("female"));
        put.addColumn(Bytes.toBytes("info"), Bytes.toBytes("age"), Bytes.toBytes("20"));

        //获取table对象
        Table table1 = connection.getTable(TableName.valueOf("member"));
        //创建put对象 "lisi"是rowkey
        Put put1 = new Put(Bytes.toBytes("jack"));
        //第一个参数:列族名, 第二个参数:列名, 第三个参数:值
        put1.addColumn(Bytes.toBytes("info"), Bytes.toBytes("gender"), Bytes.toBytes("female"));
        put1.addColumn(Bytes.toBytes("info"), Bytes.toBytes("age"), Bytes.toBytes("20"));

        List<Put> puts = new ArrayList<Put>();
        puts.add(put);
        puts.add(put1);

        table.put(puts);
    }

    //根据rowkey获取记录
    @Test
    public void get01() throws Exception{
        //获取Table对象
        Table table = connection.getTable(TableName.valueOf("member"));
        //根据rowkey获取Get对象
        String rowkey = "zhangsan";
        Get get = new Get(Bytes.toBytes(rowkey));
        Result result = table.get(get);
        Cell[] cells = result.rawCells();
        for(Cell cell : cells){
            String rowkey2  = Bytes.toString(CellUtil.cloneRow(cell));
            String columnFamilyName  = Bytes.toString(CellUtil.cloneFamily(cell));
            String qualifierName  = Bytes.toString(CellUtil.cloneQualifier(cell));
            String value  = Bytes.toString(CellUtil.cloneValue(cell));
            System.out.println(rowkey2 + "--" + columnFamilyName + "--"
                + qualifierName + "--" + value);

        }
    }

    //获取指定列族的所有列的值
    @Test
    public void get02() throws Exception{
        //获取Table对象
        Table table = connection.getTable(TableName.valueOf("member"));
        //根据rowkey获取Get对象
        String rowkey = "zhangsan";
        Get get = new Get(Bytes.toBytes(rowkey));
        get.addFamily(Bytes.toBytes("info"));
        Result result = table.get(get);
        Cell[] cells = result.rawCells();
        for(Cell cell : cells){
            String rowkey2  = Bytes.toString(CellUtil.cloneRow(cell));
            String columnFamilyName  = Bytes.toString(CellUtil.cloneFamily(cell));
            String qualifierName  = Bytes.toString(CellUtil.cloneQualifier(cell));
            String value  = Bytes.toString(CellUtil.cloneValue(cell));
            System.out.println(rowkey2 + "--" + columnFamilyName + "--"
                    + qualifierName + "--" + value);
        }

    }

    //获取指定列的值
    @Test
    public void get03() throws Exception{
        //获取Table对象
        Table table = connection.getTable(TableName.valueOf("member"));
        //根据rowkey获取Get对象
        String rowkey = "zhangsan";
        Get get = new Get(Bytes.toBytes(rowkey));
        get.addColumn(Bytes.toBytes("info"), Bytes.toBytes("age"));
        Result result = table.get(get);
        byte[] value = result.getValue(Bytes.toBytes("info"), Bytes.toBytes("age"));
        System.out.println(Bytes.toString(value));
    }

    //扫描全表
    @Test
    public void scan01()throws Exception{
        Table table = connection.getTable(TableName.valueOf("member"));
        Scan scan = new Scan();
        ResultScanner resultScanner = table.getScanner(scan);
        for (Result result : resultScanner){
            Cell[] cells = result.rawCells();
            for(Cell cell : cells){
                String rowkey2  = Bytes.toString(CellUtil.cloneRow(cell));
                String columnFamilyName  = Bytes.toString(CellUtil.cloneFamily(cell));
                String qualifierName  = Bytes.toString(CellUtil.cloneQualifier(cell));
                String value  = Bytes.toString(CellUtil.cloneValue(cell));
                System.out.println(rowkey2 + "--" + columnFamilyName + "--"
                        + qualifierName + "--" + value);
            }
            System.out.println("===================================");
        }
    }

    /**
     * 扫描全表,根据rowkey进行筛选[startRow, stopRow)
     * @throws Exception
     */
    @Test
    public void scan02()throws Exception{
        Table table = connection.getTable(TableName.valueOf("member"));
        //指定startRow和stopRow
        Scan scan = new Scan(Bytes.toBytes("lisi"), Bytes.toBytes("zhangsan"));
        ResultScanner resultScanner = table.getScanner(scan);
        for (Result result : resultScanner){
            Cell[] cells = result.rawCells();
            for(Cell cell : cells){
                String rowkey2  = Bytes.toString(CellUtil.cloneRow(cell));
                String columnFamilyName  = Bytes.toString(CellUtil.cloneFamily(cell));
                String qualifierName  = Bytes.toString(CellUtil.cloneQualifier(cell));
                String value  = Bytes.toString(CellUtil.cloneValue(cell));
                System.out.println(rowkey2 + "--" + columnFamilyName + "--"
                        + qualifierName + "--" + value);
            }
            System.out.println("===================================");
        }
    }

    /**
     * 全表扫描 + filter
     */
    @Test
    public void scan03()throws Exception{
        Table table = connection.getTable(TableName.valueOf("member"));
        //指定startRow和stopRow
        Scan scan = new Scan();
        //设置Scan的Filter,rowkey以"i"结尾的记录
       /* RegexStringComparator regexStringComparator = new RegexStringComparator("^*si$");
        Filter filter = new RowFilter(CompareFilter.CompareOp.EQUAL, regexStringComparator);
        scan.setFilter(filter);*/

        //设置Scan的Filter,rowkey以"li"开头的记录
       /* Filter filter = new PrefixFilter(Bytes.toBytes("li"));
        scan.setFilter(filter);*/

        //设置Scan的Filter,使用多个过滤器

        RegexStringComparator regexStringComparator = new RegexStringComparator("^*si$");
        Filter filter1 = new RowFilter(CompareFilter.CompareOp.EQUAL, regexStringComparator);
        Filter filter2 = new PrefixFilter(Bytes.toBytes("t"));

        FilterList filterList = new FilterList(FilterList.Operator.MUST_PASS_ONE);
        filterList.addFilter(filter1);
        filterList.addFilter(filter2);

        scan.setFilter(filterList);

        ResultScanner resultScanner = table.getScanner(scan);
        for (Result result : resultScanner){
            Cell[] cells = result.rawCells();
            for(Cell cell : cells){
                String rowkey2  = Bytes.toString(CellUtil.cloneRow(cell));
                String columnFamilyName  = Bytes.toString(CellUtil.cloneFamily(cell));
                String qualifierName  = Bytes.toString(CellUtil.cloneQualifier(cell));
                String value  = Bytes.toString(CellUtil.cloneValue(cell));
                System.out.println(rowkey2 + "--" + columnFamilyName + "--"
                        + qualifierName + "--" + value);
            }
            System.out.println("===================================");
        }
    }

    /**
     * 扫描全表 + 指定列族
     * 扫描全表 + 指定列族 + 指定列
     * @throws Exception
     */
    @Test
    public void scan04()throws Exception{
        Table table = connection.getTable(TableName.valueOf("member"));

        /*//扫描全表 + 指定列族
        ResultScanner resultScanner = table.getScanner(Bytes.toBytes("info"));*/

        //扫描全表 + 指定列族 + 指定列
        ResultScanner resultScanner = table.getScanner(Bytes.toBytes("info"), Bytes.toBytes("age"));

        for (Result result : resultScanner){
            Cell[] cells = result.rawCells();
            for(Cell cell : cells){
                String rowkey2  = Bytes.toString(CellUtil.cloneRow(cell));
                String columnFamilyName  = Bytes.toString(CellUtil.cloneFamily(cell));
                String qualifierName  = Bytes.toString(CellUtil.cloneQualifier(cell));
                String value  = Bytes.toString(CellUtil.cloneValue(cell));
                System.out.println(rowkey2 + "--" + columnFamilyName + "--"
                        + qualifierName + "--" + value);
            }
            System.out.println("===================================");
        }
    }

    /**
     * 删除操作
     * @throws Exception
     */
    @Test
    public void delete01() throws Exception{
        Table table = connection.getTable(TableName.valueOf("member"));

        /*//根据rowkey进行删除
        String rowkey = "jack";
        Delete delete = new Delete(Bytes.toBytes(rowkey));*/

        //删除列族
        String rowkey = "lisi";
        Delete delete = new Delete(Bytes.toBytes(rowkey));
        delete.addFamily(Bytes.toBytes("info"));

       /* //删除指定列,如果没有指定timestamp,那么默认删除最后一次设置的值
        //如果要想删除单元格中所有版本的值,那么要使用 delete.addColumns()
        String rowkey = "lisi";
        Delete delete = new Delete(Bytes.toBytes(rowkey));
        delete.addColumn(Bytes.toBytes("info"), Bytes.toBytes("age"));*/

        table.delete(delete);
    }

    /**
     * 删除整张表
     */
    @Test
    public void dropTable() throws Exception{
        TableName tableName = TableName.valueOf("table_one");
        admin.disableTable(tableName);
        admin.deleteTable(tableName);
    }

    @After
    public void tearDown() throws Exception{
        admin.close();
        connection.close();
    }

}

配置hosts

https://blog.csdn.net/fengqing5578/article/details/79990640

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值