栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

HBase idea Java客户端操作

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

HBase idea Java客户端操作

创建Maven项目

pom.xml

    
      org.apache.hbase
      hbase-client
      1.1.2
    
    
      org.apache.hbase
      hbase-common
      1.1.2
    

在test文件夹下创建HbaseTest.java

配置连接

	Configuration conf = null;
    private Connection connection = null;
    private Table table = null;
    // 表管理员
    private Admin admin = null;
    
	@Before
    public void init() throws IOException {
        conf = HbaseConfiguration.create();
        conf.set("hbase.zookeeper.quorum","192.168.133.151");
        conf.set("hbase.zookeeper.property.clientPort","2181");
        connection = ConnectionFactory.createConnection(conf);
        admin = connection.getAdmin();
    }

关闭连接

    @After
    public void close() throws IOException {
        if (table!=null){
            table.close();
        }
        if (connection!=null){
            connection.close();
        }
    }

测试连接

@Test
    public void test1(){
        System.out.println(connection);
        System.out.println(admin);
    }

创建表空间/删除表空间

    @Test
    public void createNameSpace(){
        NamespaceDescriptor mihaoyu= NamespaceDescriptor.create("mihaoyu").build();
        try {
            admin.createNamespace(mihaoyu);
//            admin.deleteNamespace("mihaoyu");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

创建表

    @Test
    public void createTable() throws IOException {
        TableName tableName = TableName.valueOf("mihaoyu:student");

        HTableDescriptor desc = new HTableDescriptor(tableName);

        HColumnDescriptor family1 = new HColumnDescriptor("info1");
        HColumnDescriptor family2 = new HColumnDescriptor("info2");

        desc.addFamily(family1);
        desc.addFamily(family2);

        admin.createTable(desc);
    }

删除表

    @Test
    public void deleteTable() throws IOException {
        TableName tableName = TableName.valueOf("mihaoyu:student");
        admin.disableTable(tableName);
        admin.deleteTable(tableName);
    }

插入数据

    @Test
    public void insertData() throws IOException {
        table = connection.getTable(TableName.valueOf("mihaoyu:student"));

//        Put rowkey1 = new Put(Bytes.toBytes("rowkey1"));
        rowkey1.add(Bytes.toBytes("info1"),Bytes.toBytes("name"),Bytes.toBytes("zs")); 过时
//
//        rowkey1.addColumn(Bytes.toBytes("info1"),Bytes.toBytes("name"),Bytes.toBytes("zs"));
//        rowkey1.addColumn(Bytes.toBytes("info1"),Bytes.toBytes("age"),Bytes.toBytes("zs"));
//        rowkey1.addColumn(Bytes.toBytes("info1"),Bytes.toBytes("gender"),Bytes.toBytes("zs"));
//        rowkey1.addColumn(Bytes.toBytes("info2"),Bytes.toBytes("name"),Bytes.toBytes("andemen"));
//
//        table.put(rowkey1);
        List list = new ArrayList<>();

        Put put1 = new Put(Bytes.toBytes("rowkey2"));
        put1.addColumn(Bytes.toBytes("info1"),Bytes.toBytes("name"),Bytes.toBytes("zs1"));
        put1.addColumn(Bytes.toBytes("info1"),Bytes.toBytes("age"),Bytes.toBytes("zs1"));
        put1.addColumn(Bytes.toBytes("info1"),Bytes.toBytes("gender"),Bytes.toBytes("zs1"));
        put1.addColumn(Bytes.toBytes("info2"),Bytes.toBytes("name"),Bytes.toBytes("andemen1"));
        Put put2 = new Put(Bytes.toBytes("rowkey3"));
        put2.addColumn(Bytes.toBytes("info1"),Bytes.toBytes("name"),Bytes.toBytes("zs2"));
        put2.addColumn(Bytes.toBytes("info1"),Bytes.toBytes("age"),Bytes.toBytes("zs2"));
        put2.addColumn(Bytes.toBytes("info1"),Bytes.toBytes("gender"),Bytes.toBytes("zs2"));
        put2.addColumn(Bytes.toBytes("info2"),Bytes.toBytes("name"),Bytes.toBytes("andemen2"));

        list.add(put1);
        list.add(put2);
        table.put(list);
    }

删除表数据

    @Test
    public void delete() throws IOException {
        table = connection.getTable(TableName.valueOf("mihaoyu:student"));

        // 删除一行 rowkey1全部删除
        Delete rowkey1 = new Delete(Bytes.toBytes("rowkey1"));
        table.delete(rowkey1);

        // 删除 rowkey2 的info2列族
        Delete rowkey2 = new Delete(Bytes.toBytes("rowkey2"));
        rowkey2.addFamily(Bytes.toBytes("info2"));
        table.delete(rowkey2);

        // 删除 rowkey3 列族为 info1 下的 gender列
        Delete rowkey3 = new Delete(Bytes.toBytes("rowkey3"));
        rowkey3.addColumn(Bytes.toBytes("info1"),Bytes.toBytes("gender"));
        table.delete(rowkey3);
    }

查询数据

    @Test
    public void getData() throws IOException {
        table = connection.getTable(TableName.valueOf("kb15:student"));

        Get get = new Get(Bytes.toBytes("rowkey1"));
//        get.addFamily(Bytes.toBytes("baseInfo"));

        Result result = table.get(get);

        byte[] name = result.getValue(Bytes.toBytes("baseInfo"), Bytes.toBytes("name"));
        byte[] age = result.getValue(Bytes.toBytes("baseInfo"), Bytes.toBytes("age"));
        System.out.println(Bytes.toString(name));
        System.out.println(Integer.valueOf(Bytes.toString(age)));
    }

全表扫描

    @Test
    public void scanData() throws IOException {
        table = connection.getTable(TableName.valueOf("kb15:student"));
        Scan scan = new Scan();
        scan.addColumn(Bytes.toBytes("baseInfo"),Bytes.toBytes("age"));
        scan.addColumn(Bytes.toBytes("baseInfo"),Bytes.toBytes("name"));
        scan.setStartRow(Bytes.toBytes("rowkey1"));
        scan.setStopRow(Bytes.toBytes("rowkey3"));
        ResultScanner scanner = table.getScanner(scan);
        for (Result result :
                scanner) {
            byte[] name = result.getValue(Bytes.toBytes("baseInfo"), Bytes.toBytes("name"));
            byte[] age = result.getValue(Bytes.toBytes("baseInfo"), Bytes.toBytes("age"));
            byte[] row = result.getRow();
            System.out.println(Bytes.toString(name));
            System.out.println(Bytes.toString(age));
            System.out.println(Bytes.toString(row));
        }
    }

过滤查询

    @Test
    public void getFilterRowKey() throws IOException {
        table = connection.getTable(TableName.valueOf("kb15:student"));
        Scan scan = new Scan();

        FamilyFilter familyFilter = new FamilyFilter(CompareFilter.CompareOp.EQUAL,new BinaryComparator(Bytes.toBytes("baseInfo")));

        scan.setFilter(familyFilter);

        ResultScanner scanner = table.getScanner(scan);
        for (Result result :
                scanner) {
            byte[] name = result.getValue(Bytes.toBytes("baseInfo"), Bytes.toBytes("name"));
            byte[] age = result.getValue(Bytes.toBytes("baseInfo"), Bytes.toBytes("age"));
            byte[] row = result.getRow();
            System.out.println(Bytes.toString(name));
            System.out.println(Bytes.toString(age));
            System.out.println(Bytes.toString(row));
        }
    }

全部代码

package nj.zb.kb15;

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

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

public class HbaseTest {

    Configuration conf = null;
    private Connection connection = null;
    private Table table = null;

    // 表管理员
    private Admin admin = null;

    @Before
    public void init() throws IOException {
        conf = HbaseConfiguration.create();
        conf.set("hbase.zookeeper.quorum","192.168.133.151");
        conf.set("hbase.zookeeper.property.clientPort","2181");
        connection = ConnectionFactory.createConnection(conf);
        admin = connection.getAdmin();
    }

    @Test
    public void test1(){
        System.out.println(connection);
        System.out.println(admin);
    }

    @Test
    public void createNameSpace(){
        NamespaceDescriptor mihaoyu = NamespaceDescriptor.create("mihaoyu").build();
        try {
            admin.createNamespace(mihaoyu);
//            admin.deleteNamespace("mihaoyu");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Test
    public void createTable() throws IOException {
        TableName tableName = TableName.valueOf("mihaoyu:student");

        HTableDescriptor desc = new HTableDescriptor(tableName);

        HColumnDescriptor family1 = new HColumnDescriptor("info1");
        HColumnDescriptor family2 = new HColumnDescriptor("info2");

        desc.addFamily(family1);
        desc.addFamily(family2);

        admin.createTable(desc);
    }

    @Test
    public void deleteTable() throws IOException {
        TableName tableName = TableName.valueOf("mihaoyu:student");
        admin.disableTable(tableName);
        admin.deleteTable(tableName);
    }

    @Test
    public void insertData() throws IOException {
        table = connection.getTable(TableName.valueOf("mihaoyu:student"));

//        Put rowkey1 = new Put(Bytes.toBytes("rowkey1"));
        rowkey1.add(Bytes.toBytes("info1"),Bytes.toBytes("name"),Bytes.toBytes("zs")); 过时
//
//        rowkey1.addColumn(Bytes.toBytes("info1"),Bytes.toBytes("name"),Bytes.toBytes("zs"));
//        rowkey1.addColumn(Bytes.toBytes("info1"),Bytes.toBytes("age"),Bytes.toBytes("zs"));
//        rowkey1.addColumn(Bytes.toBytes("info1"),Bytes.toBytes("gender"),Bytes.toBytes("zs"));
//        rowkey1.addColumn(Bytes.toBytes("info2"),Bytes.toBytes("name"),Bytes.toBytes("andemen"));
//
//        table.put(rowkey1);
        List list = new ArrayList<>();

        Put put1 = new Put(Bytes.toBytes("rowkey2"));
        put1.addColumn(Bytes.toBytes("info1"),Bytes.toBytes("name"),Bytes.toBytes("zs1"));
        put1.addColumn(Bytes.toBytes("info1"),Bytes.toBytes("age"),Bytes.toBytes("zs1"));
        put1.addColumn(Bytes.toBytes("info1"),Bytes.toBytes("gender"),Bytes.toBytes("zs1"));
        put1.addColumn(Bytes.toBytes("info2"),Bytes.toBytes("name"),Bytes.toBytes("andemen1"));
        Put put2 = new Put(Bytes.toBytes("rowkey3"));
        put2.addColumn(Bytes.toBytes("info1"),Bytes.toBytes("name"),Bytes.toBytes("zs2"));
        put2.addColumn(Bytes.toBytes("info1"),Bytes.toBytes("age"),Bytes.toBytes("zs2"));
        put2.addColumn(Bytes.toBytes("info1"),Bytes.toBytes("gender"),Bytes.toBytes("zs2"));
        put2.addColumn(Bytes.toBytes("info2"),Bytes.toBytes("name"),Bytes.toBytes("andemen2"));

        list.add(put1);
        list.add(put2);
        table.put(list);
    }

    @Test
    public void delete() throws IOException {
        table = connection.getTable(TableName.valueOf("mihaoyu:student"));

        // 删除一行 rowkey1全部删除
//        Delete rowkey1 = new Delete(Bytes.toBytes("rowkey1"));
//        table.delete(rowkey1);

        // 删除 rowkey2 的info2列族
//        Delete rowkey2 = new Delete(Bytes.toBytes("rowkey2"));
//        rowkey2.addFamily(Bytes.toBytes("info2"));
//        table.delete(rowkey2);

        // 删除 rowkey3 列族为 info1 下的 gender 列d
        Delete rowkey3 = new Delete(Bytes.toBytes("rowkey3"));
        rowkey3.addColumn(Bytes.toBytes("info1"),Bytes.toBytes("gender"));
        table.delete(rowkey3);
    }

    @Test
    public void getData() throws IOException {
        table = connection.getTable(TableName.valueOf("kb15:student"));

        Get get = new Get(Bytes.toBytes("rowkey1"));
//        get.addFamily(Bytes.toBytes("baseInfo"));

        Result result = table.get(get);

        byte[] name = result.getValue(Bytes.toBytes("baseInfo"), Bytes.toBytes("name"));
        byte[] age = result.getValue(Bytes.toBytes("baseInfo"), Bytes.toBytes("age"));
        System.out.println(Bytes.toString(name));
        System.out.println(Integer.valueOf(Bytes.toString(age)));

    }

    @Test
    public void scanData() throws IOException {
        table = connection.getTable(TableName.valueOf("kb15:student"));
        Scan scan = new Scan();
        scan.addColumn(Bytes.toBytes("baseInfo"),Bytes.toBytes("age"));
        scan.addColumn(Bytes.toBytes("baseInfo"),Bytes.toBytes("name"));
        scan.setStartRow(Bytes.toBytes("rowkey1"));
        scan.setStopRow(Bytes.toBytes("rowkey3"));
        ResultScanner scanner = table.getScanner(scan);
        for (Result result :
                scanner) {
            byte[] name = result.getValue(Bytes.toBytes("baseInfo"), Bytes.toBytes("name"));
            byte[] age = result.getValue(Bytes.toBytes("baseInfo"), Bytes.toBytes("age"));
            byte[] row = result.getRow();
            System.out.println(Bytes.toString(name));
            System.out.println(Bytes.toString(age));
            System.out.println(Bytes.toString(row));
        }
    }

    @Test
    public void getFilterRowKey() throws IOException {
        table = connection.getTable(TableName.valueOf("kb15:student"));
        Scan scan = new Scan();

        FamilyFilter familyFilter = new FamilyFilter(CompareFilter.CompareOp.EQUAL,new BinaryComparator(Bytes.toBytes("baseInfo")));

        scan.setFilter(familyFilter);

        ResultScanner scanner = table.getScanner(scan);
        for (Result result :
                scanner) {
            byte[] name = result.getValue(Bytes.toBytes("baseInfo"), Bytes.toBytes("name"));
            byte[] age = result.getValue(Bytes.toBytes("baseInfo"), Bytes.toBytes("age"));
            byte[] row = result.getRow();
            System.out.println(Bytes.toString(name));
            System.out.println(Bytes.toString(age));
            System.out.println(Bytes.toString(row));
        }
    }

    @After
    public void close() throws IOException {
        if (table!=null){
            table.close();
        }
        if (connection!=null){
            connection.close();
        }
    }

}
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/581772.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号