MapReduce 程序mysql JDBC驱动类找不到原因及学习hadoop写入数据到Mysql数据库的方法
报错 :ClassNotFoundException: com.mysql.jdbc.Driver
第二个方法:job2.addFileToClassPath(file)和DistributedCache.addFileToClassPath()
第三个方法:所有依赖三方包和MR程序打包到一个jar中,打包完接近180MB。jar包太大了。
第四个方法:依赖的第三方jar打包到lib中,本次任务不需要的jar包可以全删掉 (这里注意:工程的jar和集群jar版本不一致可能导致冲突)。可以运行,导出的lib也小。
conf.set(DBConfiguration.DRIVER_CLASS_PROPERTY,"com.mysql.jdbc.Driver");
conf.set(DBConfiguration.URL_PROPERTY,
"jdbc:mysql://x.x.x.x:3306/test");
conf.set(DBConfiguration.USERNAME_PROPERTY, "xxx");
conf.set(DBConfiguration.PASSWORD_PROPERTY, "xxxx"); //这三行代码是本机测试用的
// conf.set(DBConfiguration.URL_PROPERTY,
// "jdbc:mysql://localhost:3306/test");
// conf.set(DBConfiguration.PASSWORD_PROPERTY, "xxx");
Job job3 = Job.getInstance(conf,"step3:insertToMySqlDB");
job3.setJarByClass(ECMerchantMapReduceV2.class);
job3.setMapOutputKeyClass(DBOutputKey.class);
job3.setMapOutputValueClass(NullWritable.class);
job3.setMapperClass(ECMerchantThirdMap.class);
job3.setOutputFormatClass(DBOutputFormat.class);
job3.setNumReduceTasks(0);
job3.addFileToClassPath(new Path("/xxxx/mysql-connector-java-5.0.3-bin.jar"));
DBOutputFormat.setOutput(job3, "yourtablename", "column1","column2","column3","column4");
FileInputFormat.addInputPath(job3, new Path(otherArgs[3]));
if(!job3.waitForCompletion(true)) return ;
public static class DBOutputKey implements WritableComparable<DBOutputKey>,DBWritable {
private String city = "";
private String ec="";
private String account="";
private Date date2=null;
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
public DBOutputKey() {
}
public DBOutputKey(String city, String ec, String account, Date date2) {
super();
this.city = city;
this.ec = ec;
this.account = account;
this.date2 = date2;
}
public void set(String city, String ec, String account, Date date2) {
this.city = city;
this.ec = ec;
this.account = account;
this.date2 = date2;
}
@Override
public void readFields(DataInput in) throws IOException {
this.city = in.readUTF();
this.ec = in.readUTF();
this.account = in.readUTF();
try {
this.date2 = new Date(format.parse(in.readUTF()).getTime());
} catch (ParseException e) {
e.printStackTrace();
}
}
@Override
public void write(DataOutput out) throws IOException {
out.writeUTF(city);
out.writeUTF(ec);
out.writeUTF(account);
out.writeUTF(date2.toString());
}
@Override
public int compareTo(DBOutputKey other) {
if (this.city.compareTo(other.city) != 0) {
return this.city.compareTo(other.city);
} else if (!this.ec .equals(other.ec) ) {
return ec .compareTo(other.ec);
} else if (!this.account.equals(other.account)) {
return account.compareTo(other.account);
}else {
return 0;
}
}
@Override
public String toString() {
return ToStringBuilder.reflectionToString(this);
}
//下面这2个方法是DBWritable接口的方法
@Override
public void write(PreparedStatement statement) throws SQLException {
statement.setString(1, city);
statement.setString(2, ec);
statement.setString(3, account);
statement.setDate(4, date2);
}
@Override
public void readFields(ResultSet resultSet) throws SQLException {
this.city=resultSet.getString(1);
this.ec=resultSet.getString(2);
this.account=resultSet.getString(3);
this.date2=resultSet.getDate(4);
}
}
注意:
1.DBOutputFormat写入数据库,是按照key输出的,没有value,key要实现WritableComparable和DBWritable接口;
2.数据库表字段有个为Sql.date类型private Date date2=null,readFields(DataInput in)和write(DataOutput out)输入输出流不支持Sql.Date类型,通过文中红色代码部分转换格式,即可使Hadoop序列化的时候支持sql.Date数据类型。
public static class ECMerchantThirdMap extends Mapper<LongWritable, Text, DBOutputKey, NullWritable> {
DBOutputKey dbKey=new DBOutputKey();
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
String date;
@Override
protected void setup(Mapper<LongWritable, Text, DBOutputKey, NullWritable>.Context context)
throws IOException, InterruptedException {
date=context.getConfiguration().get("eradiusDate");
}
public void map(LongWritable ikey, Text ivalue, Context context) throws IOException, InterruptedException {
String[] strs = ivalue.toString().split("\\s+");
if(strs.length!=3) return;
Date date2=null;
try {
date2 = new Date(format.parse(date).getTime());
} catch (ParseException e) {
e.printStackTrace();
}
dbKey.set(strs[0], strs[1], strs[2], date2);
context.write(dbKey, NullWritable.get());
}
}
Map类直接输出key就可以,不需要reduce类,可以设置reduce数量为0;
MapReduce 程序mysql JDBC驱动类找不到原因及学习hadoop写入数据到Mysql数据库的方法的更多相关文章
- MySQL JDBC驱动版本与MySQL数据库版本对应关系
前言:前段时间发现在家使用和公司一样的mysql jdbc驱动版本发生了异常,原因:家里mysql数据库版本与公司不一致导致.查询了相关资料,发现mysql jdbc驱动版本与mysql数据库版本有一 ...
- mysql的驱动类com.mysql.jdbc.Driver过时了,需要用com.mysql.cj.jdbc.Driver代替
springboot项目整合mybatis,配置文件如下: server: port: 8081 mybatis: config-location: classpath:mybatis/mybatis ...
- mysql——jdbc驱动下载&连接mysql例子
mysql-connector-java-5.1.46.zip[解压后里面jar文件就是所需要的] https://dev.mysql.com/get/Downloads/Connector-J/my ...
- 一个MySQL JDBC驱动bug引起的血案
1.1 问题背景 公司是做电商系统的,整个系统搭建在华为云上.系统设计的时候,考虑到后续的用户和订单数量比较大,需要使用一些大数据库的组件.关系型数据库这块,考虑到后续数据量的快速增长,不是 ...
- java web工程 数据库操作报驱动类找不到的错误
这几天在进行数据库的操作,写好数据库操作类后,用测试类测试成功通过,但是部署到tomcat后,从页面访问就会报异常. 最后终于发现是tomcat使用了连接池的数据连接方式. 解决方法是把jdbc ja ...
- VMware 克隆linux后找不到eth0(学习hadoop,所以想快速搭建一个集群)
发生情况: 由于在学习hadoop,所以想快速搭建一个集群出来.所以直接在windows操作系统上用VMware安装了CentOS操作系统,配置好hadoop开发环境后,采用克隆功能,直接克 ...
- mysql jdbc驱动与java 版本对应关系
当使用某些密码套件时,Connector/J5.1需要JRE 1.8.x才能使用SSL/TLS连接到MySQL 5.6,5.7和8.0.
- MySQL JDBC驱动下载
下载地址:https://pan.baidu.com/s/1VLNaV_rz2P1jMtYrjJydiQ
- windows各种程序中文显示乱码又找不到原因时
我电脑上的各种程序,如xshell,Navicat for MySQL都不正常显示中文,该软件的编码,utf-8,gbk,gb2312来回切换好几回,没一次正常,最好解决办法如下 进入控制 ...
随机推荐
- SpringBoot第十七篇:定时任务
作者:追梦1819 原文:https://www.cnblogs.com/yanfei1819/p/11076555.html 版权声明:本文为博主原创文章,转载请附上博文链接! 引言 相信大家对 ...
- 集合类源码(三)Collection之List(CopyOnWriteArrayList, Stack)
CopyOnWriteArrayList 功能 全名 public class CopyOnWriteArrayList<E> implements List<E>, Rand ...
- CyclicBarrier一组线程相互等待
/** * CyclicBarrier 一组线程相互等待 */ public class Beer { public static void main(String[] args) { final ; ...
- fiverr无法注册的解决办法,fiverr注册教程
转载 https://www.wok99.com/450.html
- 在eclipse中,用maven创建一个web项目工程
1.在eclipse中用maven创建项目,右键new>>Maven Project 2.点击next继续 3.点击next继续,选择maven-archetype-webapp, 4.点 ...
- PyTorch 之 DataLoader
DataLoader DataLoader 是 PyTorch 中读取数据的一个重要接口,该接口定义在 dataloader.py 文件中,该接口的目的: 将自定义的 Dataset 根据 batch ...
- SprintBoot 实现上传下载
本人在 .NET 转JAVA 的路上 ,也在学习SpringBoot相关知识,这里记录一下在Springboot中实现文件上传下载的核心代码 package com.file.demo.springb ...
- aspose.cells导出Demo
/// <summary> /// 导出excel /// </summary> /// <param name="list"></par ...
- Golang中,Aes加解密
今天在用Golang解析php那边用Aes加密的一个key.网上大多是用base64将结果编码一下.而且用到了向量.我php 那边没有用到向量.所以golang这边也是要去掉的.参考网站的改了下.能够 ...
- 纯C语言实现线性链表
#include <stdio.h> #include <stdlib.h> typedef int ElemType; typedef struct LNode{ ElemT ...