HBase初学-增删查改

put用法:

put '表名','主键','列族:列名','值'

create用法:

create '表名','列族名'

scan用法:
查询表

scan '表明'

get用法:
查询具体列族

get '表名','主键','列族名:列'

删除表步骤:
1、禁用表

disable '表名'

2、删除表

drop '表名'

Java 操作表:
准备:
1、导入HBase中lib中的Jar文件和JUnit Jar文件
2、把hbase-site.xml、hdfs-site.xml配置文件拷贝到工程src目录下
代码:

import java.io.IOException;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.Cell;
import org.apache.hadoop.hbase.CellUtil;
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.Get;
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.client.Result;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;


public class HbaseDemo {
    HBaseAdmin hBaseAdmin;
    String TN="phone";
    HTable hTable;
    @Before
    public void begin() throws MasterNotRunningException, ZooKeeperConnectionException, IOException{
        Configuration conf=new Configuration();
        conf.set("hbase.zookeeper.quorum","node1,node2,node3");
        hBaseAdmin=new HBaseAdmin(conf);
        hTable=new HTable(conf, TN);

    }
    @After
    public void end(){
        if(hBaseAdmin!=null){
            try {
                hBaseAdmin.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        if(hTable!=null){
            try {
                hTable.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

    }
//  @Test
//  public void insert() throws IOException{
//      String rowkey="13964223261_201612312456";
//      Put put=new Put(rowkey.getBytes());
//      put.add("cf1".getBytes(), "type".getBytes(), "1".getBytes());
//      put.add("cf1".getBytes(), "time".getBytes(), "2016".getBytes());
//      put.add("cf1".getBytes(), "time".getBytes(), "2016".getBytes());
//      hTable.put(put);
//  }
//  @Test
//  public void createTable() throws IOException{
//      if(hBaseAdmin.tableExists(TN)){
//          hBaseAdmin.disableTable(TN);
//          hBaseAdmin.deleteTable(TN);
//      }
//      HTableDescriptor hTableDescriptor=new 
//              HTableDescriptor(TableName.valueOf(TN));
//      HColumnDescriptor family=new HColumnDescriptor("cf1");
//      family.setBlockCacheEnabled(true);
//      family.setInMemory(true);
//      family.setMaxVersions(1);
//      hTableDescriptor.addFamily(family);
//      hBaseAdmin.createTable(hTableDescriptor);
//  }
    @Test
    public void get() throws IOException{
        String rowkey="13964223261_201612312456";

        Get get=new Get(rowkey.getBytes());
        //下面两行表示请求查询服务器时只查询'type'、'time'字段
        get.addColumn("cf1".getBytes(), "type".getBytes());
        get.addColumn("cf1".getBytes(), "time".getBytes());

        Result res=hTable.get(get);
        //从结果中拿出需要的结果
        Cell cell=res.getColumnLatestCell("cf1".getBytes(), "time".getBytes());
        System.out.println("========="+new String(CellUtil.cloneValue(cell)));
        Cell cell1=res.getColumnLatestCell("cf1".getBytes(), "type".getBytes());
        System.out.println("========="+new String(CellUtil.cloneValue(cell1)));
    }

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值