首先编写属于他的javaBean,一个字段一个属性 要继承
Writable, DBWritable两个接口
package com.zhan.mapreduce.db.read;
import org.apache.hadoop.io.Writable;
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;
/**
* @Classname GoodBean
* @Description MrZhan
* @Date 2021/10/10 12:33
* @Created by 1
*/
public class GoodBean implements Writable, DBWritable {
private long goodsId;//商品ID
private String goodsSn;//商品编号
private String goodsName;//商品名称
private double marketPrice;//市场价
private double shopPrice;//门店价
private long saleNum;//总销售量
public GoodBean() {
}
public GoodBean(long goodsId, String goodsSn, String goodsName, double marketPrice, double shopPrice, long saleNum) {
this.goodsId = goodsId;
this.goodsSn = goodsSn;
this.goodsName = goodsName;
this.marketPrice = marketPrice;
this.shopPrice = shopPrice;
this.saleNum = saleNum;
}
public void set(long goodsId, String goodsSn, String goodsName, double marketPrice, double shopPrice, long saleNum) {
this.goodsId = goodsId;
this.goodsSn = goodsSn;
this.goodsName = goodsName;
this.marketPrice = marketPrice;
this.shopPrice = shopPrice;
this.saleNum = saleNum;
}
public long getGoodsId() {
return goodsId;
}
public void setGoodsId(long goodsId) {
this.goodsId = goodsId;
}
public String getGoodsSn() {
return goodsSn;
}
public void setGoodsSn(String goodsSn) {
this.goodsSn = goodsSn;
}
public String getGoodsName() {
return goodsName;
}
public void setGoodsName(String goodsName) {
this.goodsName = goodsName;
}
public double getMarketPrice() {
return marketPrice;
}
public void setMarketPrice(double marketPrice) {
this.marketPrice = marketPrice;
}
public double getShopPrice() {
return shopPrice;
}
public void setShopPrice(double shopPrice) {
this.shopPrice = shopPrice;
}
public long getSaleNum() {
return saleNum;
}
public void setSaleNum(long saleNum) {
this.saleNum = saleNum;
}
@Override
public String toString() {
return goodsId
+"\t"+ goodsSn
+"\t"+goodsName
+"\t"+marketPrice
+"\t"+shopPrice
+"\t"+saleNum;
}
//序列方法
@Override
public void write(DataOutput dataOutput) throws IOException {
dataOutput.writeLong(goodsId);
dataOutput.writeUTF(goodsSn);
dataOutput.writeUTF(goodsName);
dataOutput.writeDouble(marketPrice);
dataOutput.writeDouble(shopPrice);
dataOutput.writeLong(saleNum);
}
//反序列方法 //注意顺序
@Override
public void readFields(DataInput dataInput) throws IOException {
this.goodsId = dataInput.readLong();
this.goodsSn = dataInput.readUTF();
this.goodsName =dataInput.readUTF();
this.marketPrice =dataInput.readDouble();
this.shopPrice =dataInput.readDouble();
this.saleNum =dataInput.readLong();
}
//在PreparedStatement中设置对象的字段 写数据操作
@Override
public void write(PreparedStatement statement) throws SQLException {
statement.setLong(1,goodsId);
statement.setString(2,goodsSn);
statement.setString(3,goodsName);
statement.setDouble(4,marketPrice);
statement.setDouble(5,shopPrice);
statement.setLong(6,saleNum);
}
//从ResultSet读取查询的结果 赋值给对象属性 读数据库操作
@Override
public void readFields(ResultSet resultSet) throws SQLException {
this.goodsId = resultSet.getLong(1);
this.goodsSn = resultSet.getString(2);
this.goodsName =resultSet.getString(3);
this.marketPrice =resultSet.getDouble(4);
this.shopPrice =resultSet.getDouble(5);
this.saleNum =resultSet.getLong(6);
}
}
由于不需要用到reduce 所以直接编写mapper即可
package com.zhan.mapreduce.db.read;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper;
import java.io.IOException;
/**
* @Classname ReadDBMapper
* @Description MrZhan
* @Date 2021/10/10 13:31
* @Created by 1
*/
/*
DBInputFormat类用于从SQL表读取数据。底层一行一行读取表中的数据,返回<k,v>键值对。
其中k是LongWritable类型,表中数据的记录行号,从0开始;
其中v是DBWritable类型,表示该行数据对应的对象类型。
*/
public class ReadDBMapper extends Mapper<LongWritable,GoodBean,LongWritable, Text> {
// LongWritable outKey = new LongWritable();
Text outValue = new Text();
@Override
protected void map(LongWritable key, GoodBean value, Context context) throws IOException, InterruptedException {
outValue.set(value.toString());
context.write(key,outValue);
}
}
最后编写驱动类
package com.zhan.mapreduce.db.read;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.lib.db.DBConfiguration;
import org.apache.hadoop.mapreduce.lib.db.DBInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
import java.io.IOException;
/**
* @Classname ReadDBDriver
* @Description MrZhan
* @Date 2021/10/10 13:39
* @Created by 1
*/
public class ReadDBDriver {
public static void main(String[] args) throws IOException, ClassNotFoundException, InterruptedException {
//配置文件对象类
Configuration conf = new Configuration();
//todo 配置当前1作业需要的JDBC信息
DBConfiguration.configureDB(
conf,
"com.mysql.jdbc.Driver",
"jdbc:mysql:///itcast_shop",
"root",
"root"
);
//创建作业的job类
Job job = Job.getInstance(conf,ReadDBDriver.class.getSimpleName());
//设置本次mr程序的驱动类
job.setJarByClass(ReadDBDriver.class);
//设置mapper类
job.setMapperClass(ReadDBMapper.class);
//设置程序最终输出的key 和 value类型
job.setOutputKeyClass(LongWritable.class);
job.setOutputValueClass(Text.class);
//todo 本需求不需要reduce阶段 所以要把reducetask的个数设置为0
job.setNumReduceTasks(0);
//todo 设置输入组件
job.setInputFormatClass(DBInputFormat.class);
//todo 添加读取数据库相关参数
DBInputFormat.setInput(
job,
GoodBean.class,
"select goodsId,goodsSn,goodsName,marketPrice,shopPrice,saleNum from itheima_goods",
"select COUNT(goodsId) from itheima_goods"
);
FileOutputFormat.setOutputPath(job,new Path("D://mysqlOut"));
boolean b = job.waitForCompletion(true);
System.exit(b ? 0 : 1);
}
}