zoukankan      html  css  js  c++  java
  • HBase实验

    介绍

    该文为Hadoop课程的HBase的应用实验

    实验题目

    Hbase的应用

    实验目的

    1、掌握Hbase shell操作。

    2、掌握Hbase API编程。

    实验要求:

    用Hbase shell操作 ,调用API创建一个student表,其结构如下表所示

    Row Key address score
    province city street Java Hadoop Math
    zhangsan guangdong guangzhou yinglonglu 85 80 90
    lisi guangxi guilin putuolu 87 82 78

    查询zhangsan的地址(address)

    查询 lisi 的Hadoop成绩。

    二、 实验方案

    (主要写Hbase shell命令 和 运用API 编写的程序及运行结果,此语句要删除)

    1, 创建表student有两个列族address和score

     create'student', {NAME=>'address'}, {NAME=>'score'}
    

    2, 向表中添加数据,

    put'student','zhangsan','address:province','guangdon'
    
    put'student', 'zhangsan','address:city','guangzhou'
    
    put'student','zhangsan','address:street','yinglonglu'
    
    put'student','zhangsan','score:Java','85' 
    
    put'student','zhangsan','score:Hadoop','80'
    
    put'student', 'zhangsan','score:Math','90'
    
    put'student', 'lisi','address:province','guangxi'
    
    put'student', 'lisi','address:city','guilin'
    
    put'student','lisi','address:street','putuolu'
    
    put'student','lisi','score:Java','87'
    
    put'student','lisi','score:Hadoop','82'
    
    put'student','lisi','score:Math','78'
    
    1. 查询“zhangsan”的地址(address)
    get'student','zhangsan',’address’
    
    

    4.查询“lisi”的“Hadoop”成绩

    get'student','lisi',’ 'score:Hadoop'’
    
     
    
    //创建表描述对象
    
        HTableDescriptor TableDescriptor = new HTableDescriptor(tableName);
    
       //通过表描述对象,添加列簇
    
       hTableDescriptor columnDesc1=new HColumnDescriptor("address ");
    
    hTableDescriptor columnDesc1=new HColumnDescriptor("score");
    
       //通过admin创建表,需要传入表描述对象
    
       admin.createTable(hTableDescriptor);
    
       System.out.println("创建表");
    
     
    
    public void initConnection() {
    
      try{
    
       connection = ConnectionFactory.createConnection(config);
    
      } catch (IOException e) {
    
       System.out.println("连接数据库");
    
      }
    
    }
    
    public void put() throws IOException {
    
      //1. 定义表的名称
    
      TableName tableName = TableName.valueOf("student");
    
      
    
      //2. 获取表对象
    
      Table table = connection.getTable(tableName);
    
     
    
      //3. 准备数据
    
      String rowkey = "rowkey_zhangsan";
    
      Put put= new Put(Bytes.toBytes(rowKey));
    
        put.addColumn(Bytes.toBytes("address"), Bytes.toBytes("province"), Bytes.toBytes("guangdong"));
    
        put.addColumn(Bytes.toBytes("address "), Bytes.toBytes("city"), Bytes.toBytes("guangzhou"));
    
        put.addColumn(Bytes.toBytes("address "), Bytes.toBytes("street"), Bytes.toBytes("putuolu"));
    
        put.addColumn(Bytes.toBytes("score"), Bytes.toBytes("Java"), Bytes.toBytes("87"));
    
    put.addColumn(Bytes.toBytes("score"),Bytes.toBytes("Hadoop"), Bytes.toBytes("82"));
    
    put.addColumn(Bytes.toBytes("score"),Bytes.toBytes("Math"), Bytes.toBytes("78"));
    
     
    
     
    
    String rowkey = "rowkey_lisi";
    
      Put put= new Put(Bytes.toBytes(rowKey));
    
        put.addColumn(Bytes.toBytes("address"), Bytes.toBytes("province"), Bytes.toBytes("guangxi"));
    
        put.addColumn(Bytes.toBytes("address "), Bytes.toBytes("city"), Bytes.toBytes("guilin"));
    
        put.addColumn(Bytes.toBytes("address "), Bytes.toBytes("street"), Bytes.toBytes("yinglonglu"));
    
        put.addColumn(Bytes.toBytes("score"), Bytes.toBytes("Java"), Bytes.toBytes("85"));
    
    put.addColumn(Bytes.toBytes("score"),Bytes.toBytes("Hadoop"), Bytes.toBytes("80"));
    
    put.addColumn(Bytes.toBytes("score"),Bytes.toBytes("Math"), Bytes.toBytes("90"));
    

    // 4. 添加数据

        table.put(put);
    
        table.close();
    

    结论

    因吹斯汀

    参考资料

    HBase Java API、连接HBase、创建表、添加数据put、获取数据get、全表扫描scan 06 : 啊策策

  • 相关阅读:
    缓存算法之LRU与LFU
    银行家算法
    死锁,死锁的四个必要条件以及处理策略
    找出无序数组中位数的方法
    HTTP状态码
    进程调度算法
    宽字节wchar_t和窄字节char的相互转换
    胜天半子
    ? 题目 一道超难的奥数题,猜生日. A告诉B他生日的月份,告诉C他生日的日期 B说:“如果我不知道A的生日,那C肯定也不知道." C说:”本来我不知道,现在我知道了.“ B说:”哦,那我也知道了.
    有对夫妇生有一男一女,一天晚上,成员中的一个杀了另一个,剩下2个成员,1个是帮凶1个是目击者
  • 原文地址:https://www.cnblogs.com/MemoryDrive/p/13296974.html
Copyright © 2011-2022 走看看