mapreduce 读mysql_通过mapreduce把mysql的数据读取到hdfs

package com.gong.mrmysql;

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.util.Iterator;

import org.apache.hadoop.filecache.DistributedCache;

import org.apache.hadoop.fs.FileSystem;

import org.apache.hadoop.fs.Path;

import org.apache.hadoop.io.LongWritable;

import org.apache.hadoop.io.Text;

import org.apache.hadoop.io.Writable;

import org.apache.hadoop.mapred.FileOutputFormat;

import org.apache.hadoop.mapred.JobClient;

import org.apache.hadoop.mapred.JobConf;

import org.apache.hadoop.mapred.MapReduceBase;

import org.apache.hadoop.mapred.Mapper;

import org.apache.hadoop.mapred.OutputCollector;

import org.apache.hadoop.mapred.Reducer;

import org.apache.hadoop.mapred.Reporter;

import org.apache.hadoop.mapred.lib.IdentityReducer;

import org.apache.hadoop.mapred.lib.db.DBConfiguration;

import org.apache.hadoop.mapred.lib.db.DBInputFormat;

import org.apache.hadoop.mapred.lib.db.DBOutputFormat;

import org.apache.hadoop.mapred.lib.db.DBWritable;/**

* Function: 测试 mr 与 mysql 的数据交互,此测试用例将一个表中的数据复制到另一张表中

* 实际当中,可能只需要从 mysql 读,或者写到 mysql 中。

* date: 2013-7-29 上午2:34:04

* @author june*/

public classMysql2Mr {//DROP TABLE IF EXISTS `hadoop`.`studentinfo`;//CREATE TABLE studentinfo (//id INTEGER NOT NULL PRIMARY KEY,//name VARCHAR(32) NOT NULL);

public static classStudentinfoRecord implements Writable, DBWritable {intid;

String name;//构造方法

publicStudentinfoRecord() { }//Writable接口是对数据流进行操作的,所以输入是DataInput类对象

public void readFields(DataInput in) throws IOException {this.id = in.readInt(); //输入流中的读取下一个整数,并返回

this.name = Text.readString(in);

}publicString toString() {return new String(this.id + " " + this.name);

}//DBWritable负责对数据库进行操作,所以输出格式是PreparedStatement//PreparedStatement接口继承并扩展了Statement接口,用来执行动态的SQL语句,即包含参数的SQL语句

@Overridepublic voidwrite(PreparedStatement stmt) throws SQLException {

stmt.setInt(1, this.id);

stmt.setString(2, this.name);

}//DBWritable负责对数据库进行操作,输入格式是ResultSet//ResultSet接口类似于一张数据表,用来暂时存放从数据库查询操作所获得的结果集

@Overridepublic voidreadFields(ResultSet result) throws SQLException {this.id = result.getInt(1);this.name = result.getString(2);

}//Writable接口是对数据流进行操作的,所以输出是DataOutput类对象

@Overridepublic void write(DataOutput out) throws IOException {out.writeInt(this.id);

Text.writeString(out, this.name);

}

}//记住此处是静态内部类,要不然你自己实现无参构造器,或者等着抛异常://Caused by: java.lang.NoSuchMethodException: DBInputMapper.()// http://stackoverflow.com/questions/7154125/custom-mapreduce-input-format-cant-find-constructor

//网上脑残式的转帖,没见到一个写对的。。。

public static classDBInputMapper extends MapReduceBase implements

Mapper{public voidmap(LongWritable key, StudentinfoRecord value,

OutputCollectorcollector, Reporter reporter) throws IOException {

collector.collect(new LongWritable(value.id), newText(value.toString()));

}

}public static classMyReducer extends MapReduceBase implements

Reducer{

@Overridepublic void reduce(LongWritable key, Iteratorvalues,

OutputCollectoroutput, Reporter reporter) throws IOException {

String[] splits= values.next().toString().split(" ");

StudentinfoRecord r= newStudentinfoRecord();

r.id= Integer.parseInt(splits[0]);

r.name= splits[1];

output.collect(r,newText(r.name));

}

}public static voidmain(String[] args) throws IOException {

JobConf conf= new JobConf(Mysql2Mr.class);

DistributedCache.addFileToClassPath(new Path("hdfs://192.168.241.13:9000/mysqlconnector/mysql-connector-java-5.1.38-bin.jar"), conf);

conf.setMapOutputKeyClass(LongWritable.class);

conf.setMapOutputValueClass(Text.class);

conf.setOutputKeyClass(LongWritable.class);

conf.setOutputValueClass(Text.class);//conf.setOutputFormat(DBOutputFormat.class);

conf.setInputFormat(DBInputFormat.class);//mysql to hdfs

conf.set("fs.defaultFS", "hdfs://192.168.241.13:9000");//在配置文件conf中指定所用的文件系统---HDFS

conf.setReducerClass(IdentityReducer.class);

Path outPath= new Path("hdfs://192.168.241.13:9000/student/out1");

FileSystem.get(conf).delete(outPath, true);

FileOutputFormat.setOutputPath(conf, outPath);

DBConfiguration.configureDB(conf,"com.mysql.jdbc.Driver", "jdbc:mysql://192.168.241.13:3306/mrtest","root", "543116");

String[] fields= { "id", "name"};//从 t 表读数据

DBInputFormat.setInput(conf, StudentinfoRecord.class, "t", null, "id", fields);//mapreduce 将数据输出到 t2 表//DBOutputFormat.setOutput(conf, "t2", "id", "name");//FileOutputFormat.setOutputPath(conf, new Path("hdfs://192.168.241.13:9000/student/out1"));

conf.setMapperClass(org.apache.hadoop.mapred.lib.IdentityMapper.class);

conf.setMapperClass(DBInputMapper.class);//conf.setReducerClass(MyReducer.class);

JobClient.runJob(conf);

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
HDFS(Hadoop分布式文件系统)是一种用于存储大规模数据的分布式文件系统。将HDFS中的数据导出至MySQL内可以通过以下步骤实现: 1. 数据提取:首先,需要从HDFS中提取数据。可以使用Hadoop的命令行工具或编MapReduce程序来读取HDFS中的数据。根据数据的格式和结构,选择合适的方法进行数据提取。 2. 数据转换:HDFS中的原始数据可能是以不同格式或结构存在的,需要将其转换成MySQL可以接受的格式。这包括对数据进行清洗、格式化、转换等操作,以保证数据的一致性和完整性。 3. 数据加载:将转换后的数据加载至MySQL中。可以使用MySQL提供的命令行工具、GUI工具或编脚本来实现数据加载。在加载过程中,需要创建相应的数据库、表和字段,并将数据插入到对应的表中。 4. 数据校验:加载完成后,需要对数据进行校验以确保数据的准确性和完整性。可以针对数据的一些关键字段或条件进行查询和比对,确保导入的数据HDFS中的原始数据一致。 5. 数据迁移:如果需要持续将HDFS中的数据导入MySQL中,可以编定时任务或使用工作流调度工具,定期执行数据导出的操作,以实现数据的自动迁移和同步。 总之,将HDFS中的数据导出至MySQL内需要经过数据提取、转换、加载、校验和迁移等步骤。这些步骤需要结合具体的数据特点和业务需求,选择合适的方法和工具,并确保数据的完整性和一致性。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值