Sqoop采集mysql数据到HDFS遇到的坑

2017-09-14  本文已影响0人  _Raye

① 要进行数据导出的数据库信息配置脚本(db_xxx.sh)

#!/bin/sh  

# 数据库信息
export username='root'
export password='12345678' 
export database_url='jdbc:mysql://localhost:3306' 
# 数据库名字 
export base_database_name='test'  
# 要导入的表  
export tables=(log) 
# 导入HDFS的目标路径   
export target_dir='/private/tmp/dfs/data' 
# 增量导入监测的列
export monitor_col_array=('time')
# 增量导入各列的last-value值
export last_value_array=('2017-09-05') 

# 环境变量     
source /etc/profile  

② 存放数据库信息配置脚本的一个脚本(dbs.sh)

#!/bin/sh  

# 环境变量     
source /etc/profile 
# 各个数据库配置的数组
export dbs_conf_array=(db_xxx.sh)
 

③ 配置sqoop导入命令的脚本(run.sh)
-- 每行后面的\前有空格,\后面直接回车到下一行
-- 在这里写了循环,便于导入其他数据库时,只需写一个数据库信息配置脚本 再加入dbs.sh

#!/bin/sh
    # 定义一个整形变量      
    a=0
    for table_name in ${tables[*]}  
    do     
sqoop job --create ${table_name} \
-- import --connect ${database_url}/${base_database_name}  \
--username ${username} \
--password ${password} \
--table ${table_name} \ 
--target-dir ${hdfs_target_dir}/${base_database_name}/${table_name} \
--check-column ${monitor_col_array[$a]} \
--incremental append \
--last-value ${last_value[$a]}
      sqoop job --exec ${table_name}
      let a++
    done  

④ 配置最终要运行的脚本(base_import.sh)

#!/bin/sh  
# 执行,run.sh 和当前文件同级  
base_home='/usr/local/Cellar/sqoop/1.4.6/libexec/conf'  
source ${base_home}/dbs.sh

for db_cfg in ${dbs_conf_array[*]}
    do
        source ${base_home}/$db_cfg;
        # 数据库信息
        export username=${username}
        export password=${password}
        export database_url=${database_url}
        # 数据库名字 
        export base_database_name=${base_database_name} 
        # 要导入的表  
        export tables=${tables} 
        # 导入HDFS的目标路径   
        export hdfs_target_dir=${target_dir}
        # 增量导入监测数据库的列
        export monitor_col_array=${monitor_col_array}
        # last-value
        export last_value_array=${last_value_array}

        # 环境变量     
        source /etc/profile 
        # 执行导入  
        source ${base_home}/run.sh  
    done
(1) 无法创建job
问题描述:

把一个sqoop job删除后,再次创建任何job报类似如下错:
ERROR org.apache.sqoop.metastore.hsqldb.HsqldbJobStorage - Cannot restore job

问题原因:

缺省时,Sqoop metastore自动连接存储在~/.sqoop/.目录下的本地嵌入式数据库。然而要在Oozie中执行Sqoop job需要Sqoop使用共享的元数据存储,否则会报上述的错。

解决方案:

在sqoop-site.xml里面,将如下配置的注释去掉(默认配置文件是把这些配置项注释了的)

<!-- 启用metastore(默认value是false) -->
 <property>
    <name>sqoop.metastore.client.enable.autoconnect</name>
    <value>true</value>
    <description>If true, Sqoop will connect to a local metastore
      for job management when no other metastore arguments are
      provided.
    </description>
 </property>

  <property>
    <name>sqoop.metastore.client.autoconnect.url</name>
    <value>jdbc:hsqldb:file:/tmp/sqoop-meta/meta.db;shutdown=true</value>
    <description>The connect string to use when connecting to a
      job-management metastore. If unspecified, uses ~/.sqoop/.
      You can specify a different path here.
    </description>
  </property>
  <property>
    <name>sqoop.metastore.client.autoconnect.username</name>
    <value>SA</value>
    <description>The username to bind to the metastore.
    </description>
  </property>
  <property>
    <name>sqoop.metastore.client.autoconnect.password</name>
    <value></value>
    <description>The password to bind to the metastore.
    </description>
  </property>

  <!-- sqoop执行时会让输入密码,这里value改成true后,会自动保存密码,这样就可以做定时任务(job自动执行) -->
   <property>
    <name>sqoop.metastore.client.record.password</name>
    <value>true</value>
    <description>If true, allow saved passwords in the metastore.
    </description>
  </property>

  <property>
    <name>sqoop.metastore.server.location</name>
    <value>/tmp/sqoop-metastore/shared.db</value>
    <description>Path to the shared metastore database files.
    If this is not set, it will be placed in ~/.sqoop/.
    </description>
  </property>
  <property>
    <name>sqoop.metastore.server.port</name>
    <value>16000</value>
    <description>Port that this metastore should listen on.
    </description>
  </property>
(2) mapreduce执行一半,出现数据库连接超时
问题描述:

在hadoop集群上,执行任务。map完成了一些后,突然报数据库连接超时的错误。然后map还可继续执行,但是任务状态为failed;

问题原因:

考虑到hadoop的mapreduce在执行job时会向各节点分发任务。我之前只对当前节点进行了数据库连接授权,其他节点没授权。

解决方案:

对集群的每个节点进行数据库连接授权

(3) 任务执行到running job时就卡死了
解决方案:

节点的内存不够,CPU也不够。加大了各个节点的CPU和内存。(自己的解决方式,通过Ambari来修改的)

上一篇下一篇

猜你喜欢

热点阅读