[Hadoop]Sqoop 1.4.2中文文件(三)之SqoopJob與其外的操作

大搜車-自娛發表於2012-10-25
[size=large][b]一、sqoop job相關命令引數[/b][/size]
usage: sqoop job [GENERIC-ARGS] [JOB-ARGS] [-- [<tool-name>] [TOOL-ARGS]]

Job management arguments:
--create <job-id> Create a new saved job
--delete <job-id> Delete a saved job
--exec <job-id> Run a saved job
--help Print usage instructions
--list List saved jobs
--meta-connect <jdbc-uri> Specify JDBC connect string for the
metastore
--show <job-id> Show the parameters for a saved job
--verbose Print more information while working

Generic Hadoop command-line arguments:
(must preceed any tool-specific arguments)
Generic options supported are
-conf <configuration file> specify an application configuration file
-D <property=value> use value for given property
-fs <local|namenode:port> specify a namenode
-jt <local|jobtracker:port> specify a job tracker
-files <comma separated list of files> specify comma separated files to be copied to the map reduce cluster
-libjars <comma separated list of jars> specify comma separated jar files to include in the classpath.
-archives <comma separated list of archives> specify comma separated archives to be unarchived on the compute machines.


簡單第一眼望去,終於比匯入和匯出的引數少了很多,自然內容好說一些。

Job存在的目的,是對頻繁只用不變化的匯入匯出工作做自動化處理,例如建立一個Job每天做增量匯入,匯入最新的資料,這樣的任務就可以使用Job來進行。

舉個例子:
建立一個Job
$ sqoop job --create myjob -- import --connect jdbc:mysql://example.com/db \
--table mytable


查詢當前Job列表
$ sqoop job --list
Available jobs:
myjob


檢視某個Job的詳情:
 $ sqoop job --show myjob
Job: myjob
Tool: import
Options:
----------------------------
direct.import = false
codegen.input.delimiters.record = 0
hdfs.append.dir = false
db.table = mytable
...


執行某個Job:
$ sqoop job --exec myjob
10/08/19 13:08:45 INFO tool.CodeGenTool: Beginning code generation
...

執行時重寫相關引數,例如你資料庫的使用者名稱和密碼改變了:
$ sqoop job --exec myjob -- --username someuser -P
Enter password:
...


擁有--meta-connect私有儲存空間的hadoop機器才能進行job操作,否則將會報如下錯誤:
[work@vm-nba01 ~]$ sqoop job --list
12/10/24 16:38:34 ERROR tool.JobTool: There is no JobStorage implementation available
12/10/24 16:38:34 ERROR tool.JobTool: that can read your specified storage descriptor.
12/10/24 16:38:34 ERROR tool.JobTool: Don't know where to save this job info! You may
12/10/24 16:38:34 ERROR tool.JobTool: need to specify the connect string with --meta-connect.

也就是說機器不知道你要的Job儲存在哪裡了,所以關於一切Job的操作都是徒勞的。

[size=large][b]二、Metastore connection options[/b][/size]
上面Joblist出錯的問題這裡就可以解決了,這裡可以教你怎麼建立Job的儲存空間,相關引數:
Argument	 Description
--meta-connect <jdbc-uri> Specifies the JDBC connect string used to connect to the metastore


預設你會在$HOME/.sqoop目錄下有一個私有的資料儲存,你通過sqoop-metastore命令可以使用熱資料儲存來建立空間
By default, a private metastore is instantiated in $HOME/.sqoop. If you have configured a hosted metastore with the sqoop-metastore tool, you can connect to it by specifying the --meta-connect argument. This is a JDBC connect string just like the ones used to connect to databases for import.

In conf/sqoop-site.xml, you can configure sqoop.metastore.client.autoconnect.url with this address, so you do not have to supply --meta-connect to use a remote metastore. This parameter can also be modified to move the private metastore to a location on your filesystem other than your home directory.

If you configure sqoop.metastore.client.enable.autoconnect with the value false, then you must explicitly supply --meta-connect.(上述位置待以後實際操作後再翻譯)

找到sqoop-site.xml的配置檔案,發現如下配置:
<property>
<name>sqoop.metastore.client.enable.autoconnect</name>
<value>false</value>
<description>If true, Sqoop will connect to a local metastore
for job management when no other metastore arguments are
provided.
</description>
</property>

看來這個值設定為了false,導致不能夠使用Job相關操作了。

<property>
<name>sqoop.metastore.client.record.password</name>
<value>true</value>
<description>If true, allow saved passwords in the metastore.
</description>
</property>

當這個值為真的時候才會儲存密碼。

sqoop-metastore命令可以檢查你配置sqoop資料連線的正確性,當然這些配置還是在sqoop-site.xml中。

sqoop-merge命令可以允許你合併2個資料集到1個資料集。merge命令的相關引數:
Argument	 Description
--class-name <class> Specify the name of the record-specific class to use during the merge job.
--jar-file <file> Specify the name of the jar to load the record class from.
--merge-key <col> Specify the name of a column to use as the merge key.
--new-data <path> Specify the path of the newer dataset.
--onto <path> Specify the path of the older dataset.
--target-dir <path> Specify the target path for the output of the merge job.


舉例:
$ sqoop merge --new-data newer --onto older --target-dir merged \
--jar-file datatypes.jar --class-name Foo --merge-key id

這樣就會執行一個MapReduce的任務,新的資料集使用優先度高於老的資料集。
它能被使用在both SequenceFile-, Avro- and text-based incremental imports.並且新老的資料型別時相同的。

sqoop-codegen命令可以還原的你的java類,如果你的原始碼java丟失了,但是資料沒有丟失,可以使用這樣的命令:
$ sqoop codegen --connect jdbc:mysql://db.example.com/corp \
--table employees


sqoop-create-hive-table可以建立一個Hive表,複製某一個資料來源的資料儲存格式,例如:
$ sqoop create-hive-table --connect jdbc:mysql://db.example.com/corp \
--table employees --hive-table emps


sqoop-eval命令可以讓使用者快速做一個操作,結果顯示在控制檯中。例如:
$ sqoop eval --connect jdbc:mysql://db.example.com/corp \
--query "SELECT * FROM employees LIMIT 10"

$ sqoop eval --connect jdbc:mysql://db.example.com/corp \
-e "INSERT INTO foo VALUES(42, 'bar')"


sqoop-list-databases檢視某個資料來源的資料庫列表,例如:
$ sqoop list-databases --connect jdbc:mysql://database.example.com/
information_schema
employees


sqoop-list-tables檢視某個資料來源的表列表,例如:
$ sqoop list-tables --connect jdbc:mysql://database.example.com/corp
employees
payroll_checks
job_descriptions
office_supplies


sqoop現在支援的資料庫:
Database	 version	--direct support?	 connect string matches
HSQLDB 1.8.0+ No jdbc:hsqldb:*//
MySQL 5.0+ Yes jdbc:mysql://
Oracle 10.2.0+ No jdbc:oracle:*//
PostgreSQL 8.3+ Yes (import only) jdbc:postgresql://

相關文章