List of usage examples for org.apache.hadoop.io IOUtils closeStream
public static void closeStream(java.io.Closeable stream)
From source file:org.apache.slider.core.persist.JsonSerDeser.java
License:Apache License
/** * Convert from a JSON file/* w ww . ja va 2 s . com*/ * @param resource input file * @return the parsed JSON * @throws IOException IO problems * @throws JsonMappingException failure to map from the JSON to this class */ @SuppressWarnings("IOResourceOpenedButNotSafelyClosed") public synchronized T fromResource(String resource) throws IOException, JsonParseException, JsonMappingException { InputStream resStream = null; try { resStream = this.getClass().getResourceAsStream(resource); if (resStream == null) { throw new FileNotFoundException(resource); } return (T) (mapper.readValue(resStream, classType)); } catch (IOException e) { log.error("Exception while parsing json resource {}: {}", resource, e); throw e; } finally { IOUtils.closeStream(resStream); } }
From source file:org.apache.slider.core.persist.JsonSerDeser.java
License:Apache License
/** * Convert from an input stream, closing the stream afterwards. * @param stream/* w w w . j a v a 2s.com*/ * @return the parsed JSON * @throws IOException IO problems */ public T fromStream(InputStream stream) throws IOException { try { return (T) (mapper.readValue(stream, classType)); } catch (IOException e) { log.error("Exception while parsing json input stream: {}", e); throw e; } finally { IOUtils.closeStream(stream); } }
From source file:org.apache.slider.core.persist.JsonSerDeser.java
License:Apache License
/** * Write the json as bytes -then close the file * @param dataOutputStream an outout stream that will always be closed * @throws IOException on any failure/*from www . ja va 2s . c o m*/ */ private void writeJsonAsBytes(T instance, OutputStream dataOutputStream) throws IOException { try { String json = toJson(instance); byte[] b = json.getBytes(UTF_8); dataOutputStream.write(b); dataOutputStream.flush(); dataOutputStream.close(); } finally { IOUtils.closeStream(dataOutputStream); } }
From source file:org.apache.slider.core.zk.MiniZooKeeperCluster.java
License:Apache License
private static boolean waitForServerUp(int port, long timeout) throws InterruptedException { long start = System.currentTimeMillis(); while (true) { try {/*from w w w . j av a2 s. c o m*/ Socket sock = null; sock = new Socket("localhost", port); BufferedReader reader = null; try { OutputStream outstream = sock.getOutputStream(); outstream.write("stat".getBytes()); outstream.flush(); Reader isr = new InputStreamReader(sock.getInputStream()); reader = new BufferedReader(isr); String line = reader.readLine(); if (line != null && line.startsWith("Zookeeper version:")) { return true; } } finally { IOUtils.closeSocket(sock); IOUtils.closeStream(reader); } } catch (IOException e) { // ignore as this is expected LOG.debug("server localhost:" + port + " not up " + e); } if (System.currentTimeMillis() > start + timeout) { break; } Thread.sleep(250); } return false; }
From source file:org.apache.slider.server.appmaster.web.rest.agent.TestAMAgentWebServices.java
License:Apache License
@After public void tearDown() throws Exception { IOUtils.closeStream(webApp); webApp = null; }
From source file:org.apache.slider.server.services.curator.CuratorService.java
License:Apache License
protected void closeCuratorComponent(Closeable closeable) { try {/*from ww w .j av a 2s.c o m*/ IOUtils.closeStream(closeable); } catch (Throwable ignored) { //triggered on an attempt to close before started log.debug("Error when closing {}", ignored); } }
From source file:org.apache.slider.server.services.workflow.LongLivedProcess.java
License:Apache License
/** * Entry point for waiting for the program to finish *//*from w ww . ja v a 2 s. co m*/ @Override // Runnable public void run() { Preconditions.checkNotNull(process, "null process"); LOG.debug("Lifecycle callback thread running"); //notify the callback that the process has started if (lifecycleCallback != null) { lifecycleCallback.onProcessStarted(this); } try { //close stdin for the process IOUtils.closeStream(process.getOutputStream()); exitCode = process.waitFor(); } catch (InterruptedException e) { LOG.debug("Process wait interrupted -exiting thread", e); } finally { //here the process has finished LOG.debug("process {} has finished", name); //tell the logger it has to finish too finished.set(true); // shut down the threads logExecutor.shutdown(); try { logExecutor.awaitTermination(60, TimeUnit.SECONDS); } catch (InterruptedException ignored) { //ignored } //now call the callback if it is set if (lifecycleCallback != null) { lifecycleCallback.onProcessExited(this, exitCode, getExitCodeSignCorrected()); } } }
From source file:org.apache.sqoop.manager.oracle.OracleIncrementalImportTest.java
License:Apache License
private String readLineFromPath(Path filePath) throws IOException { BufferedReader reader = null; if (!isOnPhysicalCluster()) { reader = new BufferedReader(new InputStreamReader(new FileInputStream(new File(filePath.toString())))); } else {/* w w w. j ava 2 s .c om*/ FileSystem dfs = FileSystem.get(getConf()); FSDataInputStream dis = dfs.open(filePath); reader = new BufferedReader(new InputStreamReader(dis)); } String line = null; try { line = reader.readLine(); } finally { IOUtils.closeStream(reader); } return line; }
From source file:org.apache.sqoop.manager.sqlserver.SQLServerDatatypeImportDelimitedFileManualTest.java
License:Apache License
/** * Do a MapReduce-based import of the table and verify that the results were * imported as expected. (tests readFields(ResultSet) and toString()) * * @param expectedVal/*from w ww . ja v a2s . co m*/ * the value we injected into the table. * @param importCols * the columns to import. If null, all columns are used. */ protected void verifyImport(String expectedVal, String[] importCols) { // paths to where our output file will wind up. Path tableDirPath = getTablePath(); removeTableDir(); runSqoopImport(importCols); Configuration conf = getConf(); SqoopOptions opts = getSqoopOptions(conf); try { ImportTool importTool = new ImportTool(); opts = importTool.parseArguments(getArgv(false, importCols, conf), conf, opts, true); } catch (Exception e) { LOG.error(StringUtils.stringifyException(e)); fail(e.toString()); } CompilationManager compileMgr = new CompilationManager(opts); String jarFileName = compileMgr.getJarFilename(); ClassLoader prevClassLoader = null; try { prevClassLoader = ClassLoaderStack.addJarFile(jarFileName, getTableName()); // Now open and check all part-files in the table path until we find // a non-empty one that we can verify contains the value. FileSystem fs = FileSystem.getLocal(conf); FileStatus[] stats = fs.listStatus(tableDirPath); if (stats == null || stats.length == 0) { fail("Error: no files in " + tableDirPath); } boolean foundRecord = false; for (FileStatus stat : stats) { if (!stat.getPath().getName().startsWith("part-") && !stat.getPath().getName().startsWith("data-")) { // This isn't a data file. Ignore it. continue; } try { String line; String fname = stat.getPath().toString(); fname = fname.substring(5, fname.length()); BufferedReader reader = new BufferedReader( new InputStreamReader(new FileInputStream(new File(fname)))); try { line = reader.readLine(); assertEquals(" expected a different string", expectedVal, line); } finally { IOUtils.closeStream(reader); } LOG.info("Read back from sequencefile: " + line); foundRecord = true; // Add trailing '\n' to expected value since // SqoopRecord.toString() // encodes the record delim. if (null == expectedVal) { assertEquals("Error validating result from SeqFile", "null\n", line); } } catch (EOFException eoe) { // EOF in a file isn't necessarily a problem. We may have // some // empty sequence files, which will throw this. Just // continue // in the loop. } } if (!foundRecord) { fail("Couldn't read any records from SequenceFiles"); } } catch (IOException ioe) { LOG.error(StringUtils.stringifyException(ioe)); fail("IOException: " + ioe.toString()); } finally { if (null != prevClassLoader) { ClassLoaderStack.setCurrentClassLoader(prevClassLoader); } } }
From source file:org.apache.sqoop.manager.sqlserver.SQLServerMultiMapsManualTest.java
License:Apache License
public void runMultiMapTest(String splitByCol, int expectedSum) throws IOException { String[] columns = MSSQLTestUtils.getColumns(); ClassLoader prevClassLoader = null; SequenceFile.Reader reader = null; String[] argv = getArgv(true, columns, splitByCol); runImport(argv);/*from w w w .j a v a2s. c o m*/ try { ImportTool importTool = new ImportTool(); SqoopOptions opts = importTool.parseArguments(getArgv(false, columns, splitByCol), null, null, true); String username = MSSQLTestUtils.getDBUserName(); String password = MSSQLTestUtils.getDBPassWord(); opts.setUsername(username); opts.setPassword(password); CompilationManager compileMgr = new CompilationManager(opts); String jarFileName = compileMgr.getJarFilename(); prevClassLoader = ClassLoaderStack.addJarFile(jarFileName, getTableName()); List<Path> paths = getDataFilePaths(); Configuration conf = new Configuration(); int curSum = 0; // We expect multiple files. We need to open all the files and sum // up the // first column across all of them. for (Path p : paths) { reader = SeqFileReader.getSeqFileReader(p.toString()); // here we can actually instantiate (k, v) pairs. Object key = ReflectionUtils.newInstance(reader.getKeyClass(), conf); Object val = ReflectionUtils.newInstance(reader.getValueClass(), conf); // We know that these values are two ints separated by a ',' // character. Since this is all dynamic, though, we don't want // to // actually link against the class and use its methods. So we // just // parse this back into int fields manually. Sum them up and // ensure // that we get the expected total for the first column, to // verify that // we got all the results from the db into the file. // now sum up everything in the file. while (reader.next(key) != null) { reader.getCurrentValue(val); curSum += getFirstInt(val.toString()); } IOUtils.closeStream(reader); reader = null; } assertEquals("Total sum of first db column mismatch", expectedSum, curSum); } catch (InvalidOptionsException ioe) { LOG.error(StringUtils.stringifyException(ioe)); fail(ioe.toString()); } catch (ParseException pe) { LOG.error(StringUtils.stringifyException(pe)); fail(pe.toString()); } finally { IOUtils.closeStream(reader); if (null != prevClassLoader) { ClassLoaderStack.setCurrentClassLoader(prevClassLoader); } } }