hadoop之mysql实验总结_Hadoop实验——NoSQL与关系型
数据库的⽐较
实验⽬的
理解四种数据库(MySQL,HBase,Redis,MongoDB)的概念以及不同点。
熟练使⽤四种数据库操作常⽤的Shell命令。
熟悉四种数据库操作常⽤的Java API。
实验平台
操作系统:Ubuntu-16.04
Hadoop版本:2.6.0
JDK版本:1.8
IDE:Eclipse
HBase版本:1.2.3
MySQL版本:5.7.16
MongoDB版本:2.6.10
Redis:版本:3.0.6
IDE:Eclipse
数据库的安装
MySQL的安装
更新APT
sudo apt-get update
打开终端,安装mysql-server
sudo apt-get install mysql-server
输⼊密码
安装mysql-client
sudo apt-get install mysql-client
安装libmysqlclient-dev
sudo apt-get install libmysqlclient-dev
测试是否安装成功
sudo netstat -tap | grep mysql
通过上述命令检查之后,如果看到有mysql 的socket处于 listen 状态则表⽰安装成功。
Redis的安装
安装redis-server
sudo apt-get install redis-server
测试是否安装成功
sudo netstat -tap|grep redis
MongoDB的安装
安装mongodb-server
sudo apt-get install mongodb-server
测试是否安装成功
sudo netstat -tap|grep mongod
实验内容和要求
⼀,MySQL数据库操作:
student学⽣表name
English
Math
Computer
zhangsan
69
86
77
lisi
55
100
88
根据上⾯给出的表格,利⽤MySQL设计出student学⽣表格。
登陆MySQL(退出指令为quit)
mysql -u root -p
输⼊密码
创建数据库
create database test;
使⽤数据库
use test;
创建student表
create table student(
name varchar(30) not null,
English tinyint unsigned not null,
Math tinyint unsigned not null,
Computer tinyint unsigned not null
);
初始化数据
insert into student values("zhangsan",69,86,77);
insert into student values("lisi",55,100,88);
查看student表
select * from student;
查看zhangsan的Computer成绩
select name , Computer from student where name = "zhangsan";
修改lisi的Math成绩,改为95
update student set Math=95 where name="lisi";
根据上⾯已经设计出的student表,通过JDBC操作MySQL
添加数据:Name:scofield English:45 Math:89 Computer:100
Eclipse的使⽤
1. 到 File 菜单,选择 New -> Java Project
1. 输⼊ Project name,然后Finish
1. 点开项⽬,到 src ⽂件夹,右键选择 New -> Class
1. 输⼊ Package 和 Name,然后Finish
1. 将jar包从主机拉到虚拟机中的Home
1. 右键⼯程,选择 Properties ,然后在⼯程中导⼊外部jar包
1. 写好Java代码(填上密码),右键选择 Run As -> Java Application,就可以在Console⾥看到结果了
JAVA代码:
sql;
import java.sql.*;
public class MysqlTest {
static final String DRIVER = "sql.jdbc.Driver";
static final String DB = "jdbc:mysql://localhost/test?useUnicode=true&characterEncoding=utf-8&useSSL=false"; static final String USER = "root";
static final String PASSWD = "";
public static void main(String[] args) {
mysql下载jar包Connection conn = null;
Statement stmt = null;
try {
Class.forName(DRIVER);
System.out.println("Connecting to a ");
conn = Connection(DB, USER, PASSWD);
stmt = ateStatement();
String sql = "insert into student values('scofield',45,89,100)";
System.out.println("Inserting records into the table successfully!");
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
} finally {
if (stmt != null)
try {
stmt.close();
} catch (SQLException e) {
e.printStackTrace();
}
if (conn != null)
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
1. 插⼊数据之后,MySQL客户度查询结果如下
获取scofield的English成绩信息
JAVA代码:
sql;
import java.sql.*;
public class MysqlTest2 {
static final String DRIVER = "sql.jdbc.Driver";
static final String DB = "jdbc:mysql://localhost/test?useUnicode=true&characterEncoding=utf-8&useSSL=false"; static final String USER = "root";
static final String PASSWD = "0822";
public static void main(String[] args) {
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;
try {
Class.forName(DRIVER);
System.out.println("Connecting to a ");
conn = Connection(DB, USER, PASSWD);
stmt = ateStatement();
String sql = "select name,English from student where name='scofield' ";
rs = uteQuery(sql);
System.out.println("name" + "\t\t" + "English");
while (rs.next()) {
System.out.String(1) + "\t\t");
System.out.Int(2));
}
} catch (ClassNotFoundException e) {

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。