1,首先建立mysql的数据库,表:如下图
下面是MapReduce编程了,需要导入mysql数据库驱动jar包;
首先需要自定义Recoder,这里是CourceRecord
public class CourceRecord implements Writable, DBWritable {
int id;
String name;
int count;
int no;
//从in流反序列化出内容
@Override
public void readFields(DataInput in) throws IOException {
this.id = in.readInt();
this.name = Text.readString(in);
this.count = in.readInt();
this.no = in.readInt();
}
//将内容序列化到out流
@Override
public void write(DataOutput out) throws IOException {
out.writeInt(this.id);
Text.writeString(out, this.name);
out.writeInt(this.count);
out.writeInt(this.no);
}
@Override
public void readFields(ResultSet result) throws SQLException {
this.id = result.getInt(1);
this.name = result.getString(2);
this.count = result.getInt(3);
this.no = result.getInt(4);
}
@Override
public void write(PreparedStatement stmt) throws SQLException {
stmt.setInt(1, this.id);
stmt.setString(2, this.name);
stmt.setInt(3, this.count);
stmt.setInt(4, this.no);
}
@Override
public String toString() {
return new String(this.name + " " + this.count + " " + this.no);
}
}
------------------------------------------------map类-----------------------------------
public class DBMapper extends MapReduceBase implements
Mapper<LongWritable, CourceRecord, LongWritable, Text> {
public void map(LongWritable key, CourceRecord value,
OutputCollector<LongWritable, Text> collector, Reporter reporter)
throws IOException {
collector.collect(new LongWritable(value.id),
new Text(value.toString()));
}
}
--------------------------------------driver驱动类DB------------------
public class DB {
public static void main(String[] args) throws IOException {
String[] argc={"jdbc:mysql://localhost/test","root", "111111"};
try {
JobConf conf = new JobConf(DB.class);
Class.forName("com.mysql.jdbc.Driver");
DBConfiguration.configureDB(conf, "com.mysql.jdbc.Driver",argc[0], argc[1], argc[2]);
String[] fields = { "id", "name", "count", "no" };
DBInputFormat.setInput(conf, CourceRecord.class, "tb", null,"id", fields);
conf.setInputFormat(DBInputFormat.class);
conf.setOutputKeyClass(LongWritable.class);
conf.setOutputValueClass(Text.class);
Path path = new Path("DBOUTPUT");
FileOutputFormat.setOutputPath(conf, path);
conf.setMapperClass(DBMapper.class);
conf.setReducerClass(IdentityReducer.class);
//如果文件存在则删除
FileSystem hdfs = path.getFileSystem(conf);
if (hdfs.exists(path)) {
hdfs.delete(path, true);
}
JobClient.runJob(conf);
}
catch(ClassNotFoundException e) {
System.err.println("mysql.jdbc.Driver not found");
}
}
------------------------------------------------------
运行:在DBOUTPUT目录下生成一个part-00000的文件,其内容为:
17 hello 28 17
18 hadoop 28 18
19 java 28 19
20 hive 16 20
21 hbase 18 21
22 pig 18 22
实例是跑起来了,主要是DB的一个配置,相关设置函数:
DBConfiguration.configureDB (JobConf job, String driverClass, String Url, String userName, String passwd)
DBInputFormat.setInput(JobConf job, Class<?extends DBWritable> inputClass,
String tableName, String conditions,String orderBy, String fieldNames)
DBOutputFormat.setOut(JobConf
job,String tablename,String fieldNames);
eg:DBOutputFormat.setOut(job,"tb",28,"mahout",30,401)
分享到:
相关推荐
本项目以电影数据为主题,基于hadoop伪分布式搭建,结合hive数据仓库调用物理机mysql数据库实现电影相关数据统计、通过Mapreduce编程对hdfs文件系统的文件进行词频统计。使用python进行电影数据采集、处理、分析及...
02-hive的元数据库mysql方式安装配置.avi 03-hive的使用.avi 04-hive的常用语法.avi 05-hql语法及自定义函数.avi 06-hbase表结构.avi 07-hbase集群架构及表存储机制.avi 08-hbase-shell.avi 09-hbase的java ...
本项目以电影数据为主题,基于hadoop伪分布式搭建,结合hive数据仓库调用物理机mysql数据库实现电影相关数据统计、通过Mapreduce编程对hdfs文件系统的文件进行词频统计。使用python进行电影数据采集、处理、分析及...
本系统基于Hadoop平台,使用Maven进行项目管理,基于ssm框架开发的项目,使用mysql数据库,前端采用JQuery+Bootstrap+ECharts展示数据。 ### 2.开发工具介绍 ### 开发工具 系统开发过程中使用到的开发工具及软件...
6.1 RMySQL数据库编程指南 168 6.2 R利剑NoSQL 之MongoDB 183 6.3 R利剑NoSQL之Redis 192 6.4 R利剑NoSQL之Cassandra 200 6.5 R利剑NoSQL之Hive 206 6.6 用RHive从历史数据中提取逆回购信息 212 第7章 ...
6.1 RMySQL数据库编程指南 168 6.2 R利剑NoSQL 之MongoDB 183 6.3 R利剑NoSQL之Redis 192 6.4 R利剑NoSQL之Cassandra 200 6.5 R利剑NoSQL之Hive 206 6.6 用RHive从历史数据中提取逆回购信息 212 第7章 ...
3 传统数据仓库在⾯对更⼤规模数据时显得⼒不从⼼,在寄希望于⼤数据平台时,MapReduce 编程门槛让很多数据分析师望⽽却步,⽽Hive 是基于Hadoop的⼀个数据仓库⼯具,可以将结构化的数据⽂件映射为⼀张数据库表,并...
本系统基于Hadoop平台,使用Maven进行项目管理,基于ssm框架开发的项目,使用mysql数据库,前端采用JQuery+Bootstrap+ECharts展示数据。 2.开发工具介绍 开发工具 系统开发过程中使用到的开发工具及软件版本情况...
MapReduce最伟⼤之处在于其将处理⼤数据的能⼒赋予了普通开发⼈员,以⾄于普通开发⼈员即使不会任何的分布式编程知识,也能将⾃ ⼰的程序运⾏在分布式系统上处理海量数据。 Hive:MapReduce将处理⼤数据的能⼒赋予...
Flume⽇志采集,Sqoop数据库ETL(抽取、转换、加载),完成Hadoop系统组 件之间的互通,Hadoop系统组件当中数据和关系数据库当中数据之间相互导⼊导出(HDFS数据可以导⼊MySql当中去)。 MapReduce,Map函数和Reduce...
89_hadoop-mapreduce-url演示1 B% m, V- Z) ~. B9 |9 m2 u 90_job提交流程剖析 91_job split计算法则-读取切片的法则 92_job seqfile5 v! h+ R9 L1 w, U* T6 J# M 93_job 全排序-自定义分区类2 n% h" `: b4 c) C3 J9...
6、熟悉Oracle、Mongodb、Mysql数据库的使用; 7、扎实的Java语言基础,熟悉Java开发工具和调试工具的使用; 8、良好的团队协作精神,有能力对团队在软件设计、实现和测试方面进行指导; 9、良好的逻辑分析能力和沟通...
在努力提高我的技术水平以利用各种技术方面铺平道路,这些技术包括:技术背景: 编程语言:Java,Python,R,MATLAB,C,C ++ 数据库:Oracle,MS SQL,MySQL,MS Access,Hadoop,Spark,MapReduce Web服务和技术...
HDFS界⾯、MR界⾯、简单的SHELL、java访问hadoop)、HDFS(简介、SHELL、IDEA开发⼯具使⽤、全分布式集群搭建)、 MapReduce应⽤(中间计算过程、Java操作MapReduce、程序运⾏、⽇志监控)、Hadoop⾼级应⽤(YARN框架介绍...