欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

sqoop api

程序员文章站 2022-05-25 14:21:38
...
logger.info("start to export data");
// 开始时间
Long beginTime = System.currentTimeMillis();
try{
System.setProperty("HADOOP_USER_NAME","hddtmn");
Configuration conf = new Configuration();
conf.set("fs.default.name", HDFS);
//conf.set("hadoop.job.ugi", "hadoop,hadoop");
conf.set("mapred.job.tracker", JOB_TRACKER);

//运行sqoop程序需要额外jar支持
addTmpJar(jarDir,conf);
//addTmpJar("file:/D:/hadoop/sqoop-1.4.5.jar",conf);

String[] arg = new String[1];

ExportTool exporter = new ExportTool();
Sqoop sqoop = new Sqoop(exporter);

sqoop.setConf(conf);

//多个文件执行
FileSystem fs = FileSystem.get(URI.create(HDFS), conf);
Path path = new Path(hdfsDir);
FileStatus[] FileStatus = fs.listStatus(path);
for (FileStatus fileStatus : FileStatus) {
String type = fileStatus.isDir() ? "dir" : "file";
String permission = fileStatus.getPermission().toString();
short replication = fileStatus.getReplication();
String group = fileStatus.getGroup();
long len = fileStatus.getLen();
String filepath = fileStatus.getPath().toString();
logger.info(type + "\t" + replication + "\t"+ permission + "\t" + group + "\t" + len + "\t" + filepath + "\t");
if(filepath.contains("part-r")){
List<String> para = getSqoopPara(filepath, tableName, columns, splitStr);
arg = para.toArray(new String[0]);
Sqoop.runSqoop(sqoop, arg);
}
}
}catch(Exception e){
e.printStackTrace();
logger.info(e.getMessage());
}
// 结束时间
Long endTime = System.currentTimeMillis();
logger.info("总共:" + (endTime - beginTime) / 1000 + "秒");// 计算时间