NoSql实验(二)HBase数据库操作

学生表Student

     name

score

English

Math

Computer

zhangsan

69

86

77

lisi

55

100

88

         

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

(1)    用Hbase Shell命令创建学生表Student;

 

 

 

 

(2)    用scan命令浏览Student表的相关信息;

 

 

 

 

 

(3)    查询zhangsan的Computer成绩;

 

 

 

(4)修改lisi的Math成绩,改为95。

 

 

 

2.根据上面已经设计出的Student表,用HBase API编程实现以下操作:

(1)添加数据:English:45  Math:89     Computer:100

scofield

45

89

100

 

 

 

static {
    try {
        Configuration configuration = HBaseConfiguration.create();
        configuration.set("hbase.zookeeper.quorum","hadoop102,hadoop103,hadoop104");
        connection=ConnectionFactory.createConnection(configuration);
        admin=connection.getAdmin();
    } catch (IOException e) {
        e.printStackTrace();
    }
}
 
public static void putData(String tableName,String rowKey,String cf,String cn,String value) throws IOException {
     //1.获取表对象呢
   
Table table = connection.getTable(TableName.valueOf(tableName));
    //2.创建put对象(添加多行,则创建多个put对象)
   
Put put = new Put(Bytes.toBytes(rowKey));
    //3.put对象赋值(同一个put对象可以添加多个列)
   
put.addColumn(Bytes.toBytes(cf),Bytes.toBytes(cn),Bytes.toBytes(value));
    //4.插入数据
   
table.put(put);
    //5.关闭表连接

}

 

(2)获取scofield的English成绩信息。

 

public static void getData(String tableName,String rowKey,String cf,String cn) throws IOException {
    //1.获取表对象
   
Table table = connection.getTable(TableName.valueOf(tableName));
    //2.创建get对象
   
Get get = new Get(Bytes.toBytes(rowKey));
    //2.1指定获取的列族
   
get.addFamily(Bytes.toBytes(cf));
    //2.2指定列族和列
   
get.addColumn(Bytes.toBytes(cf),Bytes.toBytes(cn));
    //2.3获取数据的版本数
   
get.setMaxVersions();
    //3.获取数据
   
Result result=table.get(get);
    //4.解析result
  
for( Cell cell:result.rawCells()){
       //5.打印数据
      
System.out.printf("CF:"+Bytes.toString(CellUtil.cloneFamily(cell))+
               ",CN:"+Bytes.toString(CellUtil.cloneQualifier(cell))+
               ",Value:"+Bytes.toString(CellUtil.cloneValue(cell)));
   }
   //6.关闭表连接
   
table.close();

}
posted @ 2021-10-22 21:32  风吹过半夏  阅读(805)  评论(0编辑  收藏  举报