Hadoop/MapReduce(单词统计--读写数据库)

3 篇文章 0 订阅

单词统计

1.  MySQL     

CREATE TABLE `word` (
   `id` int(11) NOT NULL AUTO_INCREMENT,
   `name` varchar(20) NOT NULL COMMENT '单词',
   `count` int(11) NOT NULL DEFAULT '1' COMMENT '次数',
   PRIMARY KEY (`id`)
 ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='单词表'

CREATE TABLE `word2` (
   `id` int(11) NOT NULL AUTO_INCREMENT,
   `name` varchar(20) NOT NULL COMMENT '单词',
   `count` int(11) NOT NULL DEFAULT '1' COMMENT '次数',
   PRIMARY KEY (`id`)
 ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='单词统计表'

INSERT INTO word(NAME) VALUES('JAVA'),('Hadoop'),('Spring'),('Hadoop'),('JAVA'),('JAVA'),('JAVA'),('Spring'),('Mysql'),('Hadoop'),('Mysql'),('Spring'),('Hadoop'),('Mysql'),('Mysql'),('Hadoop');
2. Java

  

public class Demo08 {
	public static class WordRecord implements Writable, DBWritable {
		int count;
		String name;
		public WordRecord() {
		}
		public void readFields(DataInput in) throws IOException {
			this.count = in.readInt();
			this.name = Text.readString(in);
		}		
		public void write(PreparedStatement stmt) throws SQLException {
			stmt.setInt(1, this.count);
			stmt.setString(2, this.name);
		}
		public void readFields(ResultSet result) throws SQLException {
			this.count = result.getInt(1);
			this.name = result.getString(2);
		}
		public void write(DataOutput out) throws IOException {
			out.writeInt(this.count);
			Text.writeString(out, this.name);
		}
	}

	public static class DBInputMapper extends MapReduceBase
			implements Mapper<LongWritable, WordRecord, Text, IntWritable> {
		public void map(LongWritable key, WordRecord value, OutputCollector<Text, IntWritable> collector,
				Reporter reporter) throws IOException {
			collector.collect(new Text(value.name), new IntWritable(value.count));
		}
	}

	public static class MyReducer extends MapReduceBase
			implements Reducer<Text, IntWritable, WordRecord, LongWritable> {
		public void reduce(Text key, Iterator<IntWritable> values, OutputCollector<WordRecord, LongWritable> output,
				Reporter reporter) throws IOException {
			WordRecord r = new WordRecord();
			r.name = key.toString();
			while(values.hasNext()){		
				r.count += values.next().get();
			}
			output.collect(r, new LongWritable(r.count));
		}
	}

	@SuppressWarnings("deprecation")
	public static void main(String[] args) throws IOException {
		JobConf conf = new JobConf(Demo08.class);
               //加载mysql连接jar包
                DistributedCache.addFileToClassPath(new Path("/tool/lib/mysql.jar"), conf);

		conf.setMapOutputKeyClass(Text.class);
		conf.setMapOutputValueClass(IntWritable.class);
		conf.setOutputKeyClass(Text.class);
		conf.setOutputValueClass(IntWritable.class);

		conf.setOutputFormat(DBOutputFormat.class);
		conf.setInputFormat(DBInputFormat.class);

		DBConfiguration.configureDB(conf, "com.mysql.jdbc.Driver", "jdbc:mysql://192.168.1.194:3306/test", "root", "root");
		String[] fields = { "count", "name" };
		DBInputFormat.setInput(conf, WordRecord.class, "word", null, null, fields);
		DBOutputFormat.setOutput(conf, "word2", "count", "name");
		conf.setMapperClass(DBInputMapper.class);
		conf.setReducerClass(MyReducer.class);

		JobClient.runJob(conf);
	}
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
这个命令是在 Hadoop 上运行一个计算 pi 的示例程序。其中,参数 10 表示计算的 map 数量,100 表示每个 map 任务要计算的数据量。由于 pi 是一个无限不循环小数,程序采用 Monte Carlo 方法来估算 pi 的值。 如果你想将计算结果保留小数点后 5 位数,可以修改程序代码或者在运行命令时添加一个额外的命令来进行格式化。 修改代码: 1. 打开 `hadoop-mapreduce-examples-3.1.3.jar` 文件,解压 `pi.class` 文件。 2. 打开 `pi.class` 文件,找到 `reduce()` 方法。 3. 在 `reduce()` 方法中添加以下代码: ``` BigDecimal pi = new BigDecimal(4.0 * count / total, new MathContext(6)); ``` 这行代码将计算得到的 pi 值保留小数点后 5 位数,存储在 `pi` 变量中。 4. 重新打包 `pi.class` 文件。 5. 将修改后的 `hadoop-mapreduce-examples-3.1.3.jar` 文件上传到 Hadoop 集群。 6. 运行以下命令: ``` ./bin/hadoop jar ./share/hadoop/mapreduce/hadoop-mapreduce-examples-3.1.3.jar pi 10 100 ``` 这将在 Hadoop 上运行修改后的程序,并输出计算得到的 pi 值。 添加格式化命令: 1. 运行以下命令: ``` ./bin/hadoop jar ./share/hadoop/mapreduce/hadoop-mapreduce-examples-3.1.3.jar pi 10 100 | awk '{printf "%.5f\n", $1}' ``` 这将在 Hadoop 上运行示例程序,并将计算得到的 pi 值输出到标准输出。 2. `awk` 命令将标准输出中的第一个字段(即计算得到的 pi 值)格式化为小数点后 5 位数,并输出到控制台。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值