IDEA下实现Hbase增删改查

pom.xml
    <dependency>
      <groupId>org.apache.hbase</groupId>
      <artifactId>hbase-client</artifactId>
      <version>1.1.2</version>
    </dependency>
    <dependency>
      <groupId>org.apache.hbase</groupId>
      <artifactId>hbase-common</artifactId>
      <version>1.1.2</version>
    </dependency>


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.111.131");
        conf.set("hbase.zookeeper.property.clientPort","2181");
        connection = ConnectionFactory.createConnection(conf);
        admin =connection.getAdmin();

    }
    @Test
    public  void test(){
        System.out.println(connection);
        System.out.println(admin);
    }
    @Test
    public void createNameSpace(){
        NamespaceDescriptor test = NamespaceDescriptor.create("test").build();

        try {
            admin.createNamespace(test);
//            admin.deleteNamespace("test");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    @Test
    public void createTable() throws IOException {
        TableName tableName=TableName.valueOf("test: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("test:student");
        admin.disableTable(tableName);
        admin.deleteTable(tableName);
    }
    @Test
    public void insertData() throws IOException {
        table=connection.getTable(TableName.valueOf("test: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("23"));
//        rowkey1.addColumn(Bytes.toBytes("info1"),Bytes.toBytes("gender"),Bytes.toBytes("male"));
//        rowkey1.addColumn(Bytes.toBytes("info2"),Bytes.toBytes("adress"),Bytes.toBytes("nanjing"));
//        rowkey1.addColumn(Bytes.toBytes("info2"),Bytes.toBytes("name"),Bytes.toBytes("zb"));
//        rowkey1.addColumn(Bytes.toBytes("info2"),Bytes.toBytes("class"),Bytes.toBytes("kb15"));
//        table.put(rowkey1);
//

        List<Put>list=new ArrayList<>();
        Put put1 =new Put(Bytes.toBytes("rowkey2"));
        put1.addColumn(Bytes.toBytes("info1"),Bytes.toBytes("name"),Bytes.toBytes("zs"));
        put1.addColumn(Bytes.toBytes("info1"),Bytes.toBytes("age"),Bytes.toBytes("23"));
        put1.addColumn(Bytes.toBytes("info1"),Bytes.toBytes("gender"),Bytes.toBytes("male"));
        put1.addColumn(Bytes.toBytes("info2"),Bytes.toBytes("adress"),Bytes.toBytes("nanjing"));
        put1.addColumn(Bytes.toBytes("info2"),Bytes.toBytes("name"),Bytes.toBytes("zb"));
        put1.addColumn(Bytes.toBytes("info2"),Bytes.toBytes("class"),Bytes.toBytes("kb15"));

        Put put2=new Put(Bytes.toBytes("rowkey3"));
        put2.addColumn(Bytes.toBytes("info1"),Bytes.toBytes("name"),Bytes.toBytes("zs"));
        put2.addColumn(Bytes.toBytes("info1"),Bytes.toBytes("age"),Bytes.toBytes("23"));
        put2.addColumn(Bytes.toBytes("info1"),Bytes.toBytes("gender"),Bytes.toBytes("male"));
        put2.addColumn(Bytes.toBytes("info2"),Bytes.toBytes("adress"),Bytes.toBytes("nanjing"));
        put2.addColumn(Bytes.toBytes("info2"),Bytes.toBytes("name"),Bytes.toBytes("zb"));
        put2.addColumn(Bytes.toBytes("info2"),Bytes.toBytes("class"),Bytes.toBytes("kb15"));


        list.add(put1);
        list.add(put2);
        table.put(list);
    }
    @Test
    public void delete() throws IOException{
        table =connection.getTable(TableName.valueOf("test:student"));

//        删除 rowkey1

//        Delete rowkey1=new Delete(Bytes.toBytes("rowkey1"));
//        table.delete(rowkey1);

        //删除rowkey2 的info2列族

//        Delete delete=new Delete(Bytes.toBytes("rowkey2"));
//        delete.addFamily(Bytes.toBytes("info2"));
//        table.delete(delete);

        //删除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"));
        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(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.GREATER.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();

    }
}

  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,我可以帮您回答这个问题。下面是使用 Java 操作 HBase 数据库的增删改查示例代码: 1. 导入必要的包 ``` 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.TableName; import org.apache.hadoop.hbase.client.*; import org.apache.hadoop.hbase.util.Bytes; ``` 2. 创建 HBase 配置对象 ``` Configuration configuration = HBaseConfiguration.create(); configuration.set("hbase.zookeeper.quorum", "localhost"); // HBase 的 ZooKeeper 地址 configuration.set("hbase.zookeeper.property.clientPort", "2181"); // HBase 的 ZooKeeper 端口号 ``` 3. 创建 HBase 连接对象和表对象 ``` Connection connection = ConnectionFactory.createConnection(configuration); Table table = connection.getTable(TableName.valueOf("test_table")); // test_table 是表名 ``` 4. 插入数据 ``` Put put = new Put(Bytes.toBytes("row1")); // row1 是行键名 put.addColumn(Bytes.toBytes("cf"), Bytes.toBytes("col1"), Bytes.toBytes("value1")); // cf 是列族名,col1 是列名,value1 是值 table.put(put); ``` 5. 查询数据 ``` Get get = new Get(Bytes.toBytes("row1")); // row1 是行键名 Result result = table.get(get); for (Cell cell : result.listCells()) { String cf = Bytes.toString(CellUtil.cloneFamily(cell)); // 获取列族名 String col = Bytes.toString(CellUtil.cloneQualifier(cell)); // 获取列名 String value = Bytes.toString(CellUtil.cloneValue(cell)); // 获取值 System.out.println(cf + ":" + col + "=" + value); } ``` 6. 修改数据 ``` Put put = new Put(Bytes.toBytes("row1")); // row1 是行键名 put.addColumn(Bytes.toBytes("cf"), Bytes.toBytes("col1"), Bytes.toBytes("new_value")); // cf 是列族名,col1 是列名,new_value 是新的值 table.put(put); ``` 7. 删除数据 ``` Delete delete = new Delete(Bytes.toBytes("row1")); // row1 是行键名 delete.addColumn(Bytes.toBytes("cf"), Bytes.toBytes("col1")); // cf 是列族名,col1 是列名 table.delete(delete); ``` 以上就是使用 Java 操作 HBase 数据库的增删改查示例代码,希望对您有所帮助。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值