采用MapReduce读取数据库的数据

采用MapReduce读取数据库的数据

数据库表

DROP TABLE IF EXISTS `user`;
CREATE TABLE `user`  (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `username` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
  `usersex` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
  `yw` int(11) NULL DEFAULT NULL,
  `sx` int(11) NULL DEFAULT NULL,
  `yy` int(11) NULL DEFAULT NULL,
  PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 5 CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Compact;

-- ----------------------------
-- Records of user
-- ----------------------------
INSERT INTO `user` VALUES (1, '小文', '男', 10, 20, 30);
INSERT INTO `user` VALUES (2, '李娜', '女', 100, 120, 130);
INSERT INTO `user` VALUES (3, '丽丽', '女', 90, 120, 120);
INSERT INTO `user` VALUES (4, '美娜', '女', 80, 90, 120);

SET FOREIGN_KEY_CHECKS = 1;

实体类

package com.hadoop.demo4;

import org.apache.hadoop.mapreduce.lib.db.DBWritable;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

/**
 * 读取数据库的数据
 */
public class User implements DBWritable {
    int id;
    String username;
    String usersex;
    int yw;//语文
    int sx;//数学
    int yy;//英语

    /**
     * 序列化
     * @param preparedStatement
     * @throws SQLException
     */
    public void write(PreparedStatement preparedStatement) throws SQLException {
        preparedStatement.setInt(1,this.id);
        preparedStatement.setString(2,this.username);
        preparedStatement.setString(3,this.usersex);
        preparedStatement.setInt(4,this.yw);
        preparedStatement.setInt(5,this.sx);
        preparedStatement.setInt(6,this.yy);

    }

    /**
     * 反序列化
     * @param resultSet
     * @throws SQLException
     */
    public void readFields(ResultSet resultSet) throws SQLException {
            this.id = resultSet.getInt(1);
            this.username = resultSet.getString(2);
            this.usersex = resultSet.getString(3);
            this.yw = resultSet.getInt(4);
            this.sx = resultSet.getInt(5);
            this.yy = resultSet.getInt(6);
    }

    @Override
    public String toString() {
        return this.id+"\t"+this.username+"\t"+this.usersex+"\t"+this.yw+"\t"+this.sx+"\t"+this.yy;
    }
}

MapReduce

package com.hadoop.demo4;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.NullWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.Reducer;
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;

public class UserInputMap extends Mapper<LongWritable,User, Text, NullWritable> {
    @Override
    protected void map(LongWritable key, User value, Context context) throws IOException, InterruptedException {
        // "1   小文  男   10  20  30"
        context.write(new Text(value.toString()),NullWritable.get());

    }
}

class UserInputReduce extends Reducer<Text, NullWritable,Text, NullWritable>{
    @Override
    protected void reduce(Text key, Iterable<NullWritable> values, Context context) throws IOException, InterruptedException {
        context.write(key,NullWritable.get());
    }
}
class UserInputTest{
    public static void main(String[] args) throws IOException, ClassNotFoundException, InterruptedException {
        Configuration conf = new Configuration();
        DBConfiguration.configureDB(conf,"com.mysql.jdbc.Driver",
                "jdbc:mysql://127.0.0.1:3306/hadoop","root","12345678");
        Job job = Job.getInstance(conf);
        //加载类
        job.setJarByClass(UserInputTest.class);
        job.setMapperClass(UserInputMap.class);
        job.setReducerClass(UserInputReduce.class);

        //设置实体类与数据库的映射
        DBInputFormat.setInput(job,User.class,"user",null,null
        ,"id","username","usersex","yw","sx","yy");
        //设置map的key输出的类型和value输出的类型
        job.setMapOutputKeyClass(Text.class);
        job.setMapOutputValueClass(NullWritable.class);
        //设置reduce的输出的key的类型和value输出的类型
        job.setOutputKeyClass(Text.class);
        job.setOutputValueClass(NullWritable.class);

        Path outpath = new Path("user");
        outpath.getFileSystem(conf).delete(outpath,true);
        FileOutputFormat.setOutputPath(job,outpath);

        boolean b = job.waitForCompletion(true);
        System.exit(b?0:1);
    }
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

java庞

你的鼓励就是我们最大的动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值