HBase 0.98增删改查java代码实现

HBase0.98代码

增删改查工具用例

package HBase;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.*;
import org.apache.hadoop.hbase.client.*;
import org.apache.hadoop.hbase.filter.Filter;
import org.apache.hadoop.hbase.util.Bytes;

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


/**
 * Created by LiuWenSheng on 2017/8/7.
 */
public class HBaseUtils {
    /**
     * 创建一个表并制定列族,同一个表只能创建一次,下次如果创建相同的表则会报出异常
     * @param admin
     * @param tableName 表名
     * @param columns 列族的字符串数组
     * @throws IOException
     */
    public void createTable(HBaseAdmin admin,String tableName,String[] columns) throws IOException {
        HTableDescriptor descriptor = new HTableDescriptor(TableName.valueOf(Bytes.toBytes(tableName)));
        for (String column :columns){
            descriptor.addFamily(new HColumnDescriptor(column));
        }
        admin.createTable(descriptor);
    }

    /**
     * 插入数据
     * @param conf 配置信息
     * @param tableName 表名
     * @param rowKey rowkey
     * @param colFamily 列族名
     * @param column 列名
     * @param value 插入的值
     * @throws IOException
     */
    public void insertData(Configuration conf,String tableName,String rowKey,String colFamily,String column,String value) throws IOException {
        HTable table = new HTable(conf, tableName);
        Put put = new Put(Bytes.toBytes(rowKey));
        put.add(Bytes.toBytes(colFamily),Bytes.toBytes(column),Bytes.toBytes(value));
        table.put(put);
    }

    /**
     * 删除表
     * @param admin
     * @param talbeName
     * @throws IOException
     */
    public void deleteTable(HBaseAdmin admin,String talbeName)throws  IOException{
        admin.disableTable(Bytes.toBytes(talbeName));
        admin.deleteTable(Bytes.toBytes(talbeName));
    }
    public void deleteRow(Configuration conf,String tableName ,String row) throws IOException {
        HTable table = new HTable(conf,tableName);
        Delete del = new Delete(Bytes.toBytes(row));
        table.delete(del);
    }
    public List<Cell[]> findByFilter(Configuration conf, String tableName, Filter filter) throws IOException, IllegalAccessException, InstantiationException {
        HTable table = new HTable(conf,tableName);
        List<Cell[]> list = new ArrayList<Cell[]>();
        Scan scaner = new Scan();
        scaner.setFilter(filter);
        ResultScanner results = table.getScanner(scaner);
        for (Result result:results){
            list.add(result.rawCells());
        }
        return list;
    }

    public  List<Cell[]>findByScan(Configuration conf,String tableName)throws IOException {
        HTable table = new HTable(conf,tableName);
        List<Cell[]> list = new ArrayList<Cell[]>();
        Scan scaner = new Scan();
        ResultScanner results = table.getScanner(scaner);
        for (Result result:results){
            list.add(result.rawCells());
        }
        return list;
    }
}
Hbase中只能说对用户的每一个操作都进行了封装,put是一个类,delete是一个类、等我们可以创建这些类,然后给这些累添加属性。最后使用这些类就可以完成增删改查操作。

测试用例

package test;

import HBase.HBaseUtils;
import org.apache.hadoop.conf.Configuration;


import org.apache.hadoop.hbase.Cell;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.client.HBaseAdmin;
import org.apache.hadoop.hbase.filter.ColumnPrefixFilter;
import org.apache.hadoop.hbase.util.Bytes;
import org.junit.Before;
import org.junit.Test;

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

/**
 * Created by Administrator on 2017/8/7.
 */
public class MyHBaseTest {
    private HBaseUtils hBaseUtils = new HBaseUtils();
    private Configuration  conf = HBaseConfiguration.create();
    private HBaseAdmin admin = null;
    @Before
    public void beforeTest() throws IOException {
        conf.set("hbase.zookeeper.quorum", "127.0.0.1");//此处应填你的IP地址,如果在windows下运行必须要填写
        conf.set("hbase.zookeeper.property.clientPort", "2181");
        admin = new HBaseAdmin(conf);
    }
    @Test
    public void test_createTable() throws IOException {
        String[] strs = new String[]{"one","two"};
        hBaseUtils.createTable(admin,"test",strs);
    }
    @Test
    public void test_insertData()throws IOException{
        hBaseUtils.insertData(conf,"test","rowkey","one","name","xingming");
    }
    @Test
    public void test_findbyFilter()throws Exception{
        ColumnPrefixFilter filter = new ColumnPrefixFilter(Bytes.toBytes("rowkey"));
        List<Cell[]> test = hBaseUtils.findByFilter(conf, "test", filter);
        if (test.isEmpty()){
            System.out.print("EMPOTY");
        }
        for (Cell[] cell:test){
            for (Cell c:cell){
                System.out.print(c.getRow());
            }
        }
    }
    @Test
    public void test_findByScan()throws Exception{
        List<Cell[]> test = hBaseUtils.findByScan(conf, "test");
        if (test.isEmpty()){
            System.out.print("EMPOTY");
        }
        for (Cell[] cell:test){
            for (Cell c:cell){
                System.out.println(new String(c.getRowArray(),c.getRowOffset(),c.getRowLength()));
                System.out.println(new String(c.getValueArray(),c.getValueOffset(),c.getValueLength()));
                System.out.println(new String(c.getQualifierArray(),c.getQualifierOffset(),c.getQualifierLength()));
            }
        }
    }
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
HBase是一个开源的、分布式的、面向列的NoSQL数据库,它基于Hadoop平台,具有高可靠性、高扩展性、高性能等特点。在Java中操作HBase需要使用HBaseJava API,下面是对HBase进行增删改查的实验总结: 1. 建立连接 在使用Java API操作HBase之前,需要先建立与HBase的连接。可以通过以下代码建立连接: ``` Configuration conf = HBaseConfiguration.create(); conf.set("hbase.zookeeper.quorum", "localhost"); // Zookeeper地址 conf.set("hbase.zookeeper.property.clientPort", "2181"); // Zookeeper端口 Connection conn = ConnectionFactory.createConnection(conf); ``` 2. 创建表 HBase是面向列的数据库,所以在创建表时需要指定表的列族。以下是创建表的示例代码: ``` Admin admin = conn.getAdmin(); HTableDescriptor tableDescriptor = new HTableDescriptor(TableName.valueOf("table_name")); HColumnDescriptor columnDescriptor = new HColumnDescriptor("column_family_name"); tableDescriptor.addFamily(columnDescriptor); admin.createTable(tableDescriptor); ``` 3. 插入数据 在插入数据时,需要指定行键、列族、列和值。以下是插入数据的示例代码: ``` Table table = conn.getTable(TableName.valueOf("table_name")); Put put = new Put(Bytes.toBytes("row_key")); put.addColumn(Bytes.toBytes("column_family_name"), Bytes.toBytes("column_name"), Bytes.toBytes("value")); table.put(put); ``` 4. 查询数据 在查询数据时,可以使用Get对象根据行键查询一条数据,也可以使用Scan对象查询多条数据。示例代码如下: - 根据行键查询一条数据: ``` Table table = conn.getTable(TableName.valueOf("table_name")); Get get = new Get(Bytes.toBytes("row_key")); Result result = table.get(get); byte[] value = result.getValue(Bytes.toBytes("column_family_name"), Bytes.toBytes("column_name")); ``` - 查询多条数据: ``` Table table = conn.getTable(TableName.valueOf("table_name")); Scan scan = new Scan(); ResultScanner scanner = table.getScanner(scan); for (Result result : scanner) { byte[] value = result.getValue(Bytes.toBytes("column_family_name"), Bytes.toBytes("column_name")); } ``` 5. 更新数据 在更新数据时,需要先获取要更新的数据,然后再使用Put对象更新数据。示例代码如下: ``` Table table = conn.getTable(TableName.valueOf("table_name")); Get get = new Get(Bytes.toBytes("row_key")); Result result = table.get(get); Put put = new Put(Bytes.toBytes("row_key")); put.addColumn(Bytes.toBytes("column_family_name"), Bytes.toBytes("column_name"), Bytes.toBytes("new_value")); table.put(put); ``` 6. 删除数据 在删除数据时,需要先获取要删除的数据,然后再使用Delete对象删除数据。示例代码如下: ``` Table table = conn.getTable(TableName.valueOf("table_name")); Get get = new Get(Bytes.toBytes("row_key")); Result result = table.get(get); Delete delete = new Delete(Bytes.toBytes("row_key")); delete.addColumn(Bytes.toBytes("column_family_name"), Bytes.toBytes("column_name")); table.delete(delete); ``` 以上就是利用JavaHBase进行增删改查的实验总结。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

天心有情

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值