Example usage for org.apache.hadoop.io IOUtils closeStream

List of usage examples for org.apache.hadoop.io IOUtils closeStream

Introduction

In this page you can find the example usage for org.apache.hadoop.io IOUtils closeStream.

Prototype

public static void closeStream(java.io.Closeable stream) 

Source Link

Document

Closes the stream ignoring Throwable .

Usage

From source file:org.apache.giraffa.TestLeaseManagement.java

License:Apache License

@Test
public void testLeaseFailure() throws IOException {
    String src = "/testLeaseFailure";
    Path path = new Path(src);
    long currentTime = Time.now();
    FSDataOutputStream outputStream = grfs.create(path);
    try {//from   w  w w .  ja va  2  s.com
        checkLease(src, currentTime);

        try {
            grfs.create(path, false);
            fail("Expected AlreadyBeingCreatedException");
        } catch (AlreadyBeingCreatedException e) {
        }

        // keep stream open intentionally
        checkLease(src, currentTime);
    } finally {
        IOUtils.closeStream(outputStream);
    }
    INodeFile iNode = INodeFile.valueOf(nodeManager.getINode(src));
    assertThat(iNode.getFileState(), is(FileState.CLOSED));
    FileLease lease = iNode.getLease();
    assertThat(lease, is(nullValue()));
}

From source file:org.apache.giraffa.TestLeaseManagement.java

License:Apache License

@Test
public void testLeaseRecovery() throws IOException {
    String src = "/testLeaseRecovery";
    Path path = new Path(src);

    HRegionServer server = UTIL.getHBaseCluster().getRegionServer(0);
    LeaseManager leaseManager = LeaseManager
            .originateSharedLeaseManager(server.getRpcServer().getListenerAddress().toString());

    FSDataOutputStream outputStream = grfs.create(path);
    String clientName = grfs.grfaClient.getClientName();
    outputStream.write(1);/*from   w ww.  j  av a2 s  .  c om*/
    outputStream.write(2);
    outputStream.hflush();
    try {
        leaseManager.setHardLimit(10L);
        INodeFile iNode = null;
        for (int i = 0; i < 100; i++) {
            leaseManager.triggerLeaseRecovery();
            try {
                Thread.sleep(100L);
            } catch (InterruptedException ignored) {
            }
            iNode = INodeFile.valueOf(nodeManager.getINode(src));
            if (iNode.getFileState() == FileState.CLOSED)
                break;
        }
        assertThat(iNode.getFileState(), is(FileState.CLOSED));
        assertThat(iNode.getLen(), is(2L));
        assertThat(iNode.getLease(), is(nullValue()));
        assertThat(leaseManager.getLeases(clientName), is(nullValue()));
    } finally {
        leaseManager.setHardLimit(HdfsConstants.LEASE_HARDLIMIT_PERIOD);
        IOUtils.closeStream(outputStream);
    }
}

From source file:org.apache.giraffa.TestLeaseManagement.java

License:Apache License

@Test
public void testClientLeaseRecovery() throws IOException {
    String src = "/testLeaseRecovery";
    Path path = new Path(src);

    HRegionServer server = UTIL.getHBaseCluster().getRegionServer(0);
    LeaseManager leaseManager = LeaseManager
            .originateSharedLeaseManager(server.getRpcServer().getListenerAddress().toString());

    FSDataOutputStream outputStream = grfs.create(path);
    String clientName = grfs.grfaClient.getClientName();
    outputStream.write(1);/*from w  w  w  .  jav a2 s .com*/
    outputStream.write(2);
    outputStream.hflush();
    try {
        boolean recovered = grfs.grfaClient.getNamespaceService().recoverLease(src,
                grfs.grfaClient.getClientName());
        assertThat(recovered, is(true));
        INodeFile iNode = INodeFile.valueOf(nodeManager.getINode(src));
        assertThat(iNode.getFileState(), is(FileState.CLOSED));
        assertThat(iNode.getLen(), is(2L));
        assertThat(iNode.getLease(), is(nullValue()));
        assertThat(leaseManager.getLeases(clientName), is(nullValue()));
    } finally {
        IOUtils.closeStream(outputStream);
    }
}

From source file:org.apache.gora.store.impl.FileBackedDataStoreBase.java

License:Apache License

@Override
public void close() {
    IOUtils.closeStream(inputStream);
    IOUtils.closeStream(outputStream);
    inputStream = null;
    outputStream = null;
}

From source file:org.apache.hama.ipc.AsyncServer.java

License:Apache License

/**
 * Setup response for the IPC Call./*from  ww w . j  a  va2 s .co m*/
 * 
 * @param response buffer to serialize the response into
 * @param call {@link Call} to which we are setting up the response
 * @param status {@link Status} of the IPC call
 * @param rv return value for the IPC Call, if the call was successful
 * @param errorClass error class, if the the call failed
 * @param error error message, if the call failed
 * @throws IOException
 */
private void setupResponse(ByteArrayOutputStream response, Call call, Status status, Writable rv,
        String errorClass, String error) throws IOException {
    response.reset();
    DataOutputStream out = new DataOutputStream(response);
    out.writeInt(call.id); // write call id
    out.writeInt(status.state); // write status

    if (status == Status.SUCCESS) {
        rv.write(out);
    } else {
        WritableUtils.writeString(out, errorClass);
        WritableUtils.writeString(out, error);
    }
    call.setResponse(ByteBuffer.wrap(response.toByteArray()));
    IOUtils.closeStream(out);
}

From source file:org.apache.hdt.core.cluster.HadoopCluster.java

License:Apache License

/**
 * Write this location settings to the given output stream
 * /*from  ww  w . j  a va2s  .c o  m*/
 * @param out the output stream
 * @throws IOException
 */
public void storeSettingsToFile(File file) throws IOException {
    FileOutputStream fos = new FileOutputStream(file);
    try {
        this.conf.writeXml(fos);
        fos.close();
        fos = null;
    } finally {
        IOUtils.closeStream(fos);
    }

}

From source file:org.apache.hdt.debug.core.cluster.RunOnHadoopWizard.java

License:Apache License

/**
 * Performs any actions appropriate in response to the user having pressed
 * the Finish button, or refuse if finishing now is not permitted.
 *///from   w ww  .  ja v  a  2s .c o m
/* @inheritDoc */
@Override
public boolean performFinish() {

    /*
     * Create a new location or get an existing one
     */
    HadoopCluster location = null;
    if (mainPage.createNew.getSelection()) {
        location = createNewPage.performFinish();

    } else if (mainPage.table.getSelection().length == 1) {
        location = (HadoopCluster) mainPage.table.getSelection()[0].getData();
    }

    if (location == null)
        return false;

    /*
     * Get the base directory of the plug-in for storing configurations and
     * JARs
     */
    File baseDir = Activator.getDefault().getStateLocation().toFile();

    // Package the Job into a JAR
    File jarFile = JarModule.createJarPackage(resource);
    if (jarFile == null) {
        ErrorMessageDialog.display("Run on Hadoop", "Unable to create or locate the JAR file for the Job");
        return false;
    }

    /*
     * Generate a temporary Hadoop configuration directory and add it to the
     * classpath of the launch configuration
     */

    File confDir;
    try {
        confDir = File.createTempFile("hadoop-conf-", "", baseDir);
        confDir.delete();
        confDir.mkdirs();
        if (!confDir.isDirectory()) {
            ErrorMessageDialog.display("Run on Hadoop", "Cannot create temporary directory: " + confDir);
            return false;
        }
    } catch (IOException ioe) {
        ioe.printStackTrace();
        return false;
    }

    // Prepare the Hadoop configuration
    JobConf conf = new JobConf(location.getConfiguration());
    conf.setJar(jarFile.getAbsolutePath());

    // Write it to the disk file
    try {
        // File confFile = File.createTempFile("core-site-", ".xml",
        // confDir);
        File confFile = new File(confDir, "core-site.xml");
        FileOutputStream fos = new FileOutputStream(confFile);
        try {
            conf.writeXml(fos);
            fos.close();
            fos = null;
        } finally {
            IOUtils.closeStream(fos);
        }

    } catch (IOException ioe) {
        ioe.printStackTrace();
        return false;
    }

    // Setup the Launch class path
    List<String> classPath;
    try {
        classPath = iConf.getAttribute(IJavaLaunchConfigurationConstants.ATTR_CLASSPATH, new ArrayList());
        IPath confIPath = new Path(confDir.getAbsolutePath());
        IRuntimeClasspathEntry cpEntry = JavaRuntime.newArchiveRuntimeClasspathEntry(confIPath);
        classPath.add(0, cpEntry.getMemento());
        iConf.setAttribute(IJavaLaunchConfigurationConstants.ATTR_CLASSPATH, classPath);
        iConf.setAttribute(IJavaLaunchConfigurationConstants.ATTR_PROGRAM_ARGUMENTS,
                mainPage.argumentsText.getText());

    } catch (CoreException e) {
        e.printStackTrace();
        return false;
    }

    // location.runResource(resource, progressMonitor);
    return true;
}

From source file:org.apache.hive.beeline.BeeLine.java

License:Apache License

private int executeFile(String fileName) {
    InputStream initStream = null;
    try {//from  ww  w.j av a2s . c  o  m
        if (!isBeeLine) {
            org.apache.hadoop.fs.Path path = new org.apache.hadoop.fs.Path(fileName);
            FileSystem fs;
            HiveConf conf = getCommands().getHiveConf(true);
            if (!path.toUri().isAbsolute()) {
                fs = FileSystem.getLocal(conf);
                path = fs.makeQualified(path);
            } else {
                fs = FileSystem.get(path.toUri(), conf);
            }
            initStream = fs.open(path);
        } else {
            initStream = new FileInputStream(fileName);
        }
        return execute(getConsoleReader(initStream), !getOpts().getForce());
    } catch (Throwable t) {
        handleException(t);
        return ERRNO_OTHER;
    } finally {
        IOUtils.closeStream(initStream);
        consoleReader = null;
        output(""); // dummy new line
    }
}

From source file:org.apache.hive.beeline.Commands.java

License:Apache License

/**
 * Connect to the database defined in the specified properties file.
 *///w  w w  . ja  va  2s  .c  o  m
public boolean properties(String line) throws Exception {
    String example = "";
    example += "Usage: properties <properties file>" + BeeLine.getSeparator();

    String[] parts = beeLine.split(line);
    if (parts.length < 2) {
        return beeLine.error(example);
    }

    int successes = 0;

    for (int i = 1; i < parts.length; i++) {
        Properties props = new Properties();
        InputStream stream = new FileInputStream(parts[i]);
        try {
            props.load(stream);
        } finally {
            IOUtils.closeStream(stream);
        }
        if (connect(props)) {
            successes++;
        }
    }

    if (successes != (parts.length - 1)) {
        return false;
    } else {
        return true;
    }
}

From source file:org.apache.hive.beeline.SeparatedValuesOutputFormat.java

License:Apache License

private String getFormattedStr(String[] vals) {
    StringWriter strWriter = new StringWriter();
    CsvListWriter writer = new CsvListWriter(strWriter, getCsvPreference());
    if (vals.length > 0) {
        try {/*from   w  w  w .j  a  v  a  2  s.co  m*/
            writer.write(vals);
        } catch (IOException e) {
            beeLine.error(e);
        } finally {
            IOUtils.closeStream(writer);
        }
    }
    return strWriter.toString();
}