hbase java操作api - Append,Incr自增,batchCallback,batch(put,delete,append可同时使用)的使用

代码示例

有很多的代码是重复的,只是为形成记忆,见谅,
另外需要将 hbase-site.xml,hdfs-site.xml,core-site.xml三个文件放到Resources上目录中

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.*;
import org.apache.hadoop.hbase.client.*;
import org.apache.hadoop.hbase.client.coprocessor.Batch;
import org.apache.hadoop.hbase.filter.*;
import org.apache.hadoop.hbase.io.compress.Compression;
import org.apache.hadoop.hbase.util.Bytes;
import java.util.ArrayList;
import java.util.List;

public class HbaseApi_test_1 {
    private static Connection hbaseConn;
    private static Configuration hbaseConf;
    //HBaseAdmin 提供了一个接口来管理 HBase 数据库的表信息
    private static Admin hbaseAdmin;

    /*** 静态构造,在调用静态方法前运行,  初始化连接对象  * */
    static {
        hbaseConf = HBaseConfiguration.create();
        try {
            hbaseConn = ConnectionFactory.createConnection(hbaseConf);
            System.out.println("连接上了?" + !hbaseConn.isClosed());
            hbaseAdmin = hbaseConn.getAdmin();
        } catch (java.lang.Exception e) {
            e.printStackTrace();
        }
    }

//****************************为某列的值 追加记录 ,在原有的value后面追加新的value,  "a" + "bc"  -->  "abc"
    public static void row_appendData() throws java.lang.Exception {
        TableName tableName = TableName.valueOf("ns1:mytest_4");
        //取得一个要操作的表
        Table table = hbaseConn.getTable(tableName);
        //设置要追加的行的rowkey
        Append append = new Append(Bytes.toBytes("zhangshan"));
        //为某列的值,追加新的值
        append.add(Bytes.toBytes("course"), Bytes.toBytes("yuwen"), Bytes.toBytes(" 零蛋"));
        append.add(Bytes.toBytes("grade"), Bytes.toBytes(""), Bytes.toBytes(" dao ji dian le"));
        //开始追加
        table.append(append);

        //关闭资源
        table.close();
    }

//自增incr
    //incr '表名', '行键', '列族:列名', 步长值
    public static void incr() throws java.lang.Exception
    {
        TableName tableName = TableName.valueOf("ns1:mytest_4");
        //取得一个要操作的表
        Table table = hbaseConn.getTable(tableName);

        //只为一个列incr
        table.incrementColumnValue(Bytes.toBytes("xiaoming"),Bytes.toBytes("grade"), Bytes.toBytes(""),5);

        //为多个列incr
        Increment increment = new Increment(Bytes.toBytes("zhangshan"));
        increment.addColumn(Bytes.toBytes("course"), Bytes.toBytes("yuwen"),1);
        increment.addColumn(Bytes.toBytes("course"), Bytes.toBytes("shuxue"),2);
        increment.addColumn(Bytes.toBytes("course"), Bytes.toBytes("english"),3);
        increment.addColumn(Bytes.toBytes("grade"), Bytes.toBytes(""),4);
        table.increment(increment);

        //关闭资源
        table.close();
    }

//****************************批处理****************************
    public static void pichuli() throws java.lang.Exception {
        TableName tableName = TableName.valueOf("ns1:mytest_4");
        //取得一个要操作的表
        Table table = hbaseConn.getTable(tableName);

        ArrayList<Row> rows = new ArrayList<>();

        //设置要操作的行,rowkey
        Put put_1 = new Put(Bytes.toBytes("xiaoming"));
        //参数1:列族 ,参数2:列,参数3:值,(这里的时间戳会自动生成)
        put_1.addColumn(Bytes.toBytes("course"), Bytes.toBytes("yuwen"), Bytes.toBytes("50"));
        put_1.addColumn(Bytes.toBytes("grade"), Bytes.toBytes(""), Bytes.toBytes("A"));

        Get get_1 = new Get(Bytes.toBytes("xiaoming"));

        Delete del_1 = new Delete(Bytes.toBytes("zhangshan"));

        Append append_1 = new Append(Bytes.toBytes("xiaoming"));
        append_1.add(Bytes.toBytes("course"), Bytes.toBytes("yuwen"), Bytes.toBytes("分"));
        append_1.add(Bytes.toBytes("grade"), Bytes.toBytes(""), Bytes.toBytes("组"));

        rows.add(put_1);
        rows.add(get_1);
        rows.add(del_1);
        rows.add(append_1);

        Object[] results = new Object[rows.size()];

        //执行批处理
        //table.batch(rows,results);
        //执行批处理,有回调函数
        table.batchCallback(rows, results, new Batch.Callback<Result>() {
            @Override
            public void update(byte[] rowKeyBytes, byte[] keyValueBytes, Result result) {
                System.out.println(Bytes.toString(rowKeyBytes));
                System.out.println();
                System.out.println(Bytes.toString(keyValueBytes));
                System.out.println();
                System.out.println(result);
            }
        });

        //关闭资源
        table.close();
    }
}

 

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值