void saveThreadDumpPage(File file) throws OozieClientException, IOException { final URL url = new URL(super.getOozieUrl() + "admin/jvminfo.jsp"); final HttpURLConnection retryableConnection = createRetryableConnection(url, "GET"); if ((retryableConnection.getResponseCode() == HttpURLConnection.HTTP_OK)) { try (InputStream is = retryableConnection.getInputStream(); final FileOutputStream os = new FileOutputStream(file)) { IOUtils.copyStream(is, os); } } else { throw new OozieClientException("HTTP error", retryableConnection.getResponseMessage()); } } }
@Override public void close() { try { writer.flush(); } catch (IOException e) { System.err.printf("Could not persist data. Exception: %s%n", e.getMessage()); } finally { IOUtils.closeSafely(writer); } } }
/** * Copy the ssh base and wrapper scripts to the local directory. */ @SuppressFBWarnings(value ="PATH_TRAVERSAL_OUT", justification = "Path is created runtime") private void initSshScripts() { String dirLocation = Services.get().getRuntimeDir() + "/ssh"; File path = new File(dirLocation); path.mkdirs(); if (!path.exists()) { throw new RuntimeException(XLog.format("Not able to create required directory {0}", dirLocation)); } try { IOUtils.copyCharStream(IOUtils.getResourceAsReader("ssh-base.sh", -1), new OutputStreamWriter( new FileOutputStream(dirLocation + "/ssh-base.sh"), Charsets.UTF_8)); IOUtils.copyCharStream(IOUtils.getResourceAsReader("ssh-wrapper.sh", -1), new OutputStreamWriter( new FileOutputStream(dirLocation + "/ssh-wrapper.sh"), Charsets.UTF_8)); } catch (IOException ie) { throw new RuntimeException(XLog.format("Not able to copy required scripts file to {0} " + "for SshActionHandler", dirLocation)); } }
InputStream is = getResourceAsStream(classPath, -1); OutputStream os = new FileOutputStream(new File(dir, classFileName)); copyStream(is, os); zipDir(classesDir, "", zos); return jar;
protected String getBundleXml(String resourceXmlName) { try { Reader reader = IOUtils.getResourceAsReader(resourceXmlName, -1); String appXml = IOUtils.getReaderAsString(reader, -1); return appXml; } catch (IOException ioe) { throw new RuntimeException(XLog.format("Could not get " + resourceXmlName, ioe)); } }
FileSystem fs = getFileSystem(); fs.mkdirs(new Path(wfAppPath, "lib")); File jarFile = IOUtils.createJar(new File(getTestCaseDir()), "test.jar", MapperReducerForTest.class); InputStream is = new FileInputStream(jarFile); OutputStream os = fs.create(new Path(wfAppPath, "lib/test.jar")); IOUtils.copyStream(is, os); Path input = new Path(wfAppPath, "input"); fs.mkdirs(input); writer2.close(); Reader reader = IOUtils.getResourceAsReader("wf-url-template.xml", -1); Writer writer1 = new OutputStreamWriter(fs.create(new Path(wfAppPath, "workflow.xml"))); IOUtils.copyCharStream(reader, writer1);
protected void setCoordConf(Configuration jobConf) throws IOException { Path wfAppPath = new Path(getFsTestCaseDir(), "app"); FileSystem fs = getFileSystem(); fs.mkdirs(new Path(wfAppPath, "lib")); File jarFile = IOUtils.createJar(new File(getTestCaseDir()), "test.jar", MapperReducerForTest.class); InputStream is = new FileInputStream(jarFile); OutputStream os = fs.create(new Path(wfAppPath, "lib/test.jar")); IOUtils.copyStream(is, os); Path input = new Path(wfAppPath, "input"); fs.mkdirs(input); Writer writer = new OutputStreamWriter(fs.create(new Path(input, "test.txt"))); writer.write("hello"); writer.close(); final String APP1 = "<workflow-app xmlns='uri:oozie:workflow:0.1' name='app'>" + "<start to='hadoop'/>" + "<action name=\"hadoop\">" + "<map-reduce>" + "<job-tracker>${jobTracker}</job-tracker>" + "<name-node>${nameNode}</name-node>" + "<configuration>" + "<property><name>mapred.map.tasks</name><value>1</value></property>" + "<property><name>mapred.reduce.tasks</name><value>0</value></property>" + "<property><name>oozie.job.info.testing</name><value>test</value></property>" + "</configuration>" + "</map-reduce>" + "<ok to=\"end\"/>" + "<error to=\"k\"/>" + "</action>" + "<kill name=\"k\">" + "<message>kill</message>" + "</kill><end name=\"end\"/>" + "</workflow-app>"; Writer writer2 = new OutputStreamWriter(fs.create(new Path(wfAppPath, "workflow.xml"))); writer2.write(APP1); writer2.close(); jobConf.set(OozieClient.USER_NAME, getTestUser()); jobConf.set("myJobTracker", getJobTrackerUri()); jobConf.set("myNameNode", getNameNodeUri()); jobConf.set("wfAppPath", new Path(wfAppPath, "workflow.xml").toString()); jobConf.set("mrclass", MapperReducerForTest.class.getName()); }
private void prepareOozieConfDir(String oozieSite, String alternateSiteFile) throws Exception { if (!alternateSiteFile.equals(ConfigurationService.SITE_CONFIG_FILE)) { setSystemProperty(ConfigurationService.OOZIE_CONFIG_FILE, alternateSiteFile); } File siteFile = new File(getTestCaseConfDir(), alternateSiteFile); IOUtils.copyStream(IOUtils.getResourceAsStream(oozieSite, -1), new FileOutputStream(siteFile)); }
@Override public void run() { try { parser.validateAndParse(IOUtils.getResourceAsReader("wf-race-condition.xml", -1), new Configuration()); } catch (Exception e) { error = true; e.printStackTrace(); } done = true; } }
/** * Return a classpath resource as a reader. <p> It is assumed that the resource is a text resource. * * @param path classpath for the resource. * @param maxLen max content length allowed. * @return the reader for the resource. * @throws IOException thrown if the resource could not be read. */ public static Reader getResourceAsReader(String path, int maxLen) throws IOException { return new InputStreamReader(getResourceAsStream(path, maxLen), Charsets.UTF_8); }
public static Properties readProperties(Reader reader, int maxDataLen) throws IOException { String data = IOUtils.getReaderAsString(reader, maxDataLen); return stringToProperties(data); }
/** * Zips a local directory, recursively, into a ZIP stream. * * @param dir directory to ZIP. * @param relativePath basePath in the ZIP for the files, normally "/". * @param zos the ZIP output stream to ZIP the directory. * @throws java.io.IOException thrown if the directory could not be zipped. */ public static void zipDir(File dir, String relativePath, ZipOutputStream zos) throws IOException { zipDir(dir, relativePath, zos, true); zos.close(); }
private boolean tryCreateOnDFS(final String userName, final String appPathWithFileName, final String sourceContent) { try { final URI uri = new URI(appPathWithFileName); final HadoopAccessorService has = Services.get().get(HadoopAccessorService.class); final Configuration fsConf = has.createConfiguration(uri.getAuthority()); final FileSystem dfs = has.createFileSystem(userName, uri, fsConf); final Path path = new Path(uri.getPath()); LOG.debug("HDFS path [{0}] does not exist, will try to create.", path.toString()); try (final FSDataOutputStream target = dfs.create(path)) { LOG.debug("HDFS path [{0}] created.", path.toString()); IOUtils.copyCharStream(new StringReader(sourceContent), new OutputStreamWriter(target, StandardCharsets.UTF_8)); } LOG.debug("XML written to HDFS file [{0}].", path.toString()); return true; } catch (final URISyntaxException | IOException | HadoopAccessorException e) { LOG.warn("Could not write XML [%s] to HDFS. Error message: %s", appPathWithFileName, e.getMessage()); return false; } }
private String _testCoordSubmit(String coordinatorXml, Configuration conf, String inputLogic, String inputEvent, boolean dryRun) throws Exception { String appPath = "file://" + getTestCaseDir() + File.separator + "coordinator.xml"; String content = IOUtils.getResourceAsString(coordinatorXml, -1); content = content.replace("=input-logic=", inputLogic); content = content.replace("=input-events=", inputEvent); Writer writer = new FileWriter(new URI(appPath).getPath()); IOUtils.copyCharStream(new StringReader(content), writer); conf.set(OozieClient.COORDINATOR_APP_PATH, appPath); conf.set(OozieClient.USER_NAME, getTestUser()); conf.set("nameNode", "hdfs://localhost:9000"); conf.set("queueName", "default"); conf.set("jobTracker", "localhost:9001"); conf.set("examplesRoot", "examples"); if (conf.get("A_done_flag") == null) { conf.set("A_done_flag", "_SUCCESS"); } return new CoordSubmitXCommand(dryRun, conf).call(); }
/** * Delete recursively a local directory. * * @param file directory to delete. * @throws IOException thrown if the directory could not be deleted. */ public static void delete(File file) throws IOException { ParamChecker.notNull(file, "file"); if (file.getAbsolutePath().length() < 5) { throw new RuntimeException(XLog.format("Path[{0}] is too short, not deleting", file.getAbsolutePath())); } if (file.exists()) { if (file.isDirectory()) { File[] children = file.listFiles(); if (children != null) { for (File child : children) { delete(child); } } } if (!file.delete()) { throw new RuntimeException(XLog.format("Could not delete path[{0}]", file.getAbsolutePath())); } } }
private void setSystemProps() throws IOException { IOUtils.createJar(new File(getTestCaseDir()), MyOozie.class.getName() + ".jar", MyOozie.class); IOUtils.createJar(new File(getTestCaseDir()), MyPig.class.getName() + ".jar", MyPig.class); IOUtils.createJar(new File(getTestCaseDir()), TestHive.class.getName() + ".jar", TestHive.class); Configuration conf = getOozieConfig(); conf.set(WorkflowAppService.SYSTEM_LIB_PATH, getFsTestCaseDir() + "/share/lib"); conf.set(Services.CONF_SERVICE_CLASSES, conf.get(Services.CONF_SERVICE_CLASSES) + "," + DummyShareLibService.class.getName()); conf.setStrings(ActionService.CONF_ACTION_EXECUTOR_CLASSES, DummyPigActionExecutor.class.getName(), DummyHiveActionExecutor.class.getName()); }
@Override protected String getCoordJobXml(Path appPath) { try { Reader reader = IOUtils.getResourceAsReader("coord-matLookup-trigger.xml", -1); String appXml = IOUtils.getReaderAsString(reader, -1); return appXml; } catch (IOException ioe) { throw new RuntimeException(XLog.format("Could not get coord-matLookup-trigger.xml", ioe)); } } }
FileSystem fs = getFileSystem(); fs.mkdirs(new Path(appPath, "lib")); File jarFile = IOUtils.createJar(new File(getTestCaseDir()), "test.jar", MapperReducerForTest.class); InputStream is = new FileInputStream(jarFile); OutputStream os = fs.create(new Path(appPath, "lib/test.jar")); IOUtils.copyStream(is, os); Path input = new Path(appPath, "input"); Path output = new Path(appPath, "output"); writer2.close(); Reader reader = IOUtils.getResourceAsReader("recovery-wf.xml", -1); Writer writer1 = new OutputStreamWriter(fs.create(new Path(appPath, "workflow.xml"))); IOUtils.copyCharStream(reader, writer1);
InputStream is = getResourceAsStream(classPath, -1); OutputStream os = new FileOutputStream(new File(dir, classFileName)); copyStream(is, os); zipDir(classesDir, "", zos); return jar;
protected Context createContext(String actionXml) throws Exception { DistcpActionExecutor ae = new DistcpActionExecutor(); Path appJarPath = new Path("lib/test.jar"); File jarFile = IOUtils.createJar(new File(getTestCaseDir()), "test.jar", LauncherMainTester.class); InputStream is = new FileInputStream(jarFile); OutputStream os = getFileSystem().create(new Path(getAppPath(), "lib/test.jar")); IOUtils.copyStream(is, os); Path appSoPath = new Path("lib/test.so"); getFileSystem().create(new Path(getAppPath(), appSoPath)).close(); XConfiguration protoConf = new XConfiguration(); protoConf.set(WorkflowAppService.HADOOP_USER, getTestUser()); protoConf.setStrings(WorkflowAppService.APP_LIB_PATH_LIST, appJarPath.toString(), appSoPath.toString()); WorkflowJobBean wf = createBaseWorkflow(protoConf, "action"); WorkflowActionBean action = (WorkflowActionBean) wf.getActions().get(0); action.setType(ae.getType()); action.setConf(actionXml); return new Context(wf, action); }