【问题标题】:Using sqoop import, How to append rows into existing hive table?使用 sqoop 导入,如何将行追加到现有的配置单元表中?
【发布时间】:2015-12-29 09:21:57
【问题描述】:

我从 SQL 服务器导入并使用以下查询创建了一个配置单元表。

sqoop import --connect 'jdbc:sqlserver://10.1.1.12;database=testdb' --username uname --password paswd --table demotable --hive-import --hive-table hivedb.demotable --create-hive-table --fields-terminated-by ','

命令成功,导入数据并创建了10000条记录的表。

我在 SQL Server 中插入了 10 条新记录,并尝试使用 --where 子句将这 10 条记录附加到现有的 hive 表中

sqoop import --connect 'jdbc:sqlserver://10.1.1.12;database=testdb' --username uname --password paswd --table demotable --where "ID > 10000" --hive-import -hive-table hivedb.demotable

但是 sqoop 作业因错误而失败

ERROR tool.ImportTool:导入时出错:导入作业失败!

我哪里错了?使用 sqoop 插入表的任何其他替代方法。

编辑: 稍微更改上述命令后,我就可以追加新行了。

sqoop import --connect 'jdbc:sqlserver://10.1.1.12;database=testdb' --username uname --password paswd --table demotable --where "ID > 10000" --hive-import -hive-table hivedb.demotable --fields-terminated-by ',' -m 1

虽然它解决了上述问题,但我无法插入修改后的行。有没有办法在不使用的情况下插入修改后的行 --增量 lastmodified 参数。

【问题讨论】:

  • 您能否使用 --verbose 运行并获得正确的错误消息。?

标签: hadoop hive sqoop


【解决方案1】:

我们可以使用这个命令:

 sqoop import --connect 'jdbc:sqlserver://10.1.1.12;database=testdb' --username uname --password paswd --query 'select * from demotable where ID > 10000' --hive-import --hive-table hivedb.demotable --target-dir demotable_data

【讨论】:

  • 我试过你的命令我得到了以下错误“必须用--target-dir指定目的地。”
  • 但是使用 --hive-import --hive-table 它会直接在 /user/hive/warehouse/ 中创建一个表,对吗?好的,我会试试的。
  • madhu:我更新了描述。请看一看。
【解决方案2】:

要将行追加到 hive 表,请使用您之前使用的相同查询,只需删除 --hive-overwrite。

我将分享我以前在 hive 中导入的 2 个查询,一个用于覆盖,一个用于附加,您可以使用相同的方式进行导入:

覆盖以前的记录

sqoop import -Dmapreduce.job.queuename=default --connect     jdbc:teradata://database_connection_string/DATABASE=database_name,TMODE=ANSI,LOGMECH=LDAP --username z****** --password ******* --query "select * from ****** where \$CONDITIONS" --split-by "HASHBUCKET(HASHROW(key to split)) MOD 4" --num-mappers 4 --hive-table hive_table_name --boundary-query "select 0, 3 from dbc.dbcinfo" --target-dir directory_name  --delete-target-dir --hive-import --hive-overwrite --driver com.teradata.jdbc.TeraDriver

追加到以前的记录

 sqoop import -Dmapreduce.job.queuename=default --connect jdbc:teradata://connection_string/DATABASE=db_name,TMODE=ANSI,LOGMECH=LDAP --username ****** --password ******--query "select * from **** where \$CONDITIONS" --split-by "HASHBUCKET(HASHROW(key to split)) MOD 4" --num-mappers 4 --hive-import --hive-table guestblock.prodrptgstrgtn --boundary-query "select 0, 3 from dbc.dbcinfo" --target-dir directory_name  --delete-target-dir --driver com.teradata.jdbc.TeraDriver

请注意,我使用了 4 个映射器,您也可以使用更多。

【讨论】:

    【解决方案3】:

    使用--append 选项和-m 1 如下所示:

    sqoop import --connect 'jdbc:sqlserver://10.1.1.12;database=testdb' --username uname --password paswd --table demotable --hive-import --hive-table hivedb.demotable --append -m 1
    

    【讨论】:

      【解决方案4】:

      我不确定您是否可以在 sqoop 中使用 --hive-import 选项直接提供 --append 选项。它至少在 1.4 版中仍然不可用。

      当 --hive-overwrite 和 --create-hive-table 缺失时,默认行为是追加。 (至少在这种情况下。

      我同意 nakulchawla09 的回答。虽然提醒自己保留 --split-by 选项。这将确保正确创建 hive 数据存储中的拆分名称。否则你不会喜欢默认命名。如果您不关心后台 Hive 仓库命名和后台数据存储,可以忽略此注释。当我尝试使用以下命令时

      在追加之前

      beeline:hive2> select count(*) from geolocation;
      
      +-------+--+
      |  _c0  |
      +-------+--+
      | 8000  |
      +-------+--+
      

      追加前hive仓库中的文件

      -rwxrwxrwx   1 root hdfs     479218 2018-10-12 11:03 /apps/hive/warehouse/geolocation/part-m-00000
      

      sqoop 命令再次追加额外的 8k 记录

      sqoop import --connect jdbc:mysql://localhost/RAWDATA --table geolocation --username root --password hadoop --target-dir /rawdata --hive-import  --driver com.mysql.jdbc.Driver --m 1 --delete-target-dir
      

      它创建了以下文件。您可以看到文件名不是很好,因为没有给出拆分选项或拆分哈希(可以是日期时间或日期)。

      -rwxrwxrwx   1 root hdfs     479218 2018-10-12 11:03 /apps/hive/warehouse/geolocation/part-m-00000
      -rwxrwxrwx   1 root hdfs     479218 2018-10-12 11:10 /apps/hive/warehouse/geolocation/part-m-00000_copy_1
      

      现在追加 hive 记录

      beeline:hive2> 从地理位置中选择 count(*);

      +-------+--+
      |  _c0  |
      +-------+--+
      | 16000  |
      +-------+--+
      

      【讨论】:

        猜你喜欢
        • 2017-08-05
        • 2021-05-09
        • 2015-11-12
        • 1970-01-01
        • 1970-01-01
        • 2013-02-03
        • 1970-01-01
        • 2015-02-28
        • 1970-01-01
        相关资源
        最近更新 更多