数据库
首页 > 数据库> > HBase数据库基础操作

HBase数据库基础操作

作者:互联网

实验要求:

 

 

 

根据上面给出的学生表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'

 

 

 

get 'student','01','score:Computer'

 

 

 

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();
    }
}

 

 

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();
    }
}

 

标签:String,数据库,Bytes,score,student,toBytes,put,操作,HBase
来源: https://www.cnblogs.com/zyj3955/p/15455547.html