Hbase简单操作

    • 用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();
        }
    }
    复制代码
posted @ 2021-10-25 19:51  枫叶鎏霜  阅读(155)  评论(0)    收藏  举报