使用Java中的Sqoop将MYSQL表导入到hdfs

时间:2019-05-15 19:15:55

标签: java mysql eclipse hdfs sqoop

我正在尝试使用JAVA中的独家新闻将表从MYSQL导入HDFS。

我在本地计算机上安装了瓢。
我的系统中有hdfs。
我已经在mysql中创建了数据库和表。 我在pom.xml中添加了sqoop依赖。

我尝试执行以下代码:

//Here I am using a table Persons, with columns PersonID and LastName

import org.apache.sqoop.client.SqoopClient;
import org.apache.sqoop.model.MConnection;
import org.apache.sqoop.model.MConnectionForms;
import org.apache.sqoop.model.MJob;
import org.apache.sqoop.model.MJobForms;
import org.apache.sqoop.model.MSubmission;
import org.apache.sqoop.validation.Status;

/**
 * @author devan
 * @date 19-Sep-2013
 * @mail msdevanms@gmail.com
 */

public class SqoopImport {
  public static void main(String[] args) {


    String connectionString = "jdbc:mysql://YourMysqlIP:3306/test";
    String username = "YourMysqUserName";
    String password = "YourMysqlPassword";
    String schemaName = "YourMysqlDB";
    String tableName = "Persons";
    String columns = "PersonID,LastName"; //comma seperated column names
    String partitionColumn = "PersonID";
    String outputDirectory = "/output/Persons";
    String url = "http://YourSqoopIP:12000/sqoop/";


    SqoopClient client = new SqoopClient(url);
    //client.setServerUrl(newUrl);
    //Dummy connection object
    MConnection newCon = client.newConnection(1);

    //Get connection and framework forms. Set name for connection
    MConnectionForms conForms = newCon.getConnectorPart();
    MConnectionForms frameworkForms = newCon.getFrameworkPart();
    newCon.setName("MyConnection");

    //Set connection forms values
    conForms.getStringInput("connection.connectionString").setValue(connectionString);
    conForms.getStringInput("connection.jdbcDriver").setValue("com.mysql.jdbc.Driver");
    conForms.getStringInput("connection.username").setValue(username);
    conForms.getStringInput("connection.password").setValue(password);

    //frameworkForms.getIntegerInput("security.maxConnections").setValue(0);

    Status status = client.createConnection(newCon);
    if (status.canProceed()) {
      System.out.println("Created. New Connection ID : " + newCon.getPersistenceId());
    } else {
      System.out.println("Check for status and forms error ");
    }

    //Creating dummy job object
    MJob newjob = client.newJob(newCon.getPersistenceId(), org.apache.sqoop.model.MJob.Type.IMPORT);
    MJobForms connectorForm = newjob.getConnectorPart();
    MJobForms frameworkForm = newjob.getFrameworkPart();

    newjob.setName("ImportJob");
    //Database configuration
    connectorForm.getStringInput("table.schemaName").setValue(schemaName);
    //Input either table name or sql
    connectorForm.getStringInput("table.tableName").setValue(tableName);
    //connectorForm.getStringInput("table.sql").setValue("select id,name from table where ${CONDITIONS}");


    connectorForm.getStringInput("table.columns").setValue(columns);
    connectorForm.getStringInput("table.partitionColumn").setValue(partitionColumn);

    //Set boundary value only if required
    //connectorForm.getStringInput("table.boundaryQuery").setValue("");

    //Output configurations
    frameworkForm.getEnumInput("output.storageType").setValue("HDFS");
    frameworkForm.getEnumInput("output.outputFormat").setValue("TEXT_FILE");//Other option: SEQUENCE_FILE / TEXT_FILE
    frameworkForm.getStringInput("output.outputDirectory").setValue(outputDirectory);
    //Job resources
    frameworkForm.getIntegerInput("throttling.extractors").setValue(1);
    frameworkForm.getIntegerInput("throttling.loaders").setValue(1);

    status = client.createJob(newjob);
    if (status.canProceed()) {
      System.out.println("New Job ID: " + newjob.getPersistenceId());
    } else {
      System.out.println("Check for status and forms error ");
    }
    //Now Submit the Job
    MSubmission submission = client.startSubmission(newjob.getPersistenceId());
    System.out.println("Status : " + submission.getStatus());
  }
}

任何帮助将不胜感激。

0 个答案:

没有答案