欢迎投稿

今日深度:

HBase基本命令,

HBase基本命令,


HBase基本命令
下面我们再看看看HBase的一些基本操作命令,我列出了几个常用的HBase Shell命令,如下:

名称
命令表达式
创建表 create '表名称', '列名称1','列名称2','列名称N'
添加记录       put '表名称', '行名称', '列名称:', '值'
查看记录 get '表名称', '行名称'
查看表中的记录总数 count  '表名称'
删除记录 delete  '表名' ,'行名称' , '列名称'
删除一张表 先要屏蔽该表,才能对该表进行删除,第一步 disable '表名称' 第二步  drop '表名称'
查看所有记录 scan "表名称"  
查看某个表某个列中所有数据 scan "表名称" , ['列名称:']
更新记录  就是重写一遍进行覆盖

  如果你是一个新手队HBase的一些命令还不算非常熟悉的话,你可以进入 hbase 的shell 模式中你可以输入 help 命令查看到你可以执行的命令和对该命令的说明,例如对scan这个命令,help中不仅仅提到有这个命令,还详细的说明了scan命令中可以使用的参数和作用,例如,根据列名称查询的方法和带LIMIT 、STARTROW的使用方法:
scan   Scan a table; pass table name and optionally a dictionary of scanner specifications.  Scanner specifications may include one or more of  the following: LIMIT, STARTROW, STOPROW, TIMESTAMP, or COLUMNS.  If no columns are specified, all columns will be scanned.  To scan all members of a column family, leave the qualifier empty as in  'col_family:'.  Examples:
            hbase> scan '.META.'
            hbase> scan '.META.', {COLUMNS => 'info:regioninfo'}
            hbase> scan 't1', {COLUMNS => ['c1', 'c2'], LIMIT => 10, STARTROW => 'xyz'}


使用Java API对HBase服务器进行操作

需要下列jar包
     hbase-0.20.6.jar
     hadoop-core-0.20.1.jar
     commons-logging-1.1.1.jar
     zookeeper-3.3.0.jar
     log4j-1.2.91.jar

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.KeyValue;
import org.apache.hadoop.hbase.client.HBaseAdmin;
import org.apache.hadoop.hbase.client.HTable;
import org.apache.hadoop.hbase.client.Result;
import org.apache.hadoop.hbase.client.ResultScanner;
import org.apache.hadoop.hbase.client.Scan;
import org.apache.hadoop.hbase.io.BatchUpdate;

@SuppressWarnings("deprecation")
public class HBaseTestCase {
    
    static HBaseConfiguration cfg = null;
    static {
        Configuration HBASE_CONFIG = new Configuration();
        HBASE_CONFIG.set("hbase.zookeeper.quorum", "192.168.50.216");
        HBASE_CONFIG.set("hbase.zookeeper.property.clientPort", "2181");
        cfg = new HBaseConfiguration(HBASE_CONFIG);
    }
    
    
    /**
     * 创建一张表
     */
    public static void creatTable(String tablename) throws Exception {
        HBaseAdmin admin = new HBaseAdmin(cfg);
        if (admin.tableExists(tablename)) {
            System.out.println("table   Exists!!!");
        }
        else{
            HTableDescriptor tableDesc = new HTableDescriptor(tablename);
            tableDesc.addFamily(new HColumnDescriptor("name:"));
            admin.createTable(tableDesc);
            System.out.println("create table ok .");
        }
    
         
    }
    
    /**
     * 添加一条数据
     */
    public static void addData (String tablename) throws Exception{
         HTable table = new HTable(cfg, tablename);
             BatchUpdate update = new BatchUpdate("Huangyi");  
             update.put("name:java", "http://www.javabloger.com".getBytes());  
             table.commit(update);  
         System.out.println("add data ok .");
    }
    
    /**
     * 显示所有数据
     */
    public static void getAllData (String tablename) throws Exception{
         HTable table = new HTable(cfg, tablename);
         Scan s = new Scan();
         ResultScanner ss = table.getScanner(s);
         for(Result r:ss){
             for(KeyValue kv:r.raw()){
                System.out.print(new String(kv.getColumn()));
                System.out.println(new String(kv.getValue()    ));
             }

         }
    }
    
    
    public static void  main (String [] agrs) {
        try {
                String tablename="tablename";
                HBaseTestCase.creatTable(tablename);
                HBaseTestCase.addData(tablename);
                HBaseTestCase.getAllData(tablename);
            } 
        catch (Exception e) {
            e.printStackTrace();
        }
        
    }
    
}

www.htsjk.Com true http://www.htsjk.com/hbase/40070.html NewsArticle HBase基本命令, HBase基本命令 下面我们再看看看HBase的一些基本操作命令,我列出了几个常用的HBase Shell命令,如下: 名称 命令表达式 创建表 create '表名称', '列名称1','列名称2','列名称...
相关文章
    暂无相关文章
评论暂时关闭