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

  • 相关阅读:
    [CF 351B]Jeff and Furik[归并排序求逆序数]
    [置顶] 道德经之常与善人
    银联手机支付(.Net Csharp),3DES加密解密,RSA加密解密,RSA私钥加密公钥解密,.Net RSA 3DES C#
    SPOJ 375 (树链剖分+线段树)
    I.MX6 eMMC分区挂载
    I.MX6 android 4.2 源码下载
    I.MX6 android 源码下载
    Android 动态注册 亮屏、息屏广播
    Android 如何进入充电模式
    I.MX6 新版u-boot分析
  • 原文地址:https://www.cnblogs.com/MemoryDrive/p/13296974.html
Copyright © 2020-2023  润新知