Hbase API

JUnit 4 开始使用 Java 5 中的注解,常用的几个 注解为:
@BeforeClass:针对所有测试,只执行一次,且必须为static void
@Before:初始化方法
@Test:测试方法,在这里可以测试期望异常和超时时间
@After:释放资源
@AfterClass:针对所有测试,只执行一次,且必须为static void
@Ignore:忽略的测试方法

我们的Hbase API只是在执行每个方法,所以我们只用上面的三种方法(@Before@Test@After)就足够了。

下面是代码实现

package test.api;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.Cell;
import org.apache.hadoop.hbase.CellUtil;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.Connection;
import org.apache.hadoop.hbase.client.ConnectionFactory;
import org.apache.hadoop.hbase.client.Delete;
import org.apache.hadoop.hbase.client.Get;
import org.apache.hadoop.hbase.client.Put;
import org.apache.hadoop.hbase.client.Result;
import org.apache.hadoop.hbase.client.ResultScanner;
import org.apache.hadoop.hbase.client.Row;
import org.apache.hadoop.hbase.client.Table;
import org.apache.hadoop.hbase.util.Bytes;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

public class api {
    Connection conn;
    @Before
    public void init() {
        //配置参数信息
        Configuration conf = new Configuration();
        conf.set("hbase.zookeeper.quorum", "oracle");
        conf.set("hbase.rootdir", "hdfs://oracle:9000/hbase");
        //建立链接,这里需要注意的是我已经在电脑中的host文件添加了集群的主机名对应的IP
        //若没有添加,"oralce"的位置还是应该写成IP地址
        try {
            conn = ConnectionFactory.createConnection(conf);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    } 
    @After
    public void close() {
        try {
            conn.close();//关闭链接
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Test
    public void testPut() {
        try {
            Table table = conn.getTable(TableName.valueOf("school"));
            Put member = new Put(Bytes.toBytes("Jerry"));
            member.addColumn(Bytes.toBytes("students"), Bytes.toBytes("sex"), Bytes.toBytes("F"));
            table.put(member);
            table.close();      
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }

    @Test
    public void testPutList() {
        try {
            Table table = conn.getTable(TableName.valueOf("school"));
            List<Put> members = new ArrayList<Put>();
            Put Sage = new Put(Bytes.toBytes("Jerry"));
            Sage.addColumn(Bytes.toBytes("students"), Bytes.toBytes("age"), Bytes.toBytes("14"));
            Put Tsex = new Put(Bytes.toBytes("Mery"));
            Tsex.addColumn(Bytes.toBytes("teachers"), Bytes.toBytes("sex"), Bytes.toBytes("F"));
            Put Tage = new Put(Bytes.toBytes("Mery"));
            Tage.addColumn(Bytes.toBytes("teachers"), Bytes.toBytes("age"), Bytes.toBytes("30"));
            members.add(Sage);
            members.add(Tsex);
            members.add(Tage);
            table.put(members);
            table.close();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }

    //怎么感觉这段有点问题啊,因为timestamp的存在,好像每次都能加上
    @Test
    public void testCheckAndPut(){
        try {
            Table table = conn.getTable(TableName.valueOf("school"));
            Put member = new Put(Bytes.toBytes("Mery"));
            member.addColumn(Bytes.toBytes("teachers"), Bytes.toBytes("sex"), Bytes.toBytes("F"));
            boolean result = table.checkAndPut(Bytes.toBytes("Mery"),Bytes.toBytes("teachers"),
                    Bytes.toBytes("sex"), Bytes.toBytes("F"), member);
            table.close();
            System.out.println(result);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    @Test
    public void testDelete(){
        try {
            Table table = conn.getTable(TableName.valueOf("school"));
            Delete member = new Delete(Bytes.toBytes("Jerry"));
            //member.deleteColumn(Bytes.toBytes("students"), Bytes.toBytes("age"));
            table.delete(member);
            table.close();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    @Test
    public void testDeleteList(){
        try {
            Table table = conn.getTable(TableName.valueOf("table"));
            Delete member1 = new Delete(Bytes.toBytes("Jerry"));
            member1.addColumn(Bytes.toBytes("students"), Bytes.toBytes("sex"));
            Delete member2 = new Delete(Bytes.toBytes("Merry"));
            member2.addColumn(Bytes.toBytes("teacher"), Bytes.toBytes("age"));
            List<Delete> members = new ArrayList<Delete>();
            members.add(member1);
            members.add(member2);
            table.delete(members);
            table.close();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    @Test
    public void testCheckAndDelete(){
        try {
            Table table = conn.getTable(TableName.valueOf("school"));
            Delete member = new Delete(Bytes.toBytes("Mery"));
            member.addColumn(Bytes.toBytes("teachers"), Bytes.toBytes("age"));
            boolean result = table.checkAndDelete(Bytes.toBytes("Mery"), Bytes.toBytes("teachers"),
                    Bytes.toBytes("age"), Bytes.toBytes("30"), member);
            System.out.println(result);
            table.close();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    @Test
    public void testGet(){
        try {
            Table table = conn.getTable(TableName.valueOf("school"));
            Get get = new Get(Bytes.toBytes("Jerry"));
            get.addFamily(Bytes.toBytes("students"));
            Result result = table.get(get);
            List<Cell> cells = result.listCells();
            for (Cell cell : cells) {
                String qualifier = Bytes.toString(CellUtil.cloneQualifier(cell));
                //System.out.println(qualifier);
                /*if(qualifier.equals("age")){
                    int value =Bytes.toInt(CellUtil.cloneValue(cell));
                    System.out.println("qualifier:"+qualifier+"\t"+"value:"+value);
                }else{*/
                    String string =Bytes.toString(CellUtil.cloneValue(cell));
                    System.out.println("qualifier:"+qualifier+"\t"+"value:"+string);
                /*}*/
            }
            table.close();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
    @Test
    public void testGetList(){
        try {
            Table table = conn.getTable(TableName.valueOf("school"));
            Get get1 = new Get(Bytes.toBytes("Jerry"));
            get1.addFamily(Bytes.toBytes("students"));
            Get get2 = new Get(Bytes.toBytes("Mery"));
            get2.addFamily(Bytes.toBytes("teachers"));
            List<Get> gets = new ArrayList<Get>();
            gets.add(get1);
            gets.add(get2);
            Result[] result = table.get(gets);
            for (Result result2 : result) {
                List<Cell> cells = result2.listCells();
                for (Cell cell : cells) {
                    String qualifier = Bytes.toString(CellUtil.cloneQualifier(cell));
                    //System.out.println(qualifier);
                    /*if(qualifier.equals("age")){
                        int value =Bytes.toInt(CellUtil.cloneValue(cell));
                        System.out.println("qualifier:"+qualifier+"\t"+"value:"+value);
                    }else{*/
                        String string =Bytes.toString(CellUtil.cloneValue(cell));
                        System.out.println("qualifier:"+qualifier+"\t"+"value:"+string);
                    /*}*/
                }
            }
            table.close();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }

    @Test
    public void testBatch(){
        try {
            Table table = conn.getTable(TableName.valueOf("school"));
            List<Row> actions = new ArrayList<Row>();
            actions.add(new Put(Bytes.toBytes("Jerry")).addColumn(Bytes.toBytes("students"),
                    Bytes.toBytes("sex"), Bytes.toBytes("F")));
            actions.add(new Delete(Bytes.toBytes("Jerry")).addColumn(Bytes.toBytes("students"),
                    Bytes.toBytes("sex")));
            Object[] results = new Object[actions.size()];
            table.batch(actions, results);
            for (int i = 0; i < results.length; i++) {
                System.out.println("Result[" + i + "]: type = " + 
                        results[i].getClass().getSimpleName() + "; " + results[i]);
            }
            table.close();
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }   

    @Test
    public void testScan() {//扫描
        try {
            Table table = conn.getTable(TableName.valueOf("shool"));
            //没有指定起始行,则从表的起始位置开始获取
            ResultScanner resultScanner = table.getScanner(Bytes.toBytes("Mery"));
            Iterator<Result> iter = resultScanner.iterator();
            while(iter.hasNext()) {
                Result result = iter.next(); 
                System.out.println(new String(result.value()));;
            }
            resultScanner.close();
            table.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值