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);
}
}
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();
}
}
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;
}
}
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);
}
}
}
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();
}
}
Aggregations