hBase客户端API-增删改

来源:互联网 发布:房地产大数据公司 编辑:程序博客网 时间:2024/04/29 18:51
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();}
原创粉丝点击