Search in sources :

Example 61 with Closer

use of org.apache.flink.shaded.guava30.com.google.common.io.Closer in project alluxio by Alluxio.

the class CpCommandIntegrationTest method equals.

private boolean equals(AlluxioURI file1, AlluxioURI file2) throws Exception {
    try (Closer closer = Closer.create()) {
        OpenFilePOptions openFileOptions = OpenFilePOptions.newBuilder().setReadType(ReadPType.NO_CACHE).build();
        FileInStream is1 = closer.register(sFileSystem.openFile(file1, openFileOptions));
        FileInStream is2 = closer.register(sFileSystem.openFile(file2, openFileOptions));
        return IOUtils.contentEquals(is1, is2);
    }
}
Also used : Closer(com.google.common.io.Closer) FileInStream(alluxio.client.file.FileInStream) OpenFilePOptions(alluxio.grpc.OpenFilePOptions)

Example 62 with Closer

use of org.apache.flink.shaded.guava30.com.google.common.io.Closer in project alluxio by Alluxio.

the class CpCommand method copyFileToLocal.

/**
 * Copies a file specified by argv from the filesystem to the local filesystem. This is the
 * utility function.
 *
 * @param srcPath The source {@link AlluxioURI} (has to be a file)
 * @param dstPath The {@link AlluxioURI} of the destination in the local filesystem
 */
private void copyFileToLocal(AlluxioURI srcPath, AlluxioURI dstPath) throws AlluxioException, IOException {
    File dstFile = new File(dstPath.getPath());
    String randomSuffix = String.format(".%s_copyToLocal_", RandomStringUtils.randomAlphanumeric(8));
    File outputFile;
    if (dstFile.isDirectory()) {
        outputFile = new File(PathUtils.concatPath(dstFile.getAbsolutePath(), srcPath.getName()));
    } else {
        outputFile = dstFile;
    }
    File tmpDst = new File(outputFile.getPath() + randomSuffix);
    try (Closer closer = Closer.create()) {
        FileInStream is = closer.register(mFileSystem.openFile(srcPath));
        FileOutputStream out = closer.register(new FileOutputStream(tmpDst));
        byte[] buf = new byte[mCopyToLocalBufferSize];
        int t = is.read(buf);
        while (t != -1) {
            out.write(buf, 0, t);
            t = is.read(buf);
        }
        if (!tmpDst.renameTo(outputFile)) {
            throw new IOException("Failed to rename " + tmpDst.getPath() + " to destination " + outputFile.getPath());
        }
        System.out.println("Copied " + srcPath + " to " + "file://" + outputFile.getPath());
    } finally {
        tmpDst.delete();
    }
}
Also used : Closer(com.google.common.io.Closer) FileOutputStream(java.io.FileOutputStream) FileInStream(alluxio.client.file.FileInStream) IOException(java.io.IOException) File(java.io.File)

Example 63 with Closer

use of org.apache.flink.shaded.guava30.com.google.common.io.Closer in project alluxio by Alluxio.

the class CpCommand method copyFromLocalFile.

private void copyFromLocalFile(AlluxioURI srcPath, AlluxioURI dstPath) throws AlluxioException, IOException {
    File src = new File(srcPath.getPath());
    if (src.isDirectory()) {
        throw new IOException("Source " + src.getAbsolutePath() + " is not a file.");
    }
    // src will be copied to.
    if (mFileSystem.exists(dstPath) && mFileSystem.getStatus(dstPath).isFolder()) {
        dstPath = dstPath.join(src.getName());
    }
    FileOutStream os = null;
    try (Closer closer = Closer.create()) {
        os = closer.register(mFileSystem.createFile(dstPath));
        FileInputStream in = closer.register(new FileInputStream(src));
        FileChannel channel = closer.register(in.getChannel());
        ByteBuffer buf = ByteBuffer.allocate(mCopyFromLocalBufferSize);
        while (channel.read(buf) != -1) {
            buf.flip();
            os.write(buf.array(), 0, buf.limit());
        }
    } catch (Exception e) {
        // around.
        if (os != null) {
            os.cancel();
            if (mFileSystem.exists(dstPath)) {
                mFileSystem.delete(dstPath);
            }
        }
        throw e;
    }
}
Also used : Closer(com.google.common.io.Closer) FileChannel(java.nio.channels.FileChannel) FileOutStream(alluxio.client.file.FileOutStream) IOException(java.io.IOException) File(java.io.File) ByteBuffer(java.nio.ByteBuffer) FileInputStream(java.io.FileInputStream) InvalidPathException(alluxio.exception.InvalidPathException) InvalidArgumentException(alluxio.exception.status.InvalidArgumentException) AlluxioException(alluxio.exception.AlluxioException) ParseException(org.apache.commons.cli.ParseException) FileDoesNotExistException(alluxio.exception.FileDoesNotExistException) FileAlreadyExistsException(alluxio.exception.FileAlreadyExistsException) IOException(java.io.IOException)

Example 64 with Closer

use of org.apache.flink.shaded.guava30.com.google.common.io.Closer in project alluxio by Alluxio.

the class ConfigurationDocGenerator method writeYMLFile.

/**
 * Writes description of property key to yml files.
 *
 * @param defaultKeys Collection which is from PropertyKey DEFAULT_KEYS_MAP.values()
 * @param filePath path for csv files
 */
@VisibleForTesting
public static void writeYMLFile(Collection<? extends PropertyKey> defaultKeys, String filePath) throws IOException {
    if (defaultKeys.size() == 0) {
        return;
    }
    FileWriter fileWriter;
    Closer closer = Closer.create();
    String[] fileNames = { "user-configuration.yml", "master-configuration.yml", "worker-configuration.yml", "security-configuration.yml", "common-configuration.yml", "cluster-management-configuration.yml" };
    try {
        // HashMap for FileWriter per each category
        Map<String, FileWriter> fileWriterMap = new HashMap<>();
        for (String fileName : fileNames) {
            fileWriter = new FileWriter(PathUtils.concatPath(filePath, fileName));
            // put fileWriter
            String key = fileName.substring(0, fileName.indexOf("configuration") - 1);
            fileWriterMap.put(key, fileWriter);
            // register file writer
            closer.register(fileWriter);
        }
        // Sort defaultKeys
        List<PropertyKey> dfkeys = new ArrayList<>(defaultKeys);
        Collections.sort(dfkeys);
        for (PropertyKey iteratorPK : dfkeys) {
            String pKey = iteratorPK.toString();
            // Puts descriptions in single quotes to avoid having to escaping reserved characters.
            // Still needs to escape single quotes with double single quotes.
            String description = iteratorPK.getDescription().replace("'", "''");
            // Write property key and default value to yml files
            if (iteratorPK.isIgnoredSiteProperty()) {
                description += " Note: overwriting this property will only work when it is passed as a " + "JVM system property (e.g., appending \"-D" + iteratorPK + "\"=<NEW_VALUE>\" to " + "$ALLUXIO_JAVA_OPTS). Setting it in alluxio-site.properties will not work.";
            }
            String keyValueStr = pKey + ":\n  '" + description + "'\n";
            if (pKey.startsWith("alluxio.user.")) {
                fileWriter = fileWriterMap.get("user");
            } else if (pKey.startsWith("alluxio.master.")) {
                fileWriter = fileWriterMap.get("master");
            } else if (pKey.startsWith("alluxio.worker.")) {
                fileWriter = fileWriterMap.get("worker");
            } else if (pKey.startsWith("alluxio.security.")) {
                fileWriter = fileWriterMap.get("security");
            } else if (pKey.startsWith("alluxio.integration.")) {
                fileWriter = fileWriterMap.get("cluster-management");
            } else {
                fileWriter = fileWriterMap.get("common");
            }
            fileWriter.append(StringEscapeUtils.escapeHtml4(keyValueStr));
        }
        LOG.info("YML files for description of Property Keys were created successfully.");
    } catch (Exception e) {
        throw closer.rethrow(e);
    } finally {
        try {
            closer.close();
        } catch (IOException e) {
            LOG.error("Error while flushing/closing YML files for description of Property Keys " + "FileWriter", e);
        }
    }
}
Also used : Closer(com.google.common.io.Closer) HashMap(java.util.HashMap) FileWriter(java.io.FileWriter) ArrayList(java.util.ArrayList) IOException(java.io.IOException) PropertyKey(alluxio.conf.PropertyKey) IOException(java.io.IOException) VisibleForTesting(com.google.common.annotations.VisibleForTesting)

Example 65 with Closer

use of org.apache.flink.shaded.guava30.com.google.common.io.Closer in project jmxtrans by jmxtrans.

the class LibratoWriter2 method write.

@Override
public void write(@Nonnull Writer writer, @Nonnull Server server, @Nonnull Query query, @Nonnull Iterable<Result> results) throws IOException {
    Closer closer = Closer.create();
    try {
        JsonGenerator g = closer.register(jsonFactory.createGenerator(writer));
        g.writeStartObject();
        g.writeArrayFieldStart("counters");
        g.writeEndArray();
        String source = getSource(server);
        g.writeArrayFieldStart("gauges");
        for (Result result : results) {
            if (isNumeric(result.getValue())) {
                g.writeStartObject();
                g.writeStringField("name", KeyUtils.getKeyString(query, result, typeNames));
                if (source != null && !source.isEmpty()) {
                    g.writeStringField("source", source);
                }
                g.writeNumberField("measure_time", SECONDS.convert(result.getEpoch(), MILLISECONDS));
                Object value = result.getValue();
                if (value instanceof Integer) {
                    g.writeNumberField("value", (Integer) value);
                } else if (value instanceof Long) {
                    g.writeNumberField("value", (Long) value);
                } else if (value instanceof Float) {
                    g.writeNumberField("value", (Float) value);
                } else if (value instanceof Double) {
                    g.writeNumberField("value", (Double) value);
                }
                g.writeEndObject();
            }
        }
        g.writeEndArray();
        g.writeEndObject();
        g.flush();
    } catch (Throwable t) {
        throw closer.rethrow(t);
    } finally {
        closer.close();
    }
}
Also used : Closer(com.google.common.io.Closer) JsonGenerator(com.fasterxml.jackson.core.JsonGenerator) Result(com.googlecode.jmxtrans.model.Result)

Aggregations

Closer (com.google.common.io.Closer)213 IOException (java.io.IOException)95 File (java.io.File)26 Test (org.testng.annotations.Test)21 Path (org.apache.hadoop.fs.Path)18 Test (org.junit.Test)18 Properties (java.util.Properties)16 Closer (org.apache.flink.shaded.guava30.com.google.common.io.Closer)16 FileOutputStream (java.io.FileOutputStream)15 ArrayList (java.util.ArrayList)15 WorkUnit (org.apache.gobblin.source.workunit.WorkUnit)13 FileInputStream (java.io.FileInputStream)12 InputStream (java.io.InputStream)12 OutputStream (java.io.OutputStream)12 Map (java.util.Map)12 ByteArrayInputStream (java.io.ByteArrayInputStream)10 DataInputStream (java.io.DataInputStream)10 UncheckedIOException (java.io.UncheckedIOException)10 Configuration (org.apache.hadoop.conf.Configuration)10 Text (org.apache.hadoop.io.Text)9