Skip to content

Instantly share code, notes, and snippets.

@t3rmin4t0r
Last active December 24, 2015 06:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save t3rmin4t0r/6756270 to your computer and use it in GitHub Desktop.
Save t3rmin4t0r/6756270 to your computer and use it in GitHub Desktop.
HDFS2 go reader/writer example
package main
/*
To build, you need the following magic & an HDP2 install
export CGO_CFLAGS="-I/usr/include/ -I$HADOOP_HOME/include"
export CGO_LDFLAGS="-L $HADOOP_HOME/lib/native -L $JAVA_HOME/jre/lib/amd64/server/"
Then just
go get -u -a
go build -ldflags="-r $HADOOP_HOME/lib/native/"
This will give you a binary which will run as long as HADOOP_HOME is set up properly.
The hdf2go will set up a classpath by itself and the -r arg will bake in the libhdfs.so path into the binary
*/
import "github.com/t3rmin4t0r/hdfs2go"
import "fmt"
func main() {
fs, err := hdfs2.Connect("localhost", 56565)
if err != nil {
fmt.Printf("Error on connecting to hdfs: %v\n", err)
return
}
defer fs.Disconnect()
file, err := fs.OpenFile("/a.txt", hdfs2.O_WRONLY|hdfs2.O_CREATE, 0, 0, 0)
if err != nil {
fmt.Printf("Error on opening file: %v\n", err)
return
}
buf := []byte("hello hdfs world, from go!")
size, err := fs.Write(file, buf, len(buf))
if err != nil {
fmt.Printf("Error on writing bytes to file: %v\n", err)
return
} else {
fmt.Printf("Wrote %d bytes to /a.txt\n", size)
}
err = fs.Flush(file)
err = fs.CloseFile(file)
rfile, err := fs.OpenFile("/a.txt", hdfs2.O_RDONLY, 0, 0, 0)
buffer := make([]byte, len(buf))
val, err := fs.Read(rfile, buffer, len(buffer))
if err != nil {
fmt.Printf("Error on reading file: %v\n", err)
return
} else {
fmt.Printf("Read %d bytes from /a.txt\n", val)
}
fs.CloseFile(rfile)
fs.Delete("/a.txt", false)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment