将多个数据库中的表导入 Hadoop 和 Union

Importing tables from multiple databases into Hadoop and Union

我有这个具体场景:

SQL 服务器中有按年命名的数据库,命名方式类似于 "FOOXXYY",其中 XXYY 表示财政年度。现在我想从所有这些数据库中获取一个特定的 table "bar",将其联合到配置单元中的单个 table 并将其存储到 HDFS 中。

最好和最快的方法是什么?

您需要创建数据库、创建分区 table、添加分区、运行 4 个不同的 sqoop 命令来连接到每个数据库并将数据加载到分区中。以下是示例代码片段。

像这样创建数据库然后分区table;

CREATE TABLE `order_items`(
  `order_item_id` int, 
  `order_item_order_id` int, 
  `order_item_order_date` string, 
  `order_item_product_id` int, 
  `order_item_quantity` smallint, 
  `order_item_subtotal` float, 
  `order_item_product_price` float)
PARTITIONED BY ( 
  `order_month` string)
ROW FORMAT DELIMITED 
  FIELDS TERMINATED BY '|';

然后您可以使用这些命令添加分区:

alter table order_items add partition (order_month=201301);
alter table order_items add partition (order_month=201302);

创建 table 后,您可以 运行 describe formatted order_items。它将给出 table 的路径,您可以在 hive 中使用 dfs -ls 命令进行验证。

From describe formatted
Location:               hdfs://sandbox.hortonworks.com:8020/apps/hive/warehouse/retail_ods.db/order_items

dfs -ls /apps/hive/warehouse/retail_ods.db/order_items

你会得到2个目录,抓取路径。

现在你有 table 和每年的分区(针对你的情况)。现在您可以对每个数据库使用 sqoop import 命令从 table 查询并复制到相应的分区。

您可以找到示例 sqoop 命令 here。您甚至可以将查询作为 sqoop 导入命令的一部分传递(Google sqoop 用户指南)。

sqoop import \
  --connect "jdbc:mysql://sandbox.hortonworks.com:3306/retail_db" \
  --username=retail_dba \
  --password=hadoop \
  --table order_items \
  --target-dir /apps/hive/warehouse/retail_ods.db/order_items/order_month=201301 \
  --append \
  --fields-terminated-by '|' \
  --lines-terminated-by '\n' \
  --outdir java_files