hadoop集群读写mysql_Hadoop集群(第10期)_MySQL关系数据库

packagecom.hebut.mr;

importJava.io.IOException;

importjava.io.DataInput;

importjava.io.DataOutput;

importjava.sql.PreparedStatement;

importjava.sql.ResultSet;

importjava.sql.SQLException;

importjava.util.Iterator;

importjava.util.StringTokenizer;

importorg.apache.Hadoop.filecache.DistributedCache;

importorg.apache.hadoop.fs.Path;

importorg.apache.hadoop.io.IntWritable;

importorg.apache.hadoop.io.Text;

importorg.apache.hadoop.io.Writable;

importorg.apache.hadoop.mapred.FileInputFormat;

importorg.apache.hadoop.mapred.JobClient;

importorg.apache.hadoop.mapred.JobConf;

importorg.apache.hadoop.mapred.MapReduceBase;

importorg.apache.hadoop.mapred.Mapper;

importorg.apache.hadoop.mapred.OutputCollector;

importorg.apache.hadoop.mapred.Reducer;

importorg.apache.hadoop.mapred.Reporter;

importorg.apache.hadoop.mapred.TextInputFormat;

importorg.apache.hadoop.mapred.lib.db.DBOutputFormat;

importorg.apache.hadoop.mapred.lib.db.DBWritable;

importorg.apache.hadoop.mapred.lib.db.DBConfiguration;

publicclassWriteDB {

// Map处理过程

publicstaticclassMap extendsMapReduceBase implements

Mapper {

privatefinalstaticIntWritable one= newIntWritable(1);

privateText word= newText();

@Override

publicvoidmap(Object key, Text value,

OutputCollector output, Reporter reporter)

throwsIOException {

String line = value.toString();

StringTokenizer tokenizer = newStringTokenizer(line);

while(tokenizer.hasMoreTokens()) {

word.set(tokenizer.nextToken());

output.collect(word, one);

}

}

}

// Combine处理过程

publicstaticclassCombine extendsMapReduceBase implements

Reducer {

@Override

publicvoidreduce(Text key, Iterator values,

OutputCollector output, Reporter reporter)

throwsIOException {

intsum = 0;

while(values.hasNext()) {

sum += values.next().get();

}

output.collect(key, newIntWritable(sum));

}

}

// Reduce处理过程

publicstaticclassReduce extendsMapReduceBase implements

Reducer {

@Override

publicvoidreduce(Text key, Iterator values,

OutputCollector collector, Reporter reporter)

throwsIOException {

intsum = 0;

while(values.hasNext()) {

sum += values.next().get();

}

WordRecord wordcount = newWordRecord();

wordcount.word= key.toString();

wordcount.number= sum;

collector.collect(wordcount, newText());

}

}

publicstaticclassWordRecord implementsWritable, DBWritable {

publicString word;

publicintnumber;

@Override

publicvoidreadFields(DataInput in) throwsIOException {

this.word= Text.readString(in);

this.number= in.readInt();

}

@Override

publicvoidwrite(DataOutput out) throwsIOException {

Text.writeString(out, this.word);

out.writeInt(this.number);

}

@Override

publicvoidreadFields(ResultSet result) throwsSQLException {

this.word= result.getString(1);

this.number= result.getInt(2);

}

@Override

publicvoidwrite(PreparedStatement stmt) throwsSQLException {

stmt.setString(1, this.word);

stmt.setInt(2, this.number);

}

}

publicstaticvoidmain(String[] args) throwsException {

JobConf conf = newJobConf(WriteDB.class);

//这句话很关键

conf.set("mapred.job.tracker", "192.168.1.2:9001");

DistributedCache.addFileToClassPath(newPath(

"/lib/MySQL-connector-java-5.1.18-bin.jar"), conf);

//设置输入输出类型

conf.setInputFormat(TextInputFormat.class);

conf.setOutputFormat(DBOutputFormat.class);

//不加这两句,通不过,但是网上给的例子没有这两句。

conf.setOutputKeyClass(Text.class);

conf.setOutputValueClass(IntWritable.class);

//设置Map和Reduce类

conf.setMapperClass(Map.class);

conf.setCombinerClass(Combine.class);

conf.setReducerClass(Reduce.class);

//设置输如目录

FileInputFormat.setInputPaths(conf, newPath("wdb_in"));

//建立数据库连接

DBConfiguration.configureDB(conf, "com.mysql.jdbc.Driver",

"jdbc:mysql://192.168.1.24:3306/school", "root", "hadoop");

//写入"wordcount"表中的数据

String[] fields = { "word", "number"};

DBOutputFormat.setOutput(conf, "wordcount", fields);

JobClient.runJob(conf);

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值