hadoop api提供了一些遍历文件的api,通过该api可以实现遍历文件目录:

import java.io.FileNotFoundException;
import java.io.IOException;
import java.net.URI;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.CountDownLatch; import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileStatus;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path; public class BatchSubmitMain {
public static void main(String[] args) throws Exception {
String mrTableName = args[0];
String fglibTableName = args[1]; Configuration conf = new Configuration();
/*
* <property> <name>fs.defaultFS</name> <value>hdfs://hcluster</value>
* </property>
*/
conf.set("fs.defaultFS", "hdfs://hcluster");
FileSystem fileSystem = FileSystem.get(conf); String mrFilePath = "/myuser/hivedb/" + mrTableName;
String fglibFilePath = "/myuser/hivedb/" + fglibTableName; System.out.println(mrFilePath);
List<String> mrObjectIdItems = getObjectIdItems(fileSystem, mrFilePath); System.out.println(fglibFilePath);
List<String> fglibObjectIdItems = getObjectIdItems(fileSystem, fglibFilePath); List<String> objectIdItems = new ArrayList<>(); for (String mrObjectId : mrObjectIdItems) {
for (String fglibObjectId : fglibObjectIdItems) {
if (mrObjectId == fglibObjectId) {
objectIdItems.add(mrObjectId);
}
}
} String submitShPath = "/app/myaccount/service/submitsparkjob.sh"; CountDownLatch threadSignal = new CountDownLatch(objectIdItems.size()); for (int ii = 0; ii < objectIdItems.size(); ii++) {
String objectId = objectIdItems.get(ii);
Thread thread = new ImportThread(objectId, submitShPath, threadSignal);
thread.start();
} threadSignal.await(); System.out.println(Thread.currentThread().getName() + "complete");
} private static List<String> getObjectIdItems(FileSystem fileSystem, String filePath) throws FileNotFoundException, IOException {
List<String> objectItems = new ArrayList<>(); Path path = new Path(filePath);
// 获取文件列表
FileStatus[] files = fileSystem.listStatus(path);
// 展示文件信息
for (int i = 0; i < files.length; i++) {
try {
if (files[i].isDirectory()) {
String[] fileItems = files[i].getPath().getName().split("/");
String objectId = fileItems[fileItems.length - 1].replace("objectid=", "");
objectItems.add(objectId);
System.out.println(objectId);
}
} catch (Exception e) {
e.printStackTrace();
}
} return objectItems;
} /**
* @param hdfs
* FileSystem 对象
* @param path
* 文件路径
*/
public static void iteratorShowFiles(FileSystem hdfs, Path path) {
try {
if (hdfs == null || path == null) {
return;
} // 获取文件列表
FileStatus[] files = hdfs.listStatus(path); // 展示文件信息
for (int i = 0; i < files.length; i++) {
try {
if (files[i].isDirectory()) {
System.out.print(">>>" + files[i].getPath() + ", dir owner:" + files[i].getOwner());
// 递归调用
iteratorShowFiles(hdfs, files[i].getPath());
} else if (files[i].isFile()) {
System.out.print(" " + files[i].getPath() + ",length:" + files[i].getLen() + ", owner:" + files[i].getOwner());
}
} catch (Exception e) {
e.printStackTrace();
}
}
} catch (Exception e) {
e.printStackTrace();
}
} }

并行执行sh的线程:

import java.util.concurrent.CountDownLatch;

public class ImportThread extends Thread {
private final JavaShellInvoker javaShellInvoker = new JavaShellInvoker(); private CountDownLatch countDownLatch;
private String objectId;
private String submitShPath; public ImportThread(String objectId, String submitShPath, CountDownLatch countDownLatch) {
this.objectId = objectId;
this.submitShPath = submitShPath;
this.countDownLatch = countDownLatch;
} @Override
public void run() {
System.out.println(Thread.currentThread().getName() + "start... " + this.submitShPath + " " + this.objectId.toString());// 打印开始标记 try {
int result = this.javaShellInvoker.executeShell("mrraster", this.submitShPath, this.objectId);
if (result != 0) {
System.out.println(Thread.currentThread().getName() + " result type is error");
}
} catch (Exception e) {
e.printStackTrace();
System.out.println(Thread.currentThread().getName() + "-error:" + e.getMessage());
} this.countDownLatch.countDown();// 计时器减1
System.out.println(Thread.currentThread().getName() + " complete,last " + this.countDownLatch.getCount() + " threads");// 打印结束标记
}
}

执行sh的java代码:

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date; public class JavaShellInvoker {
private static final String executeShellLogFile = "./executeShell_%s_%s.log"; public int executeShell(String shellCommandType, String shellCommand, String args) throws Exception {
int success = 0; args = (args == null) ? "" : args; String now = new SimpleDateFormat("yyyy-MM-dd").format(new Date());
File logFile = new File(String.format(executeShellLogFile, shellCommandType, now)); ProcessBuilder pb = new ProcessBuilder("sh", shellCommand, args);
pb.redirectOutput(ProcessBuilder.Redirect.appendTo(logFile));
pb.redirectError(ProcessBuilder.Redirect.appendTo(logFile)); Process pid = null; try {
pid = pb.start();
success = pid.waitFor();
} catch (Exception ex) {
success = 2;
System.out.println("executeShell-error:" + ex.getMessage());
throw ex;
} finally {
if (pid.isAlive()) {
success = pid.exitValue();
pid.destroy();
}
} return success;
}
}

submitsparkjob.sh

#!/bin/sh
source ../login.sh
spark-submit --master yarn-cluster --class MySparkJobMainClass --driver-class-path /app/myaccount/service/jars/ojdbc7.jar --jars /app/myaccount/service/jars/ojdbc7.jar --num-executors
20 --driver-memory 6g --executor-cores 1 --executor-memory 8g MySparkJobJar.jar $1

执行BatchSubmit.jar的命令:

hadoop jar BatchSubmit.jar

Hadoop API:遍历文件分区目录,并根据目录下的数据进行并行提交spark任务的更多相关文章

  1. 遍历文件夹及其子文件夹下的.pdf文件,并解压文件夹下所有的压缩包

    List<PDFPATH> pdfpath = new List<PDFPATH>(); List<string> ziplist = new List<st ...

  2. BAT 遍历文件夹和子文件夹下所有文件

    echo off & color 0A ::指定起始文件夹 set DIR="%cd%" echo DIR=%DIR% :: 参数 /R 表示需要遍历子文件夹,去掉表示不遍 ...

  3. windowsAPI遍历文件夹(速度高于递归)

    #region API 遍历文件夹及其子文件夹和子文件 #region 声明WIN32API函数以及结构 ************************************** [DllImpo ...

  4. C++下遍历文件夹

    编写程序遍历文件夹及其子文件夹下所有文件,并输出到标准输出流或者文件流. 1. 先考虑在单层目录下,遍历所有文件.以C:\WINDOWS为例: 用到数据结构_finddata_t,文件信息结构体的指针 ...

  5. 使用Hadoop API 压缩HDFS文件

    下篇解压缩:使用Hadoop API 解压缩 HDFS文件 起因: 集群磁盘剩余空间不足. 删除了存储在HDFS上的,一定时间之前的中间结果,发现并不能释放太多空间,查看计算业务,发现,每天的日志存在 ...

  6. MFC拖拽、选择目录、遍历文件

    1.选择目录 void CDecryptFileDlg::OnBnClickedSel() { std::wstring selectedDir; WCHAR szDir[MAX_PATH]; Zer ...

  7. 使用Hadoop API 解压缩 HDFS文件

    接上篇:使用Hadoop API 压缩HDFS文件 压缩完了,当然需要解压缩了. 直接上代码: private static void getFile(String filePath) throws ...

  8. 基于 java 【Web安全】文件上传漏洞及目录遍历攻击

    前言:web安全之文件上传漏洞,顺带讲一下目录遍历攻击.本文基于 java 写了一个示例. 原理 在上网的过程中,我们经常会将一些如图片.压缩包之类的文件上传到远端服务器进行保存.文件上传攻击指的是恶 ...

  9. MFC_选择目录对话框_选择文件对话框_指定目录遍历文件

    选择目录对话框 void C资源共享吧视频广告清理工具Dlg::OnBnClickedCls() { // 清空编辑框内容 m_Edit.SetWindowTextW(L""); ...

随机推荐

  1. NGUI_Toggle

    七.Toggle复选框:就是对一个选项做上一个标记,表示这个选项已经被选中了. 1.当我们要判断是否使用复选框,可以遵循一下规律: (1).该功能只有两种状态  是/否 (2).该功能同一时间只能激活 ...

  2. Eclipse 基础操作与设置

    1.快捷键 ctrl+F 在某个文档里搜索对应字段 ctrl+H 全文件查询对应字段 ctrl +shift +R 快速查找某个java类 ctrl +shift +O 自动导入需要的包,删除没用过的 ...

  3. 【Python】excel读写操作 xlrd & xlwt

    xlrd ■ xlrd xlrd模块用于读取excel文件内容 基本用法: workbook = xlrd.open_workbook('文件路径') workbook.sheet_names() # ...

  4. saltStack 安装部署

    1.saltStack 服务架构介绍 SaltStack 是一种基于C/S架构的服务模式,在SaltStack架构中服务器端叫作Master,客户端叫作Minion,传统C/S架构为:客户端发送请求给 ...

  5. CodeBlocks使用介绍

    CodeBlocks对单个文件没法调试,需要建工程后才能调试,所以使用CodeBlocks必须先建工程. 一.如何用CodeBlocks建工程文件. 选择File->New->Projec ...

  6. Alpha第十天

    Alpha第十天 听说 031502543 周龙荣(队长) 031502615 李家鹏 031502632 伍晨薇 031502637 张柽 031502639 郑秦 1.前言 任务分配是VV.ZQ. ...

  7. PTA題目的處理(一)

    **題目1:A乘B** **實驗代碼** #include <stdio.h> #include <stdlib.h> int main() { signed int a,b; ...

  8. C语言——第六周作业

    题目 题目一:高速公路超速处罚 1.实验代码 #include <stdio.h> int main() { int speed,maxspeed; double x; scanf(&qu ...

  9. 静态关键字static用法。

    static的特点:1,static是一个修饰符,用于修饰成员.2,static修饰的成员被所有的对象所共享.3,static优先于对象存在,因为static的成员随着类的加载就已经存在了. 4,st ...

  10. bzoj 4399 魔法少女LJJ

    4399: 魔法少女LJJ Time Limit: 20 Sec  Memory Limit: 162 MBhttp://www.lydsy.com/JudgeOnline/problem.php?i ...