先写一个自己的MyRecordWriter类 extends RecordWriter

package calllog;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.RecordWriter;
import org.apache.hadoop.mapreduce.TaskAttemptContext; public class IRecordWrite extends RecordWriter<Text, IntWritable>{ @Override
public void write(Text key, IntWritable value){ String driver = "com.mysql.jdbc.Driver";
String url = "jdbc:mysql://192.168.120.110:3306/calllog?characterEncoding=UTF-8";
String user = "root";
String password = "******";
System.out.println("开始写入数据");
Connection conn = null;
Statement statement = null; //数据处理
String string = key.toString();
String[] split = string.split("\t");
String zhujian = split[0]+"_"+split[1]+"_"+split[2]; try {
Class.forName(driver);
conn = DriverManager.getConnection(url, user, password);
conn.setAutoCommit(true);
statement = conn.createStatement();
//有则更新,无则插入
//INSERT INTO `tb_call` (`id_date_contact`, `id_date_dimension`, `id_contact`, `call_sum`, `call_duration_sum`) VALUES (?, ?, ?, ?, ?) ON DUPLICATE KEY UPDATE `id_date_contact` = ?;
String sql = "INSERT INTO mylog values ('"+zhujian+"','"+split[0]+"','"+split[1]+"','"+split[2]+"','"+value.toString()+"') ON DUPLICATE KEY UPDATE name_phone_time = '"+zhujian+"';";
System.out.println(sql);
statement.execute(sql);
System.err.println("---------插入成功!--------------------------");
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
try {
if (statement!=null) {
statement.close();
}
if (conn!=null) {
conn.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
@Override
public void close(TaskAttemptContext context) throws IOException, InterruptedException {
// TODO Auto-generated method stub }
}

 

再写一个自己的outputFormat extends OutputFormat

package calllog;
import java.io.IOException;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.JobContext;
import org.apache.hadoop.mapreduce.OutputCommitter;
import org.apache.hadoop.mapreduce.OutputFormat;
import org.apache.hadoop.mapreduce.RecordWriter;
import org.apache.hadoop.mapreduce.TaskAttemptContext; public class IOutputFormat extends OutputFormat<Text, IntWritable>{ @Override
public RecordWriter<Text, IntWritable> getRecordWriter(TaskAttemptContext context)
throws IOException, InterruptedException {
return new IRecordWrite();
} @Override
public void checkOutputSpecs(JobContext context) throws IOException, InterruptedException { } @Override
public OutputCommitter getOutputCommitter(TaskAttemptContext context) throws IOException, InterruptedException {
return (new org.apache.hadoop.mapreduce.lib.output.NullOutputFormat<Text, IntWritable>())
.getOutputCommitter(context);
} }

  最后再driver端     自定义自己的输出类

public class Idriver {

	public static void main(String[] args) throws Exception {

		Configuration conf = new Configuration();
Job job = Job.getInstance(conf); job.setJarByClass(Idriver.class); // 3 关 联 map
job.setMapperClass(Imap.class);
job.setReducerClass(Ireduce.class); job.setMapOutputKeyClass(Text.class);
job.setMapOutputValueClass(IntWritable.class); //分区
//job.setPartitionerClass(IPartitioner.class);
//job.setNumReduceTasks(4); // 4 设置最终输出数据类型
//job.setOutputKeyClass(Text.class);
//job.setOutputValueClass(IntWritable.class);
job.setOutputFormatClass(IOutputFormat.class); // 5 设置输入输出路径
FileInputFormat.setInputPaths(job, new Path(args[0]));
//FileOutputFormat.setOutputPath(job, new Path(args[1])); // 8 提交
boolean result = job.waitForCompletion(true);
System.exit(result ? 0 : 1);
} }

  

 

读取hbase数据到mysql的更多相关文章

  1. 关于mapreducer 读取hbase数据 存入mysql的实现过程

    mapreducer编程模型是一种八股文的代码逻辑,就以用户行为分析求流存率的作为例子 1.map端来说:必须继承hadoop规定好的mapper类:在读取hbase数据时,已经有现成的接口 Tabl ...

  2. 使用MapReduce读取HBase数据存储到MySQL

    Mapper读取HBase数据 package MapReduce; import org.apache.hadoop.hbase.Cell; import org.apache.hadoop.hba ...

  3. SparkSQL读取HBase数据

    这里的SparkSQL是指整合了Hive的spark-sql cli(关于SparkSQL和Hive的整合,见文章后面的参考阅读). 本质上就是通过Hive访问HBase表,具体就是通过hive-hb ...

  4. java的poi技术读取Excel数据到MySQL

    这篇blog是介绍java中的poi技术读取Excel数据,然后保存到MySQL数据中. 你也可以在 : java的poi技术读取和导入Excel了解到写入Excel的方法信息 使用JXL技术可以在 ...

  5. jxl读写excel, poi读写excel,word, 读取Excel数据到MySQL

    这篇blog是介绍: 1. java中的poi技术读取Excel数据,然后保存到MySQL数据中. 2. jxl读写excel 你也可以在 : java的poi技术读取和导入Excel了解到写入Exc ...

  6. Spark 读取HBase数据

    Spark1.6.2 读取 HBase 1.2.3 //hbase-common-1.2.3.jar //hbase-protocol-1.2.3.jar //hbase-server-1.2.3.j ...

  7. php+phpspreadsheet读取Excel数据存入mysql

    先生成Excel模板,然后导入Excel数据到mysql,每条数据对应图片上传到阿里云 <?php /** * Created by PhpStorm. * User: Administrato ...

  8. 通过读取excel数据和mysql数据库数据做对比(二)-代码编写测试

    通过上一步,环境已搭建好了. 下面开始实战, 首先,编写链接mysql的函数conn_sql.py import pymysql def sql_conn(u,pwd,h,db): conn=pymy ...

  9. spark读取hbase数据

    def main(args: Array[String]): Unit = { val hConf = HBaseConfiguration.create(); hConf.set("hba ...

随机推荐

  1. python入门10 循环语句

    两种循环: 1 for in 2 while #coding:utf-8 #/usr/bin/python """ 2018-11-03 dinghanhua 循环语句 ...

  2. Fiddler实现IOS手机抓取https报文

    如何设置代理访问内网进而抓取手机的Https报文进行分析定位. 准备工作: 1.PC上连接好VPN 2.管理员方式打开Fiddler工具 开搞: 一.设置Fiddler 1.打开Tools->O ...

  3. csu 1947 三分

    题意: 长者对小明施加了膜法,使得小明每天起床就像马丁的早晨一样. 今天小明早上6点40醒来后发现自己变成了一名高中生,这时马上就要做早操了,小明连忙爬起来 他看到操场密密麻麻的人,突然灵光一闪想到了 ...

  4. POJ 1416 Shredding Company【dfs入门】

    题目传送门:http://poj.org/problem?id=1416 Shredding Company Time Limit: 1000MS   Memory Limit: 10000K Tot ...

  5. servlet 与 tomcat版本不匹配的问题

    严重: Failed to process JAR found at URL [/StudentLeave] for ServletContainerInitializers for context ...

  6. 【luogu P2397 yyy loves Maths VI (mode) 】 题解

    题目链接:https://www.luogu.org/problemnew/show/P2397 卡空间. 对于众数出现次数 > n/2 我们考虑rand. 每次正确的概率为1/2,五个测试点, ...

  7. 【题解】洛谷P1445 [Violet]樱花 (推导+约数和)

    洛谷P1445:https://www.luogu.org/problemnew/show/P1445 推导过程 1/x+1/y=1/n! 设y=n!+k(k∈N∗) 1/x​+1/(n!+k)​=1 ...

  8. 【TOJ 1743】集合运算(set并、交、差集)

    Description 给定两个集合A和B的所有元素,计算它们的交.并.差集. Input 输入数据有多组,第一行为数据的组数T,接下来有2T行,每组数据占2行,每行有若干个整数,第一行的所有整数构成 ...

  9. chromium之non_thread_safe

    先看看介绍 // A helper class used to help verify that methods of a class are // called from the same thre ...

  10. 单机安装hadoop+hive+presto

    系统环境 在个人笔记本上使用virtualbox虚拟机 os:centos -7.x86-64.everything.1611  ,内核 3.10.0-514.el7.x86_64 注:同样可以使用r ...