sqoop使用与原理

简介

sqoop:sql-to-hadoop。

Sqoop是一个用来将Hadoop和关系型数据库中的数据相互转移的工具,可以将一个关系型数据库中的数据导进到Hadoop的HDFS中,也可以将HDFS的数据导进到关系型数据库中。

sqoop架构图

Sqoop原理(以import为例)

Sqoop在import时,需要制定split-by参数。Sqoop根据不同的split-by参数值来进行切分,然后将切分出来的区域分配到不同map中。

每个map中再处理数据库中获取的一行一行的值,写入到HDFS中。同时split-by根据不同的参数类型有不同的切分方法,如比较简单的int型,

Sqoop会取最大和最小split-by字段值,然后根据传入的num-mappers来确定划分几个区域。 比如select max(split_by),min(split-by) from

得到的max(split-by)和min(split-by)分别为1000和1,而num-mappers为2的话,则会分成两个区域(1,500)和(501-100),

同时也会分成2个sql给2个map去进行导入操作,分别为select XXX from table where split-by>=1 and split-by<500和

select XXX from table where split-by>=501 and split-by<=1000。最后每个map各自获取各自SQL中的数据进行导入工作。

 

mapreduce job所需要的各种参数在Sqoop中的实现

 

1) InputFormatClass

com.cloudera.sqoop.mapreduce.db.DataDrivenDBInputFormat

2) OutputFormatClass

1)TextFile

com.cloudera.sqoop.mapreduce.RawKeyTextOutputFormat

2)SequenceFile

org.apache.hadoop.mapreduce.lib.output.SequenceFileOutputFormat

3)AvroDataFile

com.cloudera.sqoop.mapreduce.AvroOutputFormat

3)Mapper

1)TextFile

com.cloudera.sqoop.mapreduce.TextImportMapper                 

2)SequenceFile

com.cloudera.sqoop.mapreduce.SequenceFileImportMapper        

 

3)AvroDataFile

com.cloudera.sqoop.mapreduce.AvroImportMapper

4)taskNumbers

1)mapred.map.tasks(对应num-mappers参数)   2)job.setNumReduceTasks(0);

 

实例讲解:

这里以命令行:import –connect jdbc:mysql://localhost/test  –username root –password 123456 –query “select sqoop_1.id as foo_id, sqoop_2.id as bar_id from sqoop_1 ,sqoop_2  WHERE $CONDITIONS” –target-dir /user/sqoop/test -split-by sqoop_1.id   –hadoop-home=/home/hdfs/hadoop-0.20.2-CDH3B3  –num-mappers 2

注:红色部分参数,后接根据命令衍生的参数值

1)设置Input

DataDrivenImportJob.configureInputFormat(Job job, String tableName,String tableClassName, String splitByCol)

a)DBConfiguration.configureDB(Configuration conf, String driverClass,

     String dbUrl, String userName, String passwd, Integer fetchSize)

1).mapreduce.jdbc.driver.class com.mysql.jdbc.Driver

2).mapreduce.jdbc.url  jdbc:mysql://localhost/test              

3).mapreduce.jdbc.username  root

4).mapreduce.jdbc.password  123456

5).mapreduce.jdbc.fetchsize -2147483648

b)DataDrivenDBInputFormat.setInput(Job job,Class<? extends DBWritable> inputClass, String inputQuery, String inputBoundingQuery)

1)job.setInputFormatClass(DBInputFormat.class);                 2)mapred.jdbc.input.bounding.query SELECT MIN(sqoop_1.id), MAX(sqoop_2.id) FROM (select sqoop_1.id as foo_id, sqoop_2.id as bar_id from sqoop_1 ,sqoop_2  WHERE  (1 = 1) ) AS t1

3)job.setInputFormatClass(com.cloudera.sqoop.mapreduce.db.DataDrivenDBInputFormat.class);

4)mapreduce.jdbc.input.orderby sqoop_1.id

c)mapreduce.jdbc.input.class QueryResult

d)sqoop.inline.lob.length.max 16777216

 

2)设置Output

ImportJobBase.configureOutputFormat(Job job, String tableName,String tableClassName)

a)job.setOutputFormatClass(getOutputFormatClass());                b)FileOutputFormat.setOutputCompressorClass(job, codecClass);

c)SequenceFileOutputFormat.setOutputCompressionType(job,CompressionType.BLOCK);

d)FileOutputFormat.setOutputPath(job, outputPath);

3)设置Map

DataDrivenImportJob.configureMapper(Job job, String tableName,String tableClassName)

     a)job.setOutputKeyClass(Text.class);

     b)job.setOutputValueClass(NullWritable.class);

c)job.setMapperClass(com.cloudera.sqoop.mapreduce.TextImportMapper);

 

4)设置task number

JobBase.configureNumTasks(Job job)

mapred.map.tasks 4

job.setNumReduceTasks(0);

 

大概流程

 

1.读取要导入数据的表结构,生成运行类,默认是QueryResult,打成jar包,然后提交给Hadoop

 

2.设置好job,主要也就是设置好以上第六章中的各个参数

3.这里就由Hadoop来执行MapReduce来执行Import命令了,

1)首先要对数据进行切分,也就是DataSplit

DataDrivenDBInputFormat.getSplits(JobContext job)

2)切分好范围后,写入范围,以便读取

DataDrivenDBInputFormat.write(DataOutput output) 这里是lowerBoundQuery and  upperBoundQuery

3)读取以上2)写入的范围

DataDrivenDBInputFormat.readFields(DataInput input)

4)然后创建RecordReader从数据库中读取数据

DataDrivenDBInputFormat.createRecordReader(InputSplit split,TaskAttemptContext context)

5)创建Map

TextImportMapper.setup(Context context)

6)RecordReader一行一行从关系型数据库中读取数据,设置好Map的Key和Value,交给Map

DBRecordReader.nextKeyValue()

7)运行map

TextImportMapper.map(LongWritable key, SqoopRecord val, Context context)

最后生成的Key是行数据,由QueryResult生成,Value是NullWritable.get()

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值