设为首页 收藏本站
查看: 912|回复: 0

[经验分享] Hadoop上路-04_HBase0.98.0入门

[复制链接]
累计签到:1 天
连续签到:1 天
发表于 2015-7-13 10:05:10 | 显示全部楼层 |阅读模式
  以下操作在Hadoop分布式集群基础上进行。
一。分布式环境搭建
  下载:http://www.apache.org/dyn/closer.cgi/hbase/ ,hbase-0.98.0-hadoop2-bin.tar.gz。

1.在master主控机安装HBase
1)解压
  SHELL$ tar -zxvf hbase-0.98.0-hadoop2-bin.tar.gz
  SHELL$ mv hbase-0.98.0-hadoop2 ~/hbase0.98.0hadoop2

  
2)配置环境变量
  (1)修改/etc/profile文件
  SHELL$ sudo gedit /etc/profile
DSC0000.png
  (2)验证
DSC0001.png

  
3)修改%HBASE%/conf/hbase-env.sh
DSC0002.png

  
4)修改$HBASE_HOME/conf/hbase-site.xml
  





   
        
        hbase.master
        hdfs://192.168.1.240:60000
   
   
        
        hbase.rootdir
        
        hdfs://192.168.1.240:9000/hbase
   
   
        
        hbase.cluster.distributed
        true
   
   
        
        hbase.zookeeper.quorum
        
        192.168.1.241,192.168.1.242,192.168.1.243
   
   
        
        hbase.tmp.dir
        /home/hadoop/hbase0.98.0hadoop2/hbase-tmp
   
   
        
        hbase.zookeeper.property.dataDir
        /home/hadoop/hbase0.98.0hadoop2/zookeeper-temp
   
  
  

5)$HBASE_HOME/conf/regionservers 文件增加

DSC0003.png

  
2.把HBase复制到slave从属机
  SHELL$ sudo scp -rpv /home/hadoop/hbase0.98.0hadoop2/ hadoop@hapslave*:/home/hadoop/

  
3.启动HBase集群
  在Hadoop集群启动后,再启动HBase集群。
  SHELL$ start-hbase.sh
DSC0004.png
  
  在主控机通过web界面查看(本例配置4个节点):
DSC0005.png

  
4.停止HBase集群
  SHELL$ stop-hbase.sh
DSC0006.png


  
二。HBase Shell
  SHELL$ hbase shell
DSC0007.png

  
1.建表create
DSC0008.png

  
2.列出全部表list
DSC0009.png

  
3.表描述describe
DSC00010.png

  
4.删除表disable,drop
DSC00011.png

  
5.插入条目put
DSC00012.png

  
6.展示全表scan
DSC00013.png

  
7.查询条目get
DSC00014.png

  
8.更新条目put
DSC00015.png

  
9.删除条目delete
DSC00016.png
  清空表:
DSC00017.png
  truncate是一个能够快速清空资料表内所有资料的SQL语法。并且能针对具有自动递增值的字段,做计数重置归零重新计算的作用

  
10.统计参数
DSC00018.png


  
  
三。JavaAPI
  全部API在%HBase%/docs目录里,完全是英文的。
  本例所须全部jar都可以在%HBase安装目录%/lib目录中找到。图省事,我一股脑儿全导入了。
DSC00019.png

  
1.创建一张表
  

package com.cuiweiyou.test;
// www.cuiweiyou.com
import java.io.IOException;
import java.util.Iterator;
import java.util.List;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.Cell;
import org.apache.hadoop.hbase.CellUtil;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.HColumnDescriptor;
import org.apache.hadoop.hbase.HTableDescriptor;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.Delete;
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.apache.hadoop.hbase.client.ResultScanner;
import org.apache.hadoop.hbase.client.Scan;
import org.apache.hadoop.hbase.util.Bytes;
import org.junit.Test;
public class HBaseTest {
    //创建表
    @Test
    public void creatTable() throws Exception {
        String strTBName = "tb_test";    //表
        String strColFamily = "cf";        //列族
        //配置
        Configuration conf = HBaseConfiguration.create();
        conf.set("hbase.zookeeper.quorum", "192.168.1.241,192.168.1.242,192.168.1.243");
        //管理员
        HBaseAdmin hbaseAdmin = new HBaseAdmin(conf);
        //addColumn(String tableName, HColumnDescriptor column)            //向一个已经存在的表添加咧
        //checkHBaseAvailable(HBaseConfiguration hbaseConf)                //静态函数,查看HBase是否处于运行状态
        //deleteTable(byte[] tableName)                                    //删除一个已经存在的表
        //enableTable(byte[] tableName)                                    //使表处于有效状态
        //disableTable(byte[] tableName)                                //使表处于无效状态
         //HTableDescriptor[] listTables()                                //列出所有用户控件表项
        //modifyTable(byte[] tableName, HTableDescriptor tableDesc)        //修改表的模式,是异步的操作,耗时
        //tableExists(String tableName)                                    //检查表是否存在
        //表名称
        TableName tableName = TableName.valueOf(strTBName);
        //表描述器
        HTableDescriptor tableDesc = new HTableDescriptor(tableName);
        //removeFamily(byte[] column)            //移除一个列族
        //getName()                                //获取表的名字
        //getValue(byte[] key)                    //获取属性的值
        //setValue(String key, String value)    //设置属性的值
        tableDesc.addFamily(new HColumnDescriptor(strColFamily));//添加列族
        //创建一个表,同步操作
        hbaseAdmin.createTable(tableDesc);
        
        System.out.println("创建表" +  strTBName + "成功");
    }
}  
DSC00020.png

  
2.添加一条记录
  


    //为表添加数据
    @Test
    public void addData() throws IOException {
        String strTBName = "tb_test";
        String strColFamily = "cf";
        String strColumn = "col";        //列名
        String strRowKey = "row1";        //行号
        String strValue = "values";        //值

        Configuration conf = HBaseConfiguration.create();
        conf.set("hbase.zookeeper.quorum", "192.168.1.241,192.168.1.242,192.168.1.243");
        //表实例
        HTable table = new HTable(conf, strTBName);   
        //close()                                                        释放所有的资源或挂起内部缓冲区中的更新
        //exists(Get get)                                                检查Get实例所指定的值是否存在于HTable的列中
        //get(Get get)                                                    获取指定行的某些单元格所对应的值
        //getEndKeys()                                                    获取当前一打开的表每个区域的结束键值
        //getScanner(byte[] family)                                        获取当前给定列族的scanner实例
        //getTableDescriptor()                                            获取当前表的HTableDescriptor实例
        //getTableName()                                                获取表名
        //isTableEnabled(HBaseConfiguration conf, String tableName)        检查表是否有效
        // 获取所有的列族
        HColumnDescriptor[] columnFamilies = table.getTableDescriptor().getColumnFamilies();
        //HColumnDescriptor的常用方法:
        //getName()                                //获取列族的名字
        //getValue(byte[] key)                    //获取对应的属性的值
        //setValue(String key, String value)    //设置对应属性的值
        //插入器
        Put put = new Put(Bytes.toBytes(strRowKey));// 设置行号,RowKey
        //add(byte[] family, byte[] qualifier, byte[] value)            将指定的列和对应的值添加到Put实例中
        //add(byte[] family, byte[] qualifier, long ts, byte[] value)    将指定的列和对应的值及时间戳添加到Put实例中
        //getRow()                                                        获取Put实例的行
        //getRowLock()                                                    获取Put实例的行锁
        //getTimeStamp()                                                获取Put实例的时间戳
        //isEmpty()                                                        检查familyMap是否为空
        //setTimeStamp(long timeStamp)                                    设置Put实例的时间戳
        
        for (int i = 0; i < columnFamilies.length; i++) {
            String familyName = columnFamilies.getNameAsString(); // 获取列族名
            
            //指定列族
            if (familyName.equals(strColFamily)) {
                //插入
                put.add(Bytes.toBytes(familyName), Bytes.toBytes(strColumn), Bytes.toBytes(strValue));
            }
        }
        
        table.put(put);    //运行插入器
        
        System.out.println("存入数据完毕");
    }  
DSC00021.png

  
3.读取指定行记录
  


    //根据RowKey查询整行
    @Test
    public void getRow() throws IOException {
        String strTBName = "tb_test";
        String strRowKey = "row1";
        Configuration conf = HBaseConfiguration.create();
        conf.set("hbase.zookeeper.quorum", "192.168.1.241,192.168.1.242,192.168.1.243");
        HTable table = new HTable(conf, strTBName);    //获取表实例
        
        //查询器
        Get get = new Get(Bytes.toBytes(strRowKey));    //查询指定行
        //addColumn(byte[] family, byte[] qualifier)    获取指定列族和列修饰符对应的列
        //addFamily(byte[] family)                        通过指定的列族获取其对应列的所有列
        //setTimeRange(long minStamp,long maxStamp)        获取指定取件的列的版本号
        //setFilter(Filter filter)                        当执行Get操作时设置服务器端的过滤器

        Result result = table.get(get);
        //containsColumn(byte[] family, byte[] qualifier)        检查指定的列是否存在
        //getFamilyMap(byte[] family)                            获取对应列族所包含的修饰符与值的键值对
        //getValue(byte[] family, byte[] qualifier)                获取对应列的最新值
        
        List listCells = result.listCells();    //指定行、全部列族的全部列

        for (Cell cell : listCells) {
            System.out.println("列  族:" + Bytes.toString(CellUtil.cloneFamily(cell)));
            System.out.println("列  名:" + Bytes.toString(CellUtil.cloneQualifier(cell)));
            System.out.println("列  值:" + Bytes.toString(CellUtil.cloneValue(cell)));
            System.out.println("时间戳:" + cell.getTimestamp());
        }
    }  
DSC00022.png

  
4.显示所有数据
  


    //遍历全部条目
    @Test
    public void getAllRows() throws IOException {
        String strTBName = "tb_test";
        Configuration conf = HBaseConfiguration.create();
        conf.set("hbase.zookeeper.quorum", "192.168.1.241,192.168.1.242,192.168.1.243");
        HTable table = new HTable(conf, strTBName);    //获取表实例
        //扫描器
        ResultScanner resultScanner = table.getScanner(new Scan());    //针对全表的查询器
        Iterator results = resultScanner.iterator();
        while(results.hasNext()) {
            Result result = results.next();
            List cells = result.listCells();
            for(Cell cell : cells) {
                System.out.println("列  族:" + Bytes.toString(CellUtil.cloneFamily(cell)));
                System.out.println("列  名:" + Bytes.toString(CellUtil.cloneQualifier(cell)));
                System.out.println("列  值:" + Bytes.toString(CellUtil.cloneValue(cell)));
                System.out.println("时间戳:" + cell.getTimestamp() + "\n------------------");
            }
        }
    }  
DSC00023.png

  
5.更新条目
  


    //更新表中某行的某一列
    @Test
    public void updateTable() throws IOException {
        String strTBName = "tb_test";
        String strColFamily = "cf";
        String strColumn = "col";
        String strRowKey = "row1";
        String strNewValue = "NewValues";
        Configuration conf = HBaseConfiguration.create();
        conf.set("hbase.zookeeper.quorum", "192.168.1.241,192.168.1.242,192.168.1.243");
        HTable table = new HTable(conf, strTBName);    //获取表实例

        Put put = new Put(Bytes.toBytes(strRowKey));
        //仍然是插入操作(已知列族,已知列,新值)
        put.add(Bytes.toBytes(strColFamily), Bytes.toBytes(strColumn), Bytes.toBytes(strNewValue));
        table.put(put);
        System.out.println("更新结束");
    }  
DSC00024.png

  
6.删除单元格
  


    //删除指定行的指定的列(删除单元格)
    @Test
    public void deleteColumn() throws IOException {
        String strTBName = "tb_test";
        String strColFamily = "cf";
        String strColumn = "col";
        String strRowKey = "row1";
        Configuration conf = HBaseConfiguration.create();
        conf.set("hbase.zookeeper.quorum", "192.168.1.241,192.168.1.242,192.168.1.243");
        HTable table = new HTable(conf, strTBName);    //获取表实例
        //删除器
        Delete del = new Delete(Bytes.toBytes(strRowKey));
        del.deleteColumns(Bytes.toBytes(strColFamily), Bytes.toBytes(strColumn));
        table.delete(del);
        
        System.out.println("行:" + strRowKey + ",列族:"+ strColFamily +",列:"+ strColumn +",删除完毕");
    }  
DSC00025.png

  
7.删除整行
  


    //删除整行
    @Test
    public void deleteAllColumn() throws IOException {
        String strTBName = "tb_test";
        String strRowKey = "row1";
        Configuration conf = HBaseConfiguration.create();
        conf.set("hbase.zookeeper.quorum", "192.168.1.241,192.168.1.242,192.168.1.243");
        HTable table = new HTable(conf, strTBName);    //获取表实例

        Delete deleteAll = new Delete(Bytes.toBytes(strRowKey));
        table.delete(deleteAll);
        System.out.println("这一行全删除了");
    }  
DSC00026.png

  
8.删除表单
  


    //删除表
    @Test
    public void deleteTable() throws IOException {
        String strTBName = "tb_test";
        
        Configuration conf = HBaseConfiguration.create();
        conf.set("hbase.zookeeper.quorum", "192.168.1.241,192.168.1.242,192.168.1.243");
        HBaseAdmin admin = new HBaseAdmin(conf);
        admin.disableTable(strTBName);
        admin.deleteTable(strTBName);
        System.out.println(strTBName + "表 删除了");
    }  
DSC00027.png
  
  
  - end
威格灵博客:www.cuiweiyou.com

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-86172-1-1.html 上篇帖子: [大牛翻译系列]Hadoop(21)附录D.1 优化后的重分区框架 下篇帖子: GWT+Hadoop+Hbase搭建(转)
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表