HBase第二章 基本API

1.pom.xml

<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>cn.itcast.hbase</groupId>
    <artifactId>hbase</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <dependencies>
        <!-- https://mvnrepository.com/artifact/org.apache.hadoop/hadoop-client -->
        <dependency>
            <groupId>org.apache.hadoop</groupId>
            <artifactId>hadoop-client</artifactId>
            <version>2.6.4</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.apache.hadoop/hadoop-hdfs -->
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.apache.hbase/hbase-client -->
        <dependency>
            <groupId>org.apache.hbase</groupId>
            <artifactId>hbase-client</artifactId>
            <version>0.99.2</version>
        </dependency>
    </dependencies>
</project>

2、API操作

package cn.itcast.bigdata.hbase;

import java.io.IOException;
import java.util.NavigableMap;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.Cell;
import org.apache.hadoop.hbase.CellUtil;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.HColumnDescriptor;
import org.apache.hadoop.hbase.HTableDescriptor;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.Admin;
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.Scan;
import org.apache.hadoop.hbase.client.Table;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

public class TestApi {
    private Configuration config;
    private Connection connection;

    @Before
    public void before() throws IOException {
        config = HBaseConfiguration.create();
        // 配置zookeeper
        config.set("hbase.zookeeper.quorum", "hadoop2,hadoop3,hadoop4");
        config.set("hbase.zookeeper.property.clientPort", "2181");
        connection = ConnectionFactory.createConnection(config);

    }

    /**
     * @throws IOException
     *             创建表
     */
    @Test
    public void testCreateTable() throws IOException {
        Admin admin = connection.getAdmin();
        // 表的描述类
        HTableDescriptor desc = new HTableDescriptor(TableName.valueOf("user"));
        // 列族的描述类
        HColumnDescriptor family1 = new HColumnDescriptor("f1");
        // 将列族信息和表的描述类进行关联
        desc.addFamily(family1);
        HColumnDescriptor family2 = new HColumnDescriptor("f2");
        desc.addFamily(family2);
        // 根据描述信息创建表
        admin.createTable(desc);
        admin.close();
    }

    /**
     * @throws IOException
     *             删除表
     */
    @Test
    public void testDeleteTable() throws IOException {
        Admin admin = connection.getAdmin();
        TableName tableName = TableName.valueOf("user");
        admin.disableTable(tableName);
        admin.deleteTable(tableName);
        admin.close();
    }

    /**
     * @throws IOException
     *             添加数据
     */
    @Test
    public void testPut() throws IOException {
        Table table = connection.getTable(TableName.valueOf("user"));
        Put put = new Put("00001".getBytes());
        // put 'user','00001','info1:name','zhangsan'
        put.add("f1".getBytes(), "name".getBytes(), "zhangsan".getBytes());
        table.put(put);
        table.close();

    }

    /**
     * 删除数据
     * 
     * @throws IOException
     */
    @Test
    public void testDelete() throws IOException {
        Table table = connection.getTable(TableName.valueOf("user"));
        Delete delete = new Delete("00001".getBytes());
        table.delete(delete);
        table.close();
    }

    /**
     * @throws IOException
     *             单条查询
     */
    @Test
    public void testGet() throws IOException {
        Table table = connection.getTable(TableName.valueOf("user"));
        Get get = new Get("00001".getBytes());
        Result row = table.get(get);
        // 获取family f1下所有的名字 f1:name f1:age
        NavigableMap<byte[], byte[]> familyMap = row.getFamilyMap("f1".getBytes());
        // 遍历
        for (byte[] nav : familyMap.navigableKeySet()) {
            // 打印名字:name
            System.out.println(new String(nav));
            // 获取name对应的值:zhangsan
            byte[] value = row.getValue("f1".getBytes(), nav);
            System.out.println(new String(value));
        }
        table.close();
    }

    /**
     * @throws IOException
     *             批量查询
     */
    @Test
    public void testScanner() throws IOException {
        Table table = connection.getTable(TableName.valueOf("user"));
        Scan scan = new Scan();
        // 不设置起始和结束的话 为全表查询
        scan.setStartRow("00001".getBytes());// 设置起始row
        scan.setStopRow("000010".getBytes());// 设置结束 row
        ResultScanner scanner = table.getScanner(scan);
        for (Result result : scanner) {
            Cell[] rawCells = result.rawCells();
            for (Cell cell : rawCells) {
                System.out.println("value = " + new String(CellUtil.cloneValue(cell)));
                System.out.println("family = " + new String(CellUtil.cloneFamily(cell)));
                System.out.println("qualifier = " + new String(CellUtil.cloneQualifier(cell)));

            }
        }
        table.close();

    }

    /**
     * @throws IOException
     *             关闭连接
     */
    @After
    public void after() throws IOException {
        connection.close();
    }
}

 

posted @ 2018-01-03 17:12  IT-執念  阅读(331)  评论(0编辑  收藏  举报