zoukankan      html  css  js  c++  java
  • HBase数据库基础操作

    实验要求:

    根据上面给出的学生表Student的信息,执行如下操作:

    • 用Hbase Shell命令创建学生表Student;
      create 'student','name', 'score'
      put 'student','01','name:name','zhangsan'
      put 'student','01','score:English','69'
      put 'student','01','score:Math','86'
      put 'student','01','score:Computer,'77'
      put 'student','02','name:name','lisi'
      put 'student','02','score:English','55'
      put 'student','02','score:Math','100'
      put 'student','02','score:Computer','88'

    • 用scan命令浏览Student表的相关信息; 
    scan 'student'

     

    •  查询zhangsan的Computer成绩;
    get 'student','01','score:Computer'

    •  修改lisi的Math成绩,改为95;
    put 'student' ,'02','score:Math','95'

     

    核心代码:

    //5.插入数据
    public static void putData(String tableName, String rowKey,
                                  String columnFamily, String
                                          column, String value) throws IOException{
        //获取表对象
        Table table=connection.getTable(TableName.valueOf(tableName));
        //创建put对象
        Put put=new Put(Bytes.toBytes(rowKey));
        //给put对象赋值
        put.addColumn(Bytes.toBytes(columnFamily),Bytes.toBytes(column),Bytes.toBytes(value));
        put.addColumn(Bytes.toBytes(columnFamily),Bytes.toBytes(column),Bytes.toBytes(value));
        put.addColumn(Bytes.toBytes(columnFamily),Bytes.toBytes(column),Bytes.toBytes(value));
        put.addColumn(Bytes.toBytes(columnFamily),Bytes.toBytes(column),Bytes.toBytes(value));
        //添加数据
        table.put(put);
        //关闭连接
        table.close();
    }
    
    public static void main(String[] args) throws IOException {
               //5.插入数据
            putData("student","03","name","name","scofield");
            putData("student","03","score","English","45");
            putData("student","03","score","Math","89");
            putData("student","03","score","Computer","100");
                    //关闭资源
              close();
        }
    }

    •  获取scofield的English成绩信息。
    public static void getData(String tableName,String rowKey,String columnFamily,String column) throws IOException{
        //获取对象
        Table table=connection.getTable(TableName.valueOf(tableName));
        //创建GET对象
        Get get=new Get(Bytes.toBytes(rowKey));
            //指定获取的列族
            get.addFamily(Bytes.toBytes(columnFamily));
            //指定列族和列
            get.addColumn(Bytes.toBytes(columnFamily),Bytes.toBytes(column));
        //获取数据
        Result result=table.get(get);
        //解析result
        for (Cell cell : result.rawCells()) {
            //打印数据
            System.out.println("columnFamily:"+Bytes.toString(CellUtil.cloneFamily(cell))+
                    ",column:"+Bytes.toString(CellUtil.cloneQualifier(cell))+
                    ",value:"+Bytes.toString(CellUtil.cloneValue(cell)));
        }
        //关闭表连接
        table.close();
    }
    public static void main(String[] args) throws IOException {
            //获取数据
                //获取单行数据
                getData("student","03","score","English");
            //关闭资源
              close();
        }
    }

  • 相关阅读:
    Centos7 安装zabbix3.0 服务端 详细
    kubernetes 创建nginx 容器
    kubernetes 创建tomcat 容器
    kubernetes创建yaml,pod服务一直处于 ContainerCreating状态的原因查找与解决
    SpringMVC,SpringBoot利用ajax上传文件到后台
    SpringMVC,SpringBoot使用ajax传递对象集合/数组到后台
    IDEA修改显示星号*和热部署
    IDEA上的项目托管到码云步骤
    java代码块,静态代码块,静态变量,构造方法执行顺序
    java 动态绑定 多态
  • 原文地址:https://www.cnblogs.com/zyj3955/p/15455547.html
Copyright © 2011-2022 走看看