如何将 HDFS 输出存储到 MySQL table?
How to store HDFS output to MySQL table?
我使用 Pig 和 Hive 对存储在 HDFS 中的数据集进行 MapReduce 操作。现在我想传输该输出以将其存储到 MySQL table 中。
如何将输出传输到 MySQL?
您可以利用 Apache Sqoop
从 HDFS
导出到 MySQL
。
插图:
这是HDFS中的数据
# hadoop fs -ls /example_hive
/example_hive/file1.csv
# hadoop fs -cat /example_hive/*
1,foo
2,bar
3,ack
4,irk
5,pqr
在MySQLtest
数据库
中创建目标table
> create table test.example_mysql(h1 int, h2 varchar(100));
使用 Sqoop 命令导出。 (根据您的环境更新参数值 --connect、--username、--password)
# sqoop export --connect "jdbc:mysql://localhost/test" --username "root" --password hadoop --table "example_mysql" --export-dir "hdfs:///example_hive" --input-fields-terminated-by ','
查看MySQL
中的数据
> select * from test.example_mysql;
+------+------+
| h1 | h2 |
+------+------+
| 1 | foo |
| 2 | bar |
| 3 | ack |
| 4 | irk |
| 5 | pqr |
+------+------+
我使用 Pig 和 Hive 对存储在 HDFS 中的数据集进行 MapReduce 操作。现在我想传输该输出以将其存储到 MySQL table 中。
如何将输出传输到 MySQL?
您可以利用 Apache Sqoop
从 HDFS
导出到 MySQL
。
插图:
这是HDFS中的数据
# hadoop fs -ls /example_hive
/example_hive/file1.csv
# hadoop fs -cat /example_hive/*
1,foo
2,bar
3,ack
4,irk
5,pqr
在MySQLtest
数据库
> create table test.example_mysql(h1 int, h2 varchar(100));
使用 Sqoop 命令导出。 (根据您的环境更新参数值 --connect、--username、--password)
# sqoop export --connect "jdbc:mysql://localhost/test" --username "root" --password hadoop --table "example_mysql" --export-dir "hdfs:///example_hive" --input-fields-terminated-by ','
查看MySQL
中的数据> select * from test.example_mysql;
+------+------+
| h1 | h2 |
+------+------+
| 1 | foo |
| 2 | bar |
| 3 | ack |
| 4 | irk |
| 5 | pqr |
+------+------+