This repository has been archived by the owner on Jun 27, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
#25 Use URIs and own FileOperations implementations for HDFS
- Loading branch information
Showing
8 changed files
with
112 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 2 additions & 2 deletions
4
eu.mondo.sam.core/src/main/java/eu/mondo/sam/core/results/ResultSerializer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,10 @@ | ||
package eu.mondo.sam.core.results; | ||
|
||
import java.io.IOException; | ||
import java.nio.file.Path; | ||
import java.net.URI; | ||
|
||
public interface ResultSerializer { | ||
|
||
public void serialize(BenchmarkResult benchmarkResult, Path resultFilePathWithoutExtension) | ||
public void serialize(BenchmarkResult benchmarkResult, URI resultFilePathWithoutExtension) | ||
throws IOException; | ||
} |
57 changes: 57 additions & 0 deletions
57
eu.mondo.sam.core/src/main/java/eu/mondo/sam/core/util/FileOperations.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
package eu.mondo.sam.core.util; | ||
|
||
import java.io.File; | ||
import java.io.IOException; | ||
import java.net.URI; | ||
import java.net.URISyntaxException; | ||
|
||
import org.apache.commons.io.FileUtils; | ||
import org.apache.http.client.fluent.Request; | ||
import org.apache.http.client.fluent.Response; | ||
import org.apache.http.entity.ContentType; | ||
|
||
import com.google.common.base.Throwables; | ||
|
||
public class FileOperations { | ||
|
||
private static final String WEBHDFS_SCHEME = "webhdfs"; | ||
|
||
private static URI makeWebHdfsUri(URI destination, String operation) { | ||
try { | ||
return new URI("http", "", destination.getHost(), destination.getPort(), "/webhdfs/v1/" + destination.getPath(), "op=" + operation, ""); | ||
} catch (URISyntaxException e) { | ||
throw Throwables.propagate(e); | ||
} | ||
} | ||
|
||
private static boolean isHdfs(URI destination) { | ||
return (destination.getScheme() != null) && destination.getScheme().equals(WEBHDFS_SCHEME); | ||
} | ||
|
||
public static void copy(File source, URI destination) throws IOException { | ||
if (isHdfs(destination)) { | ||
URI endpointUri = makeWebHdfsUri(destination, "CREATE"); | ||
Response response = Request.Put(endpointUri).execute(); | ||
String location = response.returnResponse().getFirstHeader("Location").getValue(); | ||
Request.Put(location).bodyFile(source, ContentType.DEFAULT_BINARY).execute(); | ||
} else { | ||
FileUtils.copyFile(source, new File(destination.getPath())); | ||
} | ||
} | ||
|
||
public static void copy(URI source, File destination) throws IOException { | ||
if (isHdfs(source)) { | ||
Request.Get(makeWebHdfsUri(source, "OPEN")).execute().saveContent(destination); | ||
} else { | ||
FileUtils.copyFile(new File(source.getPath()), destination); | ||
} | ||
} | ||
|
||
public static File loadModel(URI modelUriWithoutExtension, String postfix) throws IOException { | ||
URI modelUriWithExtension = UriUtils.appendSuffix(modelUriWithoutExtension, postfix); | ||
File tempFile = File.createTempFile("benchmark", null); | ||
copy(modelUriWithExtension, tempFile); | ||
return tempFile; | ||
} | ||
|
||
} |
20 changes: 20 additions & 0 deletions
20
eu.mondo.sam.core/src/main/java/eu/mondo/sam/core/util/UriUtils.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package eu.mondo.sam.core.util; | ||
|
||
import java.net.URI; | ||
|
||
import org.apache.commons.io.FilenameUtils; | ||
|
||
public class UriUtils { | ||
|
||
public static URI appendSuffix(URI resultFilePathWithoutExtension, String suffix) { | ||
String fileNameWithExtension = FilenameUtils.getName(resultFilePathWithoutExtension.toString()) + suffix; | ||
URI resultFilePath = resultFilePathWithoutExtension.resolve(fileNameWithExtension); | ||
return resultFilePath; | ||
} | ||
|
||
public static URI appendSegment(URI base, String segment) { | ||
String name = FilenameUtils.getName(base.getPath()); | ||
return base.resolve(name + "/" + segment); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters