redis hbase MySQL_linux中mysql,mongodb,redis,hbase数据库操作

这篇博客介绍了如何使用HBase API进行数据添加和获取,如在HBase的'Student'表中添加并查询scofield的分数信息。同时,文章还探讨了Redis数据库的操作,包括利用哈希结构设计学生表,并演示了使用hgetall、hget命令查询及修改键值对的操作。
摘要由CSDN通过智能技术生成

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。

74a14d696f3f4367bc9077f79a789036.png

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值