Sqoopを使用してMySQLからHBAseとhiveにデータをインポート(転載)

13807 ワード

mysqlの全量データをhiveまたはhbaseにインポートする必要がある場合があります.sqoopを使用するのは比較的使いやすいツールで、速度は比較的速いです.mysqlのインクリメンタルデータは、他の方法でリアルタイムで同期されています.
一、mysqlをhbaseに同期する
インポートコマンド:sqoop import--connect jdbc:mysql://xxx.xxx.xxx.xxx:3306/database --table tablename--hbase-table hbasetablename--column-family family--hbase-row-key ID--hbase-create-table--username'root'-Pパラメータ説明:--connect:データベース接続列--username:ユーザー名--P:インタラクティブ入力パスワード--table:テーブル名-m:パラレル実行sqoopインポートシーケンスのmap taskの数は、指定されていない場合にデフォルトで4つのmap-split-byを起動します:パラレルインポート中、各map taskはどのフィールドに基づいてデータセグメントを分割します.このパラメータは、作成時間、増加したID--hbase-table:hbaseでデータを受信するテーブル名--hbase-create-table:指定した受信データテーブルがhbaseに存在しない場合、新しいテーブル--column-family:列ファミリー名、すべてのソーステーブルのフィールドがその列ファミリー--hbase-row-keyに入ります:指定しない場合、ソーステーブルのkeyをhbaseのrow keyとして使用します.row keyとして1つのフィールドを指定するか、行キーの組合せを指定します.行キーの組合せを指定すると、複数のフィールドが二重引用符で囲まれ、各フィールドはカンマで区切られます.
コマンド・セクションのログを実行するには、次の手順に従います.
[hdfs@slave1 ~]$ sqoop import --connect jdbc:mysql://xxx.xxx.xxx.xxx:3306/database --table tablename --hbase-table hbasetablename --column-family family --hbase-row-key ID --hbase-create-table --username 'root' -P
Warning: /soft/bigdata/clouderamanager/cloudera/parcels/CDH-5.10.0-1.cdh5.10.0.p0.41/bin/../lib/sqoop/../accumulo does not exist! Accumulo imports will fail.
Please set $ACCUMULO_HOME to the root of your Accumulo installation.
17/04/28 15:54:37 INFO sqoop.Sqoop: Running Sqoop version: 1.4.6-cdh5.10.0
Enter password: 
17/04/28 15:54:44 INFO manager.MySQLManager: Preparing to use a MySQL streaming resultset.
17/04/28 15:54:44 INFO tool.CodeGenTool: Beginning code generation
Fri Apr 28 15:54:44 CST 2017 WARN: Establishing SSL connection without server's identity verification is not recommended. According to MySQL 5.5.45+, 5.6.26+ and 5.7.6+ requirements SSL connection must be established by default if explicit option isn't set. For compliance with existing applications not using SSL the verifyServerCertificate property is set to 'false'. You need either to explicitly disable SSL by setting useSSL=false, or set useSSL=true and provide truststore for server certificate verification.
17/04/28 15:54:45 INFO manager.SqlManager: Executing SQL statement: SELECT t.* FROM `COMP_DICT` AS t LIMIT 1
17/04/28 15:54:45 INFO manager.SqlManager: Executing SQL statement: SELECT t.* FROM `COMP_DICT` AS t LIMIT 1
17/04/28 15:54:45 INFO orm.CompilationManager: HADOOP_MAPRED_HOME is /soft/bigdata/clouderamanager/cloudera/parcels/CDH/lib/hadoop-mapreduce
Note: /tmp/sqoop-hdfs/compile/f5c3b693ffb26b66c554308ad32b2880/COMP_DICT.java uses or overrides a deprecated API.
Note: Recompile with -Xlint:deprecation for details.
17/04/28 15:54:47 INFO orm.CompilationManager: Writing jar file: /tmp/sqoop-hdfs/compile/f5c3b693ffb26b66c554308ad32b2880/COMP_DICT.jar
……
7/04/28 15:54:53 INFO mapreduce.Job: The url to track the job: http://master2:8088/proxy/application_1491881598805_0027/
17/04/28 15:54:53 INFO mapreduce.Job: Running job: job_1491881598805_0027
17/04/28 15:54:59 INFO mapreduce.Job: Job job_1491881598805_0027 running in uber mode : false
17/04/28 15:54:59 INFO mapreduce.Job:  map 0% reduce 0%
17/04/28 15:55:05 INFO mapreduce.Job:  map 20% reduce 0%
17/04/28 15:55:06 INFO mapreduce.Job:  map 60% reduce 0%
17/04/28 15:55:09 INFO mapreduce.Job:  map 100% reduce 0%
17/04/28 15:55:10 INFO mapreduce.Job: Job job_1491881598805_0027 completed successfully
17/04/28 15:55:10 INFO mapreduce.Job: Counters: 30
    File System Counters
        FILE: Number of bytes read=0
        FILE: Number of bytes written=925010
        FILE: Number of read operations=0
        FILE: Number of large read operations=0
        FILE: Number of write operations=0
        HDFS: Number of bytes read=665
        HDFS: Number of bytes written=0
        HDFS: Number of read operations=5
        HDFS: Number of large read operations=0
        HDFS: Number of write operations=0
    Job Counters 
        Launched map tasks=5
        Other local map tasks=5
        Total time spent by all maps in occupied slots (ms)=25663
        Total time spent by all reduces in occupied slots (ms)=0
        Total time spent by all map tasks (ms)=25663
        Total vcore-seconds taken by all map tasks=25663
        Total megabyte-seconds taken by all map tasks=26278912
    Map-Reduce Framework
        Map input records=10353
        Map output records=10353
        Input split bytes=665
        Spilled Records=0
        Failed Shuffles=0
        Merged Map outputs=0
        GC time elapsed (ms)=586
        CPU time spent (ms)=17940
        Physical memory (bytes) snapshot=1619959808
        Virtual memory (bytes) snapshot=14046998528
        Total committed heap usage (bytes)=1686634496
    File Input Format Counters 
        Bytes Read=0
    File Output Format Counters 
        Bytes Written=0
17/04/28 15:55:10 INFO mapreduce.ImportJobBase: Transferred 0 bytes in 20.0424 seconds (0 bytes/sec)
17/04/28 15:55:10 INFO mapreduce.ImportJobBase: Retrieved 10353 records.

PS:テーブルのプライマリ・キーもhbaseのcell、つまり列ファミリーの列に追加するには、sqoop.hbase.add.row.key=trueというパラメータを設定する必要があります.
sqoop import -D sqoop.hbase.add.row.key=true --connect jdbc:mysql://xxx.xxx.xxx.xxx:3306/database --table tablename --hbase-table hbasetablename --column-family family --hbase-row-key "etl_date,APPLY_ID" --hbase-create-table --username 'root' -P

二、mysqlはhiveに同期する
1、mysqlと同じ構造のテーブルを作成する:
sqoop create-hive-table --connect jdbc:mysql://xxx.xxx.xxx.xxx:3306/shiro --table UserInfo --hive-database shiro --hive-table userinfo --username root --password xxxxxx --fields-terminated-by "\0001"--lines-terminated-by "";パラメータの説明:--fields-terminated-by"0001"は各列間の区切り記号を設定し、"0001"はASCIIコードの1であり、hiveのデフォルトの行内区切り記号でもあり、sqoopのデフォルトの行内区切り記号は","--lines-terminated-by""は各行間の区切り記号を設定し、ここでは改行記号であり、デフォルトの区切り記号でもある.
2、mysqlデータをhiveにインポートする
sqoop import --connect jdbc:mysql://xxx.xxx.xxx.xxx:3306/testSqoop --table dydata --hive-database testsqoop --hive-import --hive-table dydata --username root --password xxxxxx --fields-terminated-by "\0001";パラメータの説明:-m 2は、2つのmapジョブによって実行されることを示します.fields-terminated-by"0001"はhiveテーブルの作成時と一致する必要があります.--hive-importは必ずこのパラメータを追加しなければなりません.そうしないとhiveに正常にインポートできません.
作者:物覚えの悪い自分へ
リンク:https://www.jianshu.com/p/929934b5e9b8
出典:簡書
簡書の著作権は著者の所有であり、いかなる形式の転載も著者に連絡して授権を得て出典を明記してください.