Big Data Handbook (2): Migrating MySQL Data into Hive
Original Chinese title: Bigdata 手册(2)-mysql 数据迁入hive
Hive and MySQL
- The dataset is weather data: a scheduled job written in PHP scrapes nationwide data from the meteorological bureau’s website every 15 minutes and saves it into MySQL.
- So we need to import this data from MySQL into Hive. The tool used is Sqoop.
- I’m using version 1.4 of sqoop.
Sqoop Configuration
#Set path to where bin/hadoop is available
export HADOOP_COMMON_HOME=/usr/hadoop/
#Set path to where hadoop-*-core.jar is available
export HADOOP_MAPRED_HOME=/usr/hadoop
#set the path to where bin/hbase is available
export HBASE_HOME=/usr/hbase
#Set the path to where bin/hive is available
export HIVE_HOME=/usr/hive
#Set the path for where zookeper config dir is
#export ZOOCFGDIR=/usr/local/zk
- Since I’m using the default ZooKeeper setup, I didn’t configure ZooKeeper in Sqoop here.
- The configuration in
sqoop-site.xmlis mainly for Sqoop extensions. I don’t use any of them, so leave it as is.
Sqoop SQL
Copying a Relational Table Schema into Hive
-
The MySQL table storing the data is called weather_info, hosted on a machine at 192.0.2.10.
-
My Sqoop installation is on the master node. I’m loading the data into a Hive table called weatherinfo.
/usr/sqoop/bin/sqoop create-hive-table --connect jdbc:mysql://192.0.2.10:3306/weather --table weather_info --username root --P --hive-table weatherinfo --fields-terminated-by "\0001" --lines-terminated-by "\n"- If you don’t want to enter the password every time, replace –P with –password *.
- –fields-terminated-by “\0001” sets the delimiter between columns. “\0001” is ASCII character 1, which is also Hive’s default field delimiter, whereas Sqoop’s default field delimiter is ”,”.
- –lines-terminated-by “\n” sets the delimiter between lines—here a newline character, which is also the default.
Copying Data
/usr/sqoop/bin/sqoop import
--connect jdbc:mysql://192.0.2.10:3306/weather
--table weather_info
--username root
--P
--hive-import
--hive-table weatherinfo
-m 1
--fields-terminated-by "\0001"
- -m specifies how many mappers run in parallel.
- –fields-terminated-by “\0001” must match what was used when creating the Hive table.
Issue: No primary key could be found for table
-
If the source table has no primary key, you only have two options:
-
Run with a single mapper:
/usr/sqoop/bin/sqoop import --connect jdbc:mysql://192.0.2.10:3306/weather --table weather_info --username root --P --hive-import --hive-table weatherinfo -m 1 --fields-terminated-by "\0001" -
Use
--split-by. My data has an ingestion timestamp (date) column that can be used to partition the data. In that case, the import command can be written as follows:/usr/sqoop/bin/sqoop import --connect jdbc:mysql://192.0.2.10:3306/weather --table weather_info --username root --P --hive-import --hive-table weatherinfo -m 5 --fields-terminated-by "\0001" --direct --split-by date
-
Issue: Does not contain a valid host:port authority
- Check the port—my error was on 10020, which came from mapred.site.xml. Looking closer, I had accidentally typed an extra bracket.
- If it’s port 9000, there might be an underscore in
/etc/hostshostnames. That isn’t accepted—just remove it.
Issue: Cannot run program “mysqldump”
- This happens when running multiple mappers because MySQL isn’t installed on the DataNodes, so
mysqldumpis missing. Just install MySQL on every machine.
Hive and mysql
- 使用的数据是气象的数据,使用php写的定时器每15分钟从气象局的网站上抓取全国的数据存入到mysql之中。
- 这样的话,要把这数据从mysql导入到hive。用的工具是sqoop
- 我用的是1.4 的sqoop;
sqoop 配置
#Set path to where bin/hadoop is available
export HADOOP_COMMON_HOME=/usr/hadoop/
#Set path to where hadoop-*-core.jar is available
export HADOOP_MAPRED_HOME=/usr/hadoop
#set the path to where bin/hbase is available
export HBASE_HOME=/usr/hbase
#Set the path to where bin/hive is available
export HIVE_HOME=/usr/hive
#Set the path for where zookeper config dir is
#export ZOOCFGDIR=/usr/local/zk
- 我的zookeper用的是默认的,这里就没有让sqoop配置了zookeper
- the conf of
sqoop-site.xmlis mainly for the extention of sqoop. I don’t use any of them. so keep it.
Sqoop SQL
将关系型数据的表结构复制到hive中
-
mysql 存数据的表叫做weather_info 放在一台地址为192.0.2.10上的机器上。
-
我的sqoop放在了master上。我把数据放入到hive 的一张叫做weatherinfo里面。
/usr/sqoop/bin/sqoop create-hive-table --connect jdbc:mysql://192.0.2.10:3306/weather --table weather_info --username root --P --hive-table weatherinfo --fields-terminated-by "\0001" --lines-terminated-by "\n"- 如果懒得每次输入密码,把–P换成 –password *
- –fields-terminated-by “\0001” 是设置每列之间的分隔符,”\0001”是ASCII码中的1,它也是hive的默认行内分隔符, 而sqoop的默认行内分隔符为”,”
- –lines-terminated-by “\n” 设置的是每行之间的分隔符,此处为换行符,也是默认的分隔符;
复制数据
/usr/sqoop/bin/sqoop import
--connect jdbc:mysql://192.0.2.10:3306/weather
--table weather_info
--username root
--P
--hive-import
--hive-table weatherinfo
-m 1
--fields-terminated-by "\0001"
- -m 是由几个map一起做。
- –fields-terminated-by “\0001” 需同创建hive表时保持一致;
问题:No primary key could be found for table
-
如果原表示没有主键的,只有能采取如下两种方法。
-
开一个map来做
/usr/sqoop/bin/sqoop import --connect jdbc:mysql://192.0.2.10:3306/weather --table weather_info --username root --P --hive-import --hive-table weatherinfo -m 1 --fields-terminated-by "\0001" -
使用
--split-by,我的数据里面有一个存入时间(date),可做固定值区分数据。这样的话复制语句就可以写成如下/usr/sqoop/bin/sqoop import --connect jdbc:mysql://192.0.2.10:3306/weather --table weather_info --username root --P --hive-import --hive-table weatherinfo -m 5 --fields-terminated-by "\0001" --direct --split-by date
-
问题:Does not contain a valid host:port authority
- 看端口,我报的错是10020。是mapred.site.xml。然后仔细一看发现多写了一个括号。
- 9000的话,有可能是/etc/hosts hosts有下划线。这个是不认的。去掉就好了。
问题:Cannot run program “mysqldump”
- 这个问题是开多个map的时候出现,因为datanode上没有装mysql。然后没有mysqldump。再每一个机器上都装一个mysql就好了