2.根据上面已经设计出的 Student 表, 用 HBase API 编程实现以下操作:
(1)添加数据: English:45 Math:89 Computer:100scofield45 89 100源代码:
package hbase_test;
import java.io.IOException;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.Admin;
import org.apache.hadoop.hbase.client.Connection;
import org.apache.hadoop.hbase.client.ConnectionFactory;
import org.apache.hadoop.hbase.client.Put;
import org.apache.hadoop.hbase.client.Table;public classHBaseTest {public staticConfiguration configuration;public staticConnection connection;public staticAdmin admin;public static voidmain(String[] args) {
configuration=HBaseConfiguration.create();
configuration.set("hbase.rootdir", "hdfs://localhost:9000/hbase");try{
connection=ConnectionFactory.createConnection(configuration);
admin=connection.getAdmin();
insertRow("Student","scofield","score","English","45");
insertRow("Student","scofield","score","Math","89");
insertRow("Student","scofield","score","Computer","100");
System.out.println("插入成功!");
}catch(IOException e) {//TODO Auto-generated catch block
e.printStackTrace();
}
close();
}public static voidinsertRow(String tableName, String rowKey, String colFamily, String col, String val) {try{
Table table=connection.getTable(TableName.valueOf(tableName));
Put put= newPut(rowKey.getBytes());
put.addColumn(colFamily.getBytes(), col.getBytes(), val.getBytes());
table.put(put);
table.close();
}catch(IOException e) {//TODO Auto-generated catch block
e.printStackTrace();
}
}public static voidclose() {try{if (admin != null) {
admin.close();
}if(null!=connection) {
connection.close();
}
}catch(IOException e) {//TODO Auto-generated catch block
e.printStackTrace();
}
}
}
(2)获取 scofield 的 English 成绩信息。源代码:
package hbase_test;
import java.io.IOException;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.Cell;
import org.apache.hadoop.hbase.CellUtil;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.Admin;
import org.apache.hadoop.hbase.client.Connection;
import org.apache.hadoop.hbase.client.ConnectionFactory;
import org.apache.hadoop.hbase.client.Get;
import org.apache.hadoop.hbase.client.Put;
import org.apache.hadoop.hbase.client.Result;
import org.apache.hadoop.hbase.client.Table;public classHBaseTest2 {public staticConfiguration configuration;public staticConnection connection;public staticAdmin admin;public static voidmain(String[] args) {
configuration=HBaseConfiguration.create();
configuration.set("hbase.rootdir", "hdfs://localhost:9000/hbase");try{
connection=ConnectionFactory.createConnection(configuration);
admin=connection.getAdmin();
getData("Student","scofield","score","English");
System.out.println("输出完成!");
}catch(IOException e) {//TODO Auto-generated catch block
e.printStackTrace();
}
close();
}public static voidgetData(String tableName, String rowKey, String colFamily, String col) {try{
Table table=connection.getTable(TableName.valueOf(tableName));
Getget=newGet(rowKey.getBytes());get.addColumn(colFamily.getBytes(), col.getBytes());
Result result=table.get(get);
showCell(result);
table.close();
}catch(IOException e) {//TODO Auto-generated catch block
e.printStackTrace();
}
}private static voidshowCell(Result result) {
Cell[] cells=result.rawCells();for(Cell cell:cells) {
System.out.println("RowName:"+new String(CellUtil.cloneRow(cell))+" ");
System.out.println("Timetamp:"+cell.getTimestamp()+" ");
System.out.println("column Family"+new String(CellUtil.cloneFamily(cell))+" ");
System.out.println("row Name:"+new String(CellUtil.cloneValue(cell))+" ");
System.out.println("value"+new String(CellUtil.cloneValue(cell))+" ");
}
}public static voidclose() {try{if (admin != null) {
admin.close();
}if(null!=connection) {
connection.close();
}
}catch(IOException e) {//TODO Auto-generated catch block
e.printStackTrace();
}
}
}
(三) Redis 数据库操作
Student 键值对如下:
zhangsan:{
English:69Math:86Computer:77}
lisi:{
English:55Math:100Computer:88}1. 根据上面给出的键值对, 完成如下操作:
(1)用 Redis 的哈希结构设计出学生表 Student (键值可以用 student.zhangsan 和 student.lisi来表示两个键值属于同一个表);
(2) 用 hgetall 命令分别输出 zhangsan 和 lisi 的成绩信息;
(3) 用 hget 命令查询 zhangsan 的 Computer 成绩;
(4)修改 lisi 的 Math 成绩, 改为 95。