一簡(jiǎn)介
Sqoop是一個(gè)用來將Hadoop和關(guān)系型數(shù)據(jù)庫(kù)中的數(shù)據(jù)相互轉(zhuǎn)移的工具,可以將一個(gè)關(guān)系型數(shù)據(jù)庫(kù)(例如 : MySQL ,Oracle ,Postgres等)中的數(shù)據(jù)導(dǎo)進(jìn)到Hadoop的HDFS中,也可以將HDFS的數(shù)據(jù)導(dǎo)進(jìn)到關(guān)系型數(shù)據(jù)庫(kù)中。
二特點(diǎn)
Sqoop中一大亮點(diǎn)就是可以通過hadoop的mapreduce把數(shù)據(jù)從關(guān)系型數(shù)據(jù)庫(kù)中導(dǎo)入數(shù)據(jù)到HDFS。
三 Sqoop 命令
Sqoop大約有13種命令,和幾種通用的參數(shù)(都支持這13種命令),這里先列出這13種命令。 接著列出Sqoop的各種通用參數(shù),然后針對(duì)以上13個(gè)命令列出他們自己的參數(shù)。Sqoop通用參數(shù)又分Common arguments,Incremental import arguments,Output line formatting arguments,Input parsing arguments,Hive arguments,HBase arguments,Generic Hadoop command-line arguments,下面一一說明: 1.Common arguments 通用參數(shù),主要是針對(duì)關(guān)系型數(shù)據(jù)庫(kù)鏈接的一些參數(shù)
四 sqoop命令舉例
1)列出mysql數(shù)據(jù)庫(kù)中的所有數(shù)據(jù)庫(kù) sqoop list-databases –connect jdbc:mysql://localhost:3306/ –username root –password 123456
2)連接mysql并列出test數(shù)據(jù)庫(kù)中的表 sqoop list-tables –connect jdbc:mysql://localhost:3306/test –username root –password 123456 命令中的test為mysql數(shù)據(jù)庫(kù)中的test數(shù)據(jù)庫(kù)名稱 username password分別為mysql數(shù)據(jù)庫(kù)的用戶密碼
3)將關(guān)系型數(shù)據(jù)的表結(jié)構(gòu)復(fù)制到hive中,只是復(fù)制表的結(jié)構(gòu),表中的內(nèi)容沒有復(fù)制過去。 sqoop create-hive-table –connect jdbc:mysql://localhost:3306/test –table sqoop_test –username root –password 123456 –hive-table test 其中 –table sqoop_test為mysql中的數(shù)據(jù)庫(kù)test中的表 –hive-table test 為hive中新建的表名稱
4)從關(guān)系數(shù)據(jù)庫(kù)導(dǎo)入文件到hive中 sqoop import –connect jdbc:mysql://localhost:3306/zxtest –username root –password 123456 –table sqoop_test –hive-import –hive-table s_test -m 1
5)將hive中的表數(shù)據(jù)導(dǎo)入到mysql中,在進(jìn)行導(dǎo)入之前,mysql中的表 hive_test必須已經(jīng)提起創(chuàng)建好了。 sqoop export –connect jdbc:mysql://localhost:3306/zxtest –username root –password root –table hive_test –export-dir /user/hive/warehouse/new_test_partition/dt=2012-03-05
6)從數(shù)據(jù)庫(kù)導(dǎo)出表的數(shù)據(jù)到HDFS上文件 ./sqoop import –connect jdbc:mysql://10.28.168.109:3306/compression –username=hadoop –password=123456 –table HADOOP_USER_INFO -m 1 –target-dir /user/test
7)從數(shù)據(jù)庫(kù)增量導(dǎo)入表數(shù)據(jù)到hdfs中 ./sqoop import –connect jdbc:mysql://10.28.168.109:3306/compression –username=hadoop –password=123456 –table HADOOP_USER_INFO -m 1 –target-dir /user/test –check-column id –incremental append –last-value 3
五 Sqoop原理(以import為例)
Sqoop在import時(shí),需要制定split-by參數(shù)。Sqoop根據(jù)不同的split-by參數(shù)值來進(jìn)行切分,然后將切分出來的區(qū)域分配到不同map中。每個(gè)map中再處理數(shù)據(jù)庫(kù)中獲取的一行一行的值,寫入到HDFS中。同時(shí)split-by根據(jù)不同的參數(shù)類型有不同的切分方法,如比較簡(jiǎn)單的int型,Sqoop會(huì)取最大和最小split-by字段值,然后根據(jù)傳入的num-mappers來確定劃分幾個(gè)區(qū)域。 比如select max(split_by),min(split-by) from得到的max(split-by)和min(split-by)分別為1000和1,而num-mappers為2的話,則會(huì)分成兩個(gè)區(qū)域(1,500)和(501-100),同時(shí)也會(huì)分成2個(gè)sql給2個(gè)map去進(jìn)行導(dǎo)入操作,分別為select XXX from table where split-by>=1 and split-by<500和select XXX from table where split-by>=501 and split-by<=1000。最后每個(gè)map各自獲取各自SQL中的數(shù)據(jù)進(jìn)行導(dǎo)入工作。
六mapreduce job所需要的各種參數(shù)在Sqoop中的實(shí)現(xiàn)
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(對(duì)應(yīng)num-mappers參數(shù)) 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)設(shè)置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)設(shè)置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)設(shè)置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)設(shè)置task number JobBase.configureNumTasks(Job job) mapred.map.tasks 4 job.setNumReduceTasks(0);
七 大概流程
1.讀取要導(dǎo)入數(shù)據(jù)的表結(jié)構(gòu),生成運(yùn)行類,默認(rèn)是QueryResult,打成jar包,然后提交給Hadoop
2.設(shè)置好job,主要也就是設(shè)置好以上第六章中的各個(gè)參數(shù)
3.這里就由Hadoop來執(zhí)行MapReduce來執(zhí)行Import命令了,
1)首先要對(duì)數(shù)據(jù)進(jìn)行切分,也就是DataSplit DataDrivenDBInputFormat.getSplits(JobContext job)
2)切分好范圍后,寫入范圍,以便讀取 DataDrivenDBInputFormat.write(DataOutput output) 這里是lowerBoundQuery and upperBoundQuery
3)讀取以上2)寫入的范圍 DataDrivenDBInputFormat.readFields(DataInput input)
4)然后創(chuàng)建RecordReader從數(shù)據(jù)庫(kù)中讀取數(shù)據(jù) DataDrivenDBInputFormat.createRecordReader(InputSplit split,TaskAttemptContext context)
5)創(chuàng)建Map TextImportMapper.setup(Context context)
6)RecordReader一行一行從關(guān)系型數(shù)據(jù)庫(kù)中讀取數(shù)據(jù),設(shè)置好Map的Key和Value,交給Map DBRecordReader.nextKeyValue()
7)運(yùn)行map TextImportMapper.map(LongWritable key, SqoopRecord val, Context context) 最后生成的Key是行數(shù)據(jù),由QueryResult生成,Value是NullWritable.get()
八 總結(jié)
通過這些,了解了MapReduce運(yùn)行流程.但對(duì)于Sqoop這種切分方式感覺還是有很大的問題.比如這里根據(jù)ID范圍來切分,如此切分出來的數(shù)據(jù)會(huì)很不平均,比如min(split-id)=1,max(split-id)=3000,交給三個(gè)map來處理。那么范圍是(1-1000),(1001-2000),(2001-3000).而假如1001-2000是沒有數(shù)據(jù),已經(jīng)被刪除了。那么這個(gè)map就什么都不能做。而其他map卻累的半死。如此就會(huì)拖累job的運(yùn)行結(jié)果。這里說的范圍很小,比如有幾十億條數(shù)據(jù)交給幾百個(gè)map去做。map一多,如果任務(wù)不均衡就會(huì)影響進(jìn)度??从袥]有更好的切分方式?比如取樣?如此看來,寫好map reduce也不簡(jiǎn)單!、
http://www./thread-6242-1-1.html |