使用DBWritable完成同mysql交互
来源:互联网 发布:阿里云备案文件 编辑:程序博客网 时间:2024/06/06 01:10
1.准备数据库
create database big4 ;
use big4 ;
create table words(id int primary key auto_increment , name varchar(20) , txt varchar(255));
insert into words(name,txt) values('tomas','hello world tom');
insert into words(txt) values('hello tom world');
insert into words(txt) values('world hello tom');
insert into words(txt) values('world tom hello');
2.编写hadoop MyDBWritable.
package com.it18zhang.hdfs.mr.mysql;
create database big4 ;
use big4 ;
create table words(id int primary key auto_increment , name varchar(20) , txt varchar(255));
insert into words(name,txt) values('tomas','hello world tom');
insert into words(txt) values('hello tom world');
insert into words(txt) values('world hello tom');
insert into words(txt) values('world tom hello');
2.编写hadoop MyDBWritable.
package com.it18zhang.hdfs.mr.mysql;
import org.apache.hadoop.io.Writable;
import org.apache.hadoop.mapreduce.lib.db.DBWritable;
import org.apache.hadoop.mapreduce.lib.db.DBWritable;
import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.io.DataOutput;
import java.io.IOException;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
/**
* MyDBWritable
*/
public class MyDBWritable implements DBWritable,Writable {
private int id ;
private String name ;
private String txt ;
* MyDBWritable
*/
public class MyDBWritable implements DBWritable,Writable {
private int id ;
private String name ;
private String txt ;
public int getId() {
return id;
}
return id;
}
public void setId(int id) {
this.id = id;
}
this.id = id;
}
public String getName() {
return name;
}
return name;
}
public void setName(String name) {
this.name = name;
}
this.name = name;
}
public String getTxt() {
return txt;
}
return txt;
}
public void setTxt(String txt) {
this.txt = txt;
}
this.txt = txt;
}
public void write(DataOutput out) throws IOException {
out.writeInt(id);
out.writeUTF(name);
out.writeUTF(txt);
}
out.writeInt(id);
out.writeUTF(name);
out.writeUTF(txt);
}
public void readFields(DataInput in) throws IOException {
id = in.readInt();
name = in.readUTF();
txt = in.readUTF();
}
id = in.readInt();
name = in.readUTF();
txt = in.readUTF();
}
/**
* 写入db
*/
public void write(PreparedStatement ppst) throws SQLException {
ppst.setInt(1,id);
ppst.setString(2,name);
ppst.setString(3,txt);
}
* 写入db
*/
public void write(PreparedStatement ppst) throws SQLException {
ppst.setInt(1,id);
ppst.setString(2,name);
ppst.setString(3,txt);
}
/**
* 从db读取
*/
public void readFields(ResultSet rs) throws SQLException {
id = rs.getInt(1);
name = rs.getString(2);
txt = rs.getString(3);
}
}
* 从db读取
*/
public void readFields(ResultSet rs) throws SQLException {
id = rs.getInt(1);
name = rs.getString(2);
txt = rs.getString(3);
}
}
3.WcMapper
public class WCMapper extends Mapper<LongWritable,MyDBWritable,Text,IntWritable> {
public class WCMapper extends Mapper<LongWritable,MyDBWritable,Text,IntWritable> {
protected void map(LongWritable key, MyDBWritable value, Context context) throws IOException, InterruptedException {
System.out.println(key);
String line = value.getTxt();
System.out.println(value.getId() + "," + value.getName());
String[] arr = line.split(" ");
for(String s : arr){
context.write(new Text(s),new IntWritable(1));
}
}
}
System.out.println(key);
String line = value.getTxt();
System.out.println(value.getId() + "," + value.getName());
String[] arr = line.split(" ");
for(String s : arr){
context.write(new Text(s),new IntWritable(1));
}
}
}
4.WCReducer
protected void reduce(Text key, Iterable<IntWritable> values, Context context) throws IOException, InterruptedException {
int count = 0 ;
for(IntWritable w : values){
count = count + w.get() ;
}
context.write(key,new IntWritable(count));
}
protected void reduce(Text key, Iterable<IntWritable> values, Context context) throws IOException, InterruptedException {
int count = 0 ;
for(IntWritable w : values){
count = count + w.get() ;
}
context.write(key,new IntWritable(count));
}
5.WCApp
public static void main(String[] args) throws Exception {
Configuration conf = new Configuration();
Job job = Job.getInstance(conf);
public static void main(String[] args) throws Exception {
Configuration conf = new Configuration();
Job job = Job.getInstance(conf);
//设置job的各种属性
job.setJobName("MySQLApp"); //作业名称
job.setJarByClass(WCApp.class); //搜索类
job.setJobName("MySQLApp"); //作业名称
job.setJarByClass(WCApp.class); //搜索类
//配置数据库信息
String driverclass = "com.mysql.jdbc.Driver" ;
String url = "jdbc:mysql://localhost:3306/big4" ;
String username= "root" ;
String password = "root" ;
//设置数据库配置
DBConfiguration.configureDB(job.getConfiguration(),driverclass,url,username,password);
//设置数据输入内容
DBInputFormat.setInput(job,MyDBWritable.class,"select id,name,txt from words","select count(*) from words");
String driverclass = "com.mysql.jdbc.Driver" ;
String url = "jdbc:mysql://localhost:3306/big4" ;
String username= "root" ;
String password = "root" ;
//设置数据库配置
DBConfiguration.configureDB(job.getConfiguration(),driverclass,url,username,password);
//设置数据输入内容
DBInputFormat.setInput(job,MyDBWritable.class,"select id,name,txt from words","select count(*) from words");
//设置输出路径
FileOutputFormat.setOutputPath(job,new Path("d:/mr/sql/out"));
FileOutputFormat.setOutputPath(job,new Path("d:/mr/sql/out"));
//设置分区类
job.setMapperClass(WCMapper.class); //mapper类
job.setReducerClass(WCReducer.class); //reducer类
job.setMapperClass(WCMapper.class); //mapper类
job.setReducerClass(WCReducer.class); //reducer类
job.setNumReduceTasks(3); //reduce个数
job.setMapOutputKeyClass(Text.class); //
job.setMapOutputValueClass(IntWritable.class); //
job.setMapOutputValueClass(IntWritable.class); //
job.setOutputKeyClass(Text.class);
job.setOutputValueClass(IntWritable.class); //
job.setOutputValueClass(IntWritable.class); //
job.waitForCompletion(true);
}
}
6.pom.xml增加mysql驱动
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.17</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.17</version>
</dependency>
7.将mr的统计结果写入mysql数据库
a)准备表
create table stats(word varchar(50),c int);
b)设置App的DBOutputFormat类
com.WCApp
a)准备表
create table stats(word varchar(50),c int);
b)设置App的DBOutputFormat类
com.WCApp
0 0
- 使用DBWritable完成同mysql交互
- Echarts使用,使用springmvc完成数据交互
- 使用JSON-RPC-Java完成Ajax交互
- 使用JSON-RPC-Java完成Ajax交互
- 使用JSON-RPC-Java完成Ajax交互
- 使用mysql交互
- PHP4与MySQL交互使用
- Flex与服务器交互之二(使用HTTPService同服务器交互)
- Flex与服务器交互(使用RemoteObject+BlazeDS+TomCat同Java交互及开发环境配置)
- Flex与服务器交互之二(使用HTTPService同服务器交互)
- 实现可编辑单元格(使用Ajax完成后台交互)。
- MVC-UIView同数据交互
- ajax同后台交互demo
- Flex与服务器交互之四(使用RemoteObject +BlazeDS+TomCat同Java交互及开发环境配置)
- Flex与服务器交互之四(使用RemoteObject +BlazeDS+TomCat同Java交互及开发环境配置)
- Flex与服务器交互之三(使用WebService组件同服务器WebService通讯)
- Flex与服务器交互之三(使用WebService组件同服务器WebService通讯)
- 从零开始,使用Cocos2d HTML5完成一个游戏——第二步:鼠标交互
- JStorm源码分析小贴士(一)进一步了解 AsyncLoopThread(草稿)
- 趋势引领新发展,电商在线客服系统的未来
- 屏幕获取程序
- pta 天梯地图 (Dijkstra)
- 斐波那契数列的Java表示
- 使用DBWritable完成同mysql交互
- beego_Excel 文件上传
- 退出登陆
- P2P金融借贷项目 前台+后台+数据库源码+移动端后台
- tensorflow1.0 windows下安装
- js的基本数据类型有哪些? ECMAScript中有5中简单数据类型(也称为基本数据类型): Undefined、Null、Boolean、Number和String。还有1中复杂的数据类型————
- 朴灵:基于MongoDB与NodeJS构建物联网系统
- Ioc和Di
- 2017.3.22 我的第一篇技术博客