java - 如何使用 Java 将表从 MySQL 导入到 Hive?

标签 java mysql hadoop sqoop

我正在尝试将表从 MySQL 导入到 Hive。但是,我收到以下错误,您能提供解决方案吗?

SqoopOptions loading .....

Import Tool running ....

14/03/18 06:48:34 WARN sqoop.ConnFactory: $SQOOP_CONF_DIR has not been set in the environment. Cannot check for additional configuration.

14/03/18 06:48:43 INFO mapred.JobClient: SPLIT_RAW_BYTES=87

14/03/18 06:48:43 INFO mapred.JobClient: Map output records=2

14/03/18 06:48:43 INFO mapreduce.ImportJobBase: Transferred 18 bytes in 5.5688 seconds (3.2323 bytes/sec)

14/03/18 06:48:43 INFO mapreduce.ImportJobBase: Retrieved 2 records.

14/03/18 06:48:43 INFO manager.SqlManager: Executing SQL statement: SELECT t.* FROM student AS t WHERE 1=0

14/03/18 06:48:43 INFO manager.SqlManager: Executing SQL statement: SELECT t.* FROM student AS t WHERE 1=0

14/03/18 06:48:43 INFO hive.HiveImport: Loading uploaded data into Hive

WARNING: org.apache.hadoop.metrics.jvm.EventCounter is deprecated. Please use org.apache.hadoop.log.metrics.EventCounter in all the log4j.properties files.

Logging initialized using configuration in jar:file:/home/master/apps/hive-0.10.0/lib/hive-common-0.10.0.jar!/hive-log4j.properties

Hive history file=/tmp/master/hive_job_log_master_201403180648_1860851359.txt

FAILED: Error in metadata: MetaException(message:file:/user/hive/warehouse/student is not a directory or unable to create one)

FAILED: Execution Error, return code 1 from org.apache.hadoop.hive.ql.exec.DDLTask

FAIL !!!

我写的代码:

public class SqoopJavaInterface {
private static final String JOB_NAME = "Sqoop Hive Job";
private static final String MAPREDUCE_JOB = "Hive Map Reduce Job";
private static final String DBURL = "jdbc:mysql://localhost:3306/test";
private static final String DRIVER = "com.mysql.jdbc.Driver";
private static final String USERNAME = "root";
private static final String PASSWORD = "root";
private static final String HADOOP_HOME = "/home/master/apps/hadoop-1.0.4";


private static final String JAR_OUTPUT_DIR = "/home/master/data";
private static final String HIVE_HOME = "/home/master/apps/hive-0.10.0";
private static final String HIVE_DIR = "/user/hive/warehouse/";
private static final String WAREHOUSE_DIR = "hdfs://localhost:9000/user/hive/warehouse/student";
private static final String SUCCESS = "SUCCESS !!!";
private static final String FAIL = "FAIL !!!";

/**
 * @param table
 * @throws IOException
 */
public static void importToHive(String table) throws IOException {
    System.out.println("SqoopOptions loading .....");
    Configuration config = new Configuration();
    // Hive connection parameters
    config.addResource(new Path(HADOOP_HOME+"/conf/core-site.xml"));
    config.addResource(new Path(HADOOP_HOME+"/conf/hdfs-site.xml"));
    config.addResource(new Path(HIVE_HOME+"/conf/hive-site.xml"));
    FileSystem dfs =FileSystem.get(config);
    /* MySQL connection parameters */
    SqoopOptions options = new SqoopOptions(config);
    options.setConnectString(DBURL);
    options.setTableName(table);
    options.setDriverClassName(DRIVER);
    options.setUsername(USERNAME);
    options.setPassword(PASSWORD);
    options.setHadoopMapRedHome(HADOOP_HOME);
    options.setHiveHome(HIVE_HOME);
    options.setHiveImport(true);
    options.setHiveTableName(table);
    options.setOverwriteHiveTable(true);
    options.setFailIfHiveTableExists(false);
    options.setFieldsTerminatedBy(',');
    options.setOverwriteHiveTable(true);
    options.setDirectMode(true);
    options.setNumMappers(1); // No. of Mappers to be launched for the job
    options.setWarehouseDir(WAREHOUSE_DIR);
    options.setJobName(JOB_NAME);
    options.setMapreduceJobName(MAPREDUCE_JOB);
    options.setTableName(table);
    options.setJarOutputDir(JAR_OUTPUT_DIR);
    System.out.println("Import Tool running ....");
    ImportTool it = new ImportTool();
    int retVal = it.run(options);
    if (retVal == 0) {
        System.out.println(SUCCESS);
    } else {
        System.out.println(FAIL);
    }
    
}

当我执行上面的代码时,出现以下错误。你能为此提供解决方案吗?

Execution failed while executing command: 192.168.10.172
Error message: bash: 192.168.10.172: command not found
Now wait 5 seconds to begin next task ...
Connection channel disconnect
net.neoremind.sshxcute.core.Result@60c2be20
Command is sqoop import --connect jdbc:mysql://localhost:3316/hadoop --username root --password root --table employees --hive-import -m 1 -- --schema default
Connection channel established succesfully
Start to run command
Connection channel closed
Check if exec success or not ... 
Execution failed while executing command: sqoop import --connect jdbc:mysql://localhost:3316/hadoop --username root --password root --table employees --hive-import -m 1 -- --schema default
Error message: bash: sqoop: command not found
Now wait 5 seconds to begin next task ...
Connection channel disconnect
SSH connection shutdown

最佳答案

由于 sqoop 选项方法已弃用,您可以使用以下代码:

public static void importToHive() throws Exception{

    Configuration config = new Configuration(); 
    config.addResource(new Path("/usr/local/hadoop/conf/core-site.xml"));
    config.addResource(new Path("/usr/local/hadoop/conf/hdfs-site.xml"));
    String[] cmd ={"import", "--connect",<connectionString>,"--username", userName,
     "--password", password,"--hadoop-home", "/usr/local/hadoop","--table",<tableName>,   "--hive-import","--create-hive-table", "--hive-table",<tableName>,"-target-dir",
           "hdfs://localhost:54310/user/hive/warehouse","-m", "1","--delete-target-dir"};

    Sqoop.runTool(cmd,config);
}

mysql请使用正确的hadoop和hive仓库路径用户名密码。请从 core-site.xml 检查您的端口(在我的例子中是 54310)

关于java - 如何使用 Java 将表从 MySQL 导入到 Hive?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22487688/

相关文章:

java - 如何使用 JPA 处理多个父类型?

hadoop - HIVE中的NULL检查

java - "Shortcut"在reduce()方法中确定Iterator<IntWritable>中的最大元素

java - getClass().getResource() 指向什么?

Java替换所有字符串在所有单词周围添加引号但忽略句点

java - 无法获取在主机上上传的路径。 jsp

c# - MVC组合框以及如何将其保存在数据库中?

mysql - 如何在单个查询中进行多个求和

hadoop - 将 avro avdl 文件共享到多个模块

java - 具有相同名称的Android gradle模块