使用MapReduce将HDFS数据导入Mysql

使用MapReduce将Mysql数据导入HDFS代码链接

将HDFS数据导入Mysql,代码示例

package com.zhen.mysqlToHDFS;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 org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.conf.Configured;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.NullWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.io.Writable;
import org.apache.hadoop.mapred.lib.db.DBWritable;
import org.apache.hadoop.mapreduce.Counter;
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.DBOutputFormat;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.input.TextInputFormat;
import org.apache.hadoop.util.Tool;
import org.apache.hadoop.util.ToolRunner;/*** @author FengZhen* 将hdfs数据导入mysql* 使用DBOutputFormat将HDFS路径下的结构化数据写入mysql中,结构化数据如下,第一列为key,后边三列为数据* 0    1    Enzo    180.66* 1    2    Din    170.666* */
public class DBOutputFormatApp extends Configured implements Tool{/*** JavaBean* 需要实现Hadoop序列化接口Writable以及与数据库交互时的序列化接口DBWritable* 官方API中解释如下:* public class DBInputFormat<T extends DBWritable>*   extends InputFormat<LongWritable, T> implements Configurable* 即Mapper的Key是LongWritable类型,不可改变;Value是继承自DBWritable接口的自定义JavaBean*/public static class BeanWritable implements Writable, DBWritable {private int id;private String name;private double height;public void readFields(ResultSet resultSet) throws SQLException {this.id = resultSet.getInt(1);this.name = resultSet.getString(2);this.height = resultSet.getDouble(3);}public void write(PreparedStatement preparedStatement) throws SQLException {preparedStatement.setInt(1, id);preparedStatement.setString(2, name);preparedStatement.setDouble(3, height);}public void readFields(DataInput dataInput) throws IOException {this.id = dataInput.readInt();this.name = dataInput.readUTF();this.height = dataInput.readDouble();}public void write(DataOutput dataOutput) throws IOException {dataOutput.writeInt(id);dataOutput.writeUTF(name);dataOutput.writeDouble(height);}public void set(int id,String name,double height){this.id = id;this.name = name;this.height = height;}@Overridepublic String toString() {return id + "\t" + name + "\t" + height;}}public static class DBOutputMapper extends Mapper<LongWritable, Text, NullWritable, BeanWritable>{private NullWritable outputKey;private BeanWritable outputValue;@Overrideprotected void setup(Mapper<LongWritable, Text, NullWritable, BeanWritable>.Context context)throws IOException, InterruptedException {this.outputKey = NullWritable.get();this.outputValue = new BeanWritable();}@Overrideprotected void map(LongWritable key, Text value,Mapper<LongWritable, Text, NullWritable, BeanWritable>.Context context)throws IOException, InterruptedException {//插入数据库成功的计数器final Counter successCounter = context.getCounter("exec", "successfully");//插入数据库失败的计数器final Counter faildCounter = context.getCounter("exec", "faild");//解析结构化数据String[] fields = value.toString().split("\t");//DBOutputFormatApp这个MapReduce应用导出的数据包含long类型的key,所以忽略key从1开始if (fields.length > 3) {int id = Integer.parseInt(fields[1]);String name = fields[2];double height = Double.parseDouble(fields[3]);this.outputValue.set(id, name, height);context.write(outputKey, outputValue);//如果插入数据库成功则递增1,表示成功计数successCounter.increment(1L);}else{//如果插入数据库失败则递增1,表示失败计数faildCounter.increment(1L);}}}/*** 输出的key必须是继承自DBWritable的类型,DBOutputFormat要求输出的key必须是DBWritable类型* */public static class DBOutputReducer extends Reducer<NullWritable, BeanWritable, BeanWritable, NullWritable>{@Overrideprotected void reduce(NullWritable key, Iterable<BeanWritable> values,Reducer<NullWritable, BeanWritable, BeanWritable, NullWritable>.Context context)throws IOException, InterruptedException {for (BeanWritable beanWritable : values) {context.write(beanWritable, key);}}}public int run(String[] arg0) throws Exception {Configuration configuration = getConf();//在创建Configuration的时候紧接着配置数据库连接信息DBConfiguration.configureDB(configuration, "com.mysql.jdbc.Driver", "jdbc:mysql://localhost:3306/hadoop", "root", "123qwe");Job job = Job.getInstance(configuration, DBOutputFormatApp.class.getSimpleName());job.setJarByClass(DBOutputFormatApp.class);job.setMapperClass(DBOutputMapper.class);job.setMapOutputKeyClass(NullWritable.class);job.setMapOutputValueClass(BeanWritable.class);job.setReducerClass(DBOutputReducer.class);job.setOutputFormatClass(DBOutputFormat.class);job.setOutputKeyClass(BeanWritable.class);job.setOutputValueClass(NullWritable.class);job.setInputFormatClass(TextInputFormat.class);FileInputFormat.setInputPaths(job, arg0[0]);//配置当前作业输出到数据库表、字段信息DBOutputFormat.setOutput(job, "people", new String[]{"id","name","height"});return job.waitForCompletion(true)?0:1;}public static int createJob(String[] args){Configuration conf = new Configuration();conf.set("dfs.datanode.socket.write.timeout", "7200000");conf.set("mapreduce.input.fileinputformat.split.minsize", "268435456");conf.set("mapreduce.input.fileinputformat.split.maxsize", "536870912");int status = 0;try {status = ToolRunner.run(conf,new DBOutputFormatApp(), args);} catch (Exception e) {e.printStackTrace();}return status;}public static void main(String[] args) {args = new String[]{"/user/hadoop/mapreduce/mysqlToHdfs/people"};int status = createJob(args);System.exit(status);}}

打成jar包,放在服务器上,执行hadoop jar命令

hadoop jar /Users/FengZhen/Desktop/Hadoop/other/mapreduce_jar/HDFSToMysql.jar com.zhen.mysqlToHDFS.DBOutputFormatApp

任务结束后mysql表中即可发现数据已经有了。

转载于:https://www.cnblogs.com/EnzoDin/p/8429992.html

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/267617.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

html多行文本框下拉,html基础-表单控件、密码框、单选按钮、复选框、多行文本框、下拉列表、按钮(提交、图片、重置)...

表单的介绍(将前端页面表单的值发送给后台&#xff0c;后台通过表单中name属性取值)可以获取客户端的信息(数据)&#xff0c;表单有各种各样的控件&#xff0c;输入框&#xff0c;复选框 按钮等表单的功能&#xff1a;交互功能表单的工作原理&#xff1a;浏览有表单的页面&…

Lync Server 2010的部署系列_第七章 部署边缘服务器(上)

一、配置边缘支持的内部DNS记录 1) 登录DC.Gianthard.com&#xff08;192.168.1.11&#xff09;。在相应的 DNS 服务器上&#xff0c;依次单击“开始”、“控制面板”、“管理工具”&#xff0c;然后单击“DNS”。 2) 在 SIP 域的控制台树中&#xff0c;展开“正向查找区域”&a…

iOS扩大按钮的点击范围

// 重写此方法将按钮的点击范围扩大 - (BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event {CGRect bounds self.bounds;// 扩大点击区域bounds CGRectInset(bounds, -20, -20);// 若点击的点在新的bounds里面。就返回yesreturn CGRectContainsPoint(bounds, poin…

html5 txt文件上传,JavaScript html5利用FileReader实现上传功能

本文实例为大家分享了H5利用FileReader上传文件的具体代码&#xff0c;供大家参考&#xff0c;具体内容如下1. Html部分文件上传演练Browse...2. JS部分var result document.getElementById("result");var input document.getElementById("file_input");…

判断标签是否出界,重新设置样式

//样式重置&#xff0c;因为会获取到上次设置的样式 $(#releaseData).css({ "height": "auto", "bottom": "auto" }); //获取底部位置 var bottom$("#releaseData").css("bottom"); if (bottom.toString().indexO…

一起谈.NET技术,ASP.NET 请求处理流程

HTTP处理流程图 以上流程的一些概念解释&#xff1a; 1.http.sys 是一个位于Win2003和WinXP SP2中的操作系统核心组件&#xff0c;能够让任何应用程序通过它提供的接口&#xff0c;以http协议进行信息通讯。 温馨提示&#xff1a;如果用户不慎删除了该驱动文件&#xff0c;不用…

链接在HTML的英文,英文:A链接标记ie下会自动补全href_HTML/Xhtml_网页制作

英文:A链接标记ie下会自动补全href.Whilst working on the Ajax Link Tracker and MapSurface I have come across an inconsistency in how the href attribute is retrieved using DOM Scripting.The href attribute is different to other element attributes in that the v…

python实现文件加密

前言&#xff1a; 想实现批量文件加密&#xff0c;可惜批量。展时没有思路 0x1 没有加密前的图片 加密后&#xff01;&#xff01;&#xff01; &#xff01;&#xff01;&#xff01;打不开了 0x02: 代码 import hashlibdef get_sha1(f):xdopen(E:/1.txt,rb).read() #以读二进…

教徒计划出品:升级ESXI41-ESXI5

这个文档是教徒计划“教徒第一期”学员做的升级ESX41到ESXI5的资料&#xff0c;以后教徒计划学员做的资料共享时&#xff0c;我都会打上“教徒计划出品”字样&#xff0c;这样有别于“现任明教教主”出品。这样能够确认是谁主导做的这个事情。“教徒计划”这个平台能够给安全CC…

html是以一种通用的方法来,c++ 有一种通用的方法来使函数模板适应为多态函数对象吗?...

我有一些功能模板,例如template void foo(T);template void bar(T);// others我需要将每一个传递给一种算法,它将被称为各种类型的算法.template void some_algorithm(F f){// call f with argument of type int// call f with argument of type SomeClass// etc.}我不能传递我…

百度2011大会见闻:百度开始推出耀主页

9月2号是百度大会的日子&#xff0c;之前通过51CTO注册&#xff0c;获得了参会资格&#xff0c;感谢51CTO带来的机会&#xff0c;可以有幸到现场观看到百度总裁李彦宏的精彩演讲。<?xml:namespace prefix o ns "urn:schemas-microsoft-com:office:office" />…

asp.net core中使用log4net

和之前的ASP.NET MVC中的使用LOG4NET的方法有些不同&#xff0c;这里先记录一下&#xff0c;使用步骤如下 &#xff1a; 1. 建立 ASP.NET CORE项目中&#xff0c;NUGET中搜索log4net后下载安装 2. 根目录建立 log4net.config文件&#xff0c;内容如下&#xff1a; <?xml ve…

2013电大计算机应用基础试题及答案,[2017年电大]电大2013年计算机应用基础试题及答案[呕心沥血整理].doc...

[2017年电大]电大2013年计算机应用基础试题及答案[呕心沥血整理]计算机应用基础复习资料一、单选题1、当前的计算机一般被认为是第四代计算机&#xff0c;它所采用的逻辑元件是______。答案&#xff1a; DA&#xff1a;晶体管 B&#xff1a;集成电路 C&#xff1a;电子管 D&…

由于远程桌面服务当前正忙|VDI无法连接

VDI 无法连接&#xff0c;“由于远程桌面服务当前正忙”。Win2008 R2 DC RDVH RDCB RDWA &#xff0c;然后为用户demo配置了个人虚拟机&#xff08;XP SP3&#xff09;&#xff0c;当用户登录rdwa.abc.com/rdweb &#xff0c;访问“我的桌面”时一段时间后报错 “由于远程桌…

寒假作业01

1.对大一上学期的总结&#xff1a; 用一句很经典的话来说&#xff0c;大学生活与高中生活不同就在于高中忙着做作业是为了有时间去做其他的事&#xff0c;而在大学&#xff0c;得忙着做完其他事情才有时间去做作业。因为不仅要处理好学习内的事情&#xff0c;还要花心思在课余活…

单招计算机英语面试口语,英语面试口语对话技巧:教育背景

2021年高职单招升学一对一咨询高职单招袁老师:16623303056(微信)英语面试口语对话技巧:教育背景一、常见的英语面试对话1a&#xff1a;can you read and write english and german?你能用英语和德语读写吗&#xff1f;b&#xff1a;no, im proficient in both written and spo…

C代码中的命名方式总结和改进

宏全部使用大写字母--------宏大写结构体名字全部使用typedef&#xff0c;typedef之后的名字为大写-------结构体别名大写函数名全部使用小写字母&#xff0c;单词之间使用下划线分割-------函数名小写&#xff0c;单词之间下划线&#xff08;1&#xff09;函数名第一个单词全小…

linux 安装软件的几种方法

一、 解析Linux应用软件安装包&#xff1a;通常Linux应用软件的安装包有三种&#xff1a;1&#xff09; tar包&#xff0c;如software-1.2.3-1.tar.gz。它是使用UNIX系统的打包工具tar打包的。2&#xff09; rpm包&#xff0c;如software-1.2.3-1.i386.rpm。它是Redhat Linux提…

对计算机网络用户而言 掌握网络,计算机网络的特点

1、可靠性在一个网络系统中&#xff0c;当一台计算机出现故障时&#xff0c;可立即由系统中的另一台计算机来代替其完成所承担的任务。同样&#xff0c;当网络的一条链路出了故障时可选择其它的通信链路进行连接。2、高效性计算机网络系统摆脱了中心计算机控制结构数据传输的局…

Bootstrap-CSS:表格

ylbtech-Bootstrap-CSS&#xff1a;表格1.返回顶部 1、Bootstrap 表格 Bootstrap 提供了一个清晰的创建表格的布局。下表列出了 Bootstrap 支持的一些表格元素&#xff1a; 标签描述<table>为表格添加基础样式。<thead>表格标题行的容器元素&#xff08;<tr>…