Legacy Archive (2015): Originally written in Chinese on doubixie.com and translated into English for this edition. All archive posts · Home

Big Data Handbook (2): Migrating MySQL Data into Hive

Original Chinese title: Bigdata 手册(2)-mysql 数据迁入hive

Hive and MySQL

  1. 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.
  2. So we need to import this data from MySQL into Hive. The tool used is Sqoop.
  3. 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.xml is 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

  1. The MySQL table storing the data is called weather_info, hosted on a machine at 192.0.2.10.

  2. 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"
    1. If you don’t want to enter the password every time, replace –P with –password *.
    2. –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 ”,”.
    3. –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"
  1. -m specifies how many mappers run in parallel.
  2. –fields-terminated-by “\0001” must match what was used when creating the Hive table.

Issue: No primary key could be found for table

  1. If the source table has no primary key, you only have two options:

    1. 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"
    2. 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

  1. Check the port—my error was on 10020, which came from mapred.site.xml. Looking closer, I had accidentally typed an extra bracket.
  2. If it’s port 9000, there might be an underscore in /etc/hosts hostnames. That isn’t accepted—just remove it.

Issue: Cannot run program “mysqldump”

  1. This happens when running multiple mappers because MySQL isn’t installed on the DataNodes, so mysqldump is missing. Just install MySQL on every machine.