hBase客户端API-增删改

package os.hbase.index;

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

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.HColumnDescriptor;
import org.apache.hadoop.hbase.HTableDescriptor;
import org.apache.hadoop.hbase.MasterNotRunningException;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.ZooKeeperConnectionException;
import org.apache.hadoop.hbase.client.Delete;
import org.apache.hadoop.hbase.client.HBaseAdmin;
import org.apache.hadoop.hbase.client.HTable;
import org.apache.hadoop.hbase.client.Put;
import org.apache.hadoop.hbase.util.Bytes;
import org.junit.Test;

public class HbaseDaoDemo {


    /*
     * 删除数据
     */
    @Test
    public void testDel() throws IOException {

        Configuration conf = HBaseConfiguration.create();
        conf.set("hbase.zookeeper.quorum","os-1:2181,os-2:2181,os-3:2181");
        //拿到一个客户端对象
        HTable hTable = new HTable(conf, "cart");

        //指定删除的行键中指定的列
        Delete delete = new Delete(Bytes.toBytes("user_02"));
        delete.deleteColumn(Bytes.toBytes("product"),Bytes.toBytes("product_id_01"));

        hTable.delete(delete);
    }

    /*
     * 一次插入多行数据
     */

    @Test
    public void testPuts() throws IOException {
        Configuration conf = HBaseConfiguration.create();
        conf.set("hbase.zookeeper.quorum","os-1:2181,os-2:2181,os-3:2181");
        //拿到一个客户端对象
        HTable hTable = new HTable(conf, "cart");



        ArrayList<Put> puts = new ArrayList<Put>();

        Put put = new Put(Bytes.toBytes("user_02"));
        put.add(Bytes.toBytes("product"), Bytes.toBytes("product_id_01"),Bytes.toBytes("pd-0002--01"));
        put.add(Bytes.toBytes("product"), Bytes.toBytes("product_num"),Bytes.toBytes("5"));
        put.add(Bytes.toBytes("goods"), Bytes.toBytes("product_id"),Bytes.toBytes("gd-0001-99"));
        put.add(Bytes.toBytes("goods"), Bytes.toBytes("product_num_01"),Bytes.toBytes("6"));


        Put put1 = new Put(Bytes.toBytes("user_03"));
        put1.add(Bytes.toBytes("product"), Bytes.toBytes("product_id_01"),Bytes.toBytes("pd-0002--01"));
        put1.add(Bytes.toBytes("product"), Bytes.toBytes("product_num"),Bytes.toBytes("5"));
        put1.add(Bytes.toBytes("goods"), Bytes.toBytes("product_id"),Bytes.toBytes("gd-0001-99"));
        put1.add(Bytes.toBytes("goods"), Bytes.toBytes("product_num_01"),Bytes.toBytes("6"));

        puts.add(put);
        puts.add(put1);

        hTable.put(puts);
        hTable.close();
    }

    /*
     * 一次插入一行数据
     */
    @Test
    public void testPut() throws IOException{

        Configuration conf = HBaseConfiguration.create();
        conf.set("hbase.zookeeper.quorum","os-1:2181,os-2:2181,os-3:2181");
        //拿到一个客户端对象
        HTable hTable = new HTable(conf, "cart");

        //将插入的数据封装成对象
        Put put = new Put(Bytes.toBytes("user_01"));
        put.add(Bytes.toBytes("product"), Bytes.toBytes("product_id"),Bytes.toBytes("pd-0001"));
        put.add(Bytes.toBytes("product"), Bytes.toBytes("product_num"),Bytes.toBytes("5"));
        put.add(Bytes.toBytes("goods"), Bytes.toBytes("product_id"),Bytes.toBytes("gd-0001"));
        put.add(Bytes.toBytes("goods"), Bytes.toBytes("product_num"),Bytes.toBytes("6"));
        //插入数据
        hTable.put(put);

        //关闭连接
        hTable.close();

    }

    /*
     * 修改数据
     */

    @Test
    public void testUpdate() throws IOException {

        Configuration conf = HBaseConfiguration.create();
        conf.set("hbase.zookeeper.quorum","os-1:2181,os-2:2181,os-3:2181");
        //拿到一个客户端对象
        HTable hTable = new HTable(conf, "cart");

        Put put = new Put(Bytes.toBytes("user_03"));
        put.add(Bytes.toBytes("product"),Bytes.toBytes("product_num"),Bytes.toBytes("222"));

        hTable.put(put);
        hTable.close();


}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值