HBase数据库基础操作

2021/10/25 2:10:12

本文主要是介绍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();
    }
}

 



这篇关于HBase数据库基础操作的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程