Created
June 21, 2016 11:55
-
-
Save timvw/f5099f8bfe676fc54101a378532d7026 to your computer and use it in GitHub Desktop.
Use HDFS to connect to Azure blob storage
This file contains hidden or 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
| <dependencies> | |
| <dependency> | |
| <groupId>org.apache.hadoop</groupId> | |
| <artifactId>hadoop-client</artifactId> | |
| <version>2.7.2</version> | |
| </dependency> | |
| <dependency> | |
| <groupId>org.apache.hadoop</groupId> | |
| <artifactId>hadoop-azure</artifactId> | |
| <version>2.7.2</version> | |
| </dependency> | |
| </dependencies> |
This file contains hidden or 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
| package be.icteam.sandbox | |
| import java.net.URI | |
| import org.apache.hadoop.conf._ | |
| import org.apache.hadoop.fs._ | |
| object Program extends App { | |
| override def main(args: Array[String]) = { | |
| val storageAccountName = "icteambg" | |
| val storageAccessKey = "==secret-storage-access-key==" | |
| val storageContainerName = "test1" | |
| val conf = new Configuration() | |
| conf.set(s"fs.azure.account.key.$storageAccountName.blob.core.windows.net", storageAccessKey) | |
| val fs = FileSystem.get(new URI(s"wasb://$storageContainerName@$storageAccountName.blob.core.windows.net/"), conf) | |
| val rootPath = new Path("/") | |
| val testFilePath = new Path("/test.txt") | |
| println(s"creating $testFilePath") | |
| fs.create(testFilePath) | |
| println(s"listing files in $rootPath") | |
| listFilesInPath(fs, rootPath) | |
| println(s"deleteting $testFilePath") | |
| fs.delete(testFilePath) | |
| fs.close() | |
| } | |
| def listFilesInPath(fs: FileSystem, path: Path): Unit = { | |
| val files: RemoteIterator[LocatedFileStatus] = fs.listFiles(path, false) | |
| while (files.hasNext) { | |
| val file = files.next() | |
| val path = file.getPath.toUri.toString | |
| println(s"found $path") | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment