Hive and mysql

  1. 使用的数据是气象的数据,使用php写的定时器每15分钟从气象局的网站上抓取全国的数据存入到mysql之中。
  2. 这样的话,要把这数据从mysql导入到hive。用的工具是sqoop
  3. 我用的是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.xml is mainly for the extention of sqoop. I don’t use any of them. so keep it.

Sqoop SQL

将关系型数据的表结构复制到hive中

  1. mysql 存数据的表叫做weather_info 放在一台地址为10.2.213.221上的机器上。
  2. 我的sqoop放在了master上。我把数据放入到hive 的一张叫做weatherinfo里面。

    /usr/sqoop/bin/sqoop create-hive-table 
    --connect jdbc:mysql://10.2.173.221:3306/weather 
    --table weather_info 
    --username root 
    --P 
    --hive-table weatherinfo  
    --fields-terminated-by "\0001"  
    --lines-terminated-by "\n"
    
    1. 如果懒得每次输入密码,把–P换成 –password *
    2. –fields-terminated-by “\0001” 是设置每列之间的分隔符,”\0001”是ASCII码中的1,它也是hive的默认行内分隔符, 而sqoop的默认行内分隔符为”,”
    3. –lines-terminated-by “\n” 设置的是每行之间的分隔符,此处为换行符,也是默认的分隔符;

复制数据

/usr/sqoop/bin/sqoop import 
--connect jdbc:mysql://10.2.173.221:3306/weather 
--table weather_info 
--username root 
--P 
--hive-import
--hive-table weatherinfo 
-m 1 
--fields-terminated-by "\0001"
  1. -m 是由几个map一起做。
  2. –fields-terminated-by “\0001” 需同创建hive表时保持一致;

问题:No primary key could be found for table

  1. 如果原表示没有主键的,只有能采取如下两种方法。

    1. 开一个map来做

      /usr/sqoop/bin/sqoop import 
      --connect jdbc:mysql://10.2.173.221:3306/weather 
      --table weather_info 
      --username root 
      --P 
      --hive-import
      --hive-table weatherinfo 
      -m 1 
      --fields-terminated-by "\0001"
      
    2. 使用--split-by,我的数据里面有一个存入时间(date),可做固定值区分数据。这样的话复制语句就可以写成如下

      /usr/sqoop/bin/sqoop import 
      --connect jdbc:mysql://10.2.173.221: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

  1. 看端口,我报的错是10020。是mapred.site.xml。然后仔细一看发现多写了一个括号。
  2. 9000的话,有可能是/etc/hosts hosts有下划线。这个是不认的。去掉就好了。

问题:Cannot run program “mysqldump”

  1. 这个问题是开多个map的时候出现,因为datanode上没有装mysql。然后没有mysqldump。再每一个机器上都装一个mysql就好了