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 : 啊策策

  • 相关阅读:
    列表导航栏实例(01)
    点阵图形的高效旋转算法
    7种健康食物助抗初秋干燥
    打造自己的reset.css
    如何点击链接直接跳转到app store指定应用下载页面
    【iOSiap防护】验证用户付费收据!拒绝iap Cracker!拒绝iap Free!让iphone越狱用户无从下手!【2012年5月2日更新防护iap Free的方法】
    如何自定义协议从自己的一个app打开另一个app
    传统网站与Web标准——DIV+CSS布局实例
    ANSI、Unicode、Unicode big endian、UTF8编码
    良好的XHTML规则
  • 原文地址:https://www.cnblogs.com/MemoryDrive/p/13296974.html
Copyright © 2011-2022 走看看