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
    评论
使用Java API操作HBase非常简单和方便。HBase提供了一个Java库,可以使用它来连接和与HBase进行交互。下面是使用Java API操作HBase的步骤: 1. 首先,需要导入HBaseJava库。可以在项目的构建文件(例如pom.xml)中添加HBase相关依赖项,或者手动将HBase库添加到项目的类路径中。 2. 创建HBase的配置对象,并设置必要的配置参数。配置对象可以指定HBase的连接地址、端口号等信息。 3. 使用HBase的配置对象创建一个HBase的连接对象。连接对象允许与HBase进行通信。 4. 通过连接对象创建一个HBase的管理员对象。管理员对象用于对HBase的表进行管理,如创建表、删除表等操作。 5. 创建HBase表的描述符对象,并指定表的名称、列族等信息。 6. 使用管理员对象创建HBase表。可以使用表的描述符对象来定义表的结构。 7. 使用HBase表的描述符对象创建一个表对象。表对象用于与HBase的表进行交互。 8. 使用表对象执行各种操作,如插入数据、更新数据、删除数据等。可以使用行键(row key)和列族名(column family)来定位和操作特定的数据。 9. 关闭与HBase的连接,释放资源。 通过以上步骤,可以使用Java API来连接和操作HBase。在实际应用中,还可以根据具体需求来添加其他操作,如查询数据、扫描表等。使用Java API操作HBase可以灵活地控制和管理HBase中的数据。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值