Skip to content

Instantly share code, notes, and snippets.

@timvw
Created June 21, 2016 11:55
Show Gist options
  • Select an option

  • Save timvw/f5099f8bfe676fc54101a378532d7026 to your computer and use it in GitHub Desktop.

Select an option

Save timvw/f5099f8bfe676fc54101a378532d7026 to your computer and use it in GitHub Desktop.
Use HDFS to connect to Azure blob storage
<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>
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