Monday, January 5, 2015

Sqoop2 client 1.99.4

There are several changes added in 1.99.4 version.
So this may helpful to you.


import org.apache.sqoop.client.SqoopClient;
import org.apache.sqoop.model.MFromConfig;
import org.apache.sqoop.model.MJob;
import org.apache.sqoop.model.MLink;
import org.apache.sqoop.model.MLinkConfig;
import org.apache.sqoop.model.MSubmission;
import org.apache.sqoop.model.MToConfig;
import org.apache.sqoop.submission.counter.Counter;
import org.apache.sqoop.submission.counter.CounterGroup;
import org.apache.sqoop.submission.counter.Counters;
import org.apache.sqoop.validation.Status;

public class MysqlToHDFS {
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 partitionColumn = "PersonID";
String outputDirectory = "/output/Persons";
String url = "http://YourSqoopIP:12000/sqoop/";
String hdfsURI = "hdfs://namenodeIP:8020/";
SqoopClient client = new SqoopClient(url);
long fromConnectorId = 2;
MLink fromLink = client.createLink(fromConnectorId);
fromLink.setName("JDBC connector1");
fromLink.setCreationUser("devan");
MLinkConfig fromLinkConfig = fromLink.getConnectorLinkConfig();
fromLinkConfig.getStringInput("linkConfig.connectionString").setValue(connectionString);
fromLinkConfig.getStringInput("linkConfig.jdbcDriver").setValue("com.mysql.jdbc.Driver");
fromLinkConfig.getStringInput("linkConfig.username").setValue(username);
fromLinkConfig.getStringInput("linkConfig.password").setValue(password);
Status fromStatus = client.saveLink(fromLink);
if (fromStatus.canProceed()) {
System.out.println("JDBC Link,ID : " + fromLink.getPersistenceId());
} else {
System.out.println("JDBC Link");
}
// create HDFS connector
long toConnectorId = 1;
MLink toLink = client.createLink(toConnectorId);
toLink.setName("HDFS connector");
toLink.setCreationUser("devan");
MLinkConfig toLinkConfig = toLink.getConnectorLinkConfig();
toLinkConfig.getStringInput("linkConfig.uri").setValue(hdfsURI);
Status toStatus = client.saveLink(toLink);
if (toStatus.canProceed()) {
System.out.println("HDFS Link,ID: " + toLink.getPersistenceId());
} else {
System.out.println("HDFS Link");
}
long fromLinkId = fromLink.getPersistenceId();
long toLinkId = toLink.getPersistenceId();
MJob job = client.createJob(fromLinkId, toLinkId);//create job with jdbc and hdfs links
job.setName("MySQL to HDFS job");
job.setCreationUser("devan");
MFromConfig fromJobConfig = job.getFromJobConfig();
fromJobConfig.getStringInput("fromJobConfig.schemaName").setValue(schemaName);
fromJobConfig.getStringInput("fromJobConfig.tableName").setValue(tableName);
fromJobConfig.getStringInput("fromJobConfig.partitionColumn").setValue(partitionColumn);
MToConfig toJobConfig = job.getToJobConfig();
toJobConfig.getStringInput("toJobConfig.outputDirectory").setValue(outputDirectory);
Status status = client.saveJob(job);
if (status.canProceed()) {
System.out.println("JOB,ID: " + job.getPersistenceId());
} else {
System.out.println("Job can't be created");
}
long jobId = job.getPersistenceId();
MSubmission submission = client.startJob(jobId);
System.out.println("JOB : " + submission.getStatus());
while (submission.getStatus().isRunning()
&& submission.getProgress() != -1) {
System.out.println("JOB: "
+ String.format("%.2f %%", submission.getProgress() * 100));
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.println("JOB... ...");
System.out.println("Hadoop :" + submission.getExternalId());
Counters counters = submission.getCounters();
if (counters != null) {
System.out.println(":");
for (CounterGroup group : counters) {
System.out.print("\t");
System.out.println(group.getName());
for (Counter counter : group) {
System.out.print("\t\t");
System.out.print(counter.getName());
System.out.print(": ");
System.out.println(counter.getValue());
}
}
}
if (submission.getExceptionInfo() != null) {
System.out.println("JOB : " + submission.getExceptionInfo());
}
System.out.println("sqoop job successfully submitted");
}
}

==========================================================
If you are creating one maven project, add the following as dependency.

 <dependency>
<groupId>org.apache.sqoop</groupId>
<artifactId>sqoop-client</artifactId>
<version>1.99.4</version>
</dependency>

No comments:

Post a Comment

Total Pageviews

Translate