RでHDFS上のデータを読み書きするパッケージを公開しました
RでHadoopを使うパッケージは、RHadoopとかRHIPEとかありますが、
単純にHDFS上のデータを読み書き出来るだけのシンプルなものが欲しかったのでパッケージを作って公開してみました。
JavaとかPigで一次集計したデータをRで読み込む時に使うイメージです。
準備
install.packages("rHadoopClient") library(rHadoopClient)
HDFS上のデータを読み込む
./tmp/iris にアップしたirisのデータを読み込む
data.hdfs <- read.hdfs("tmp/iris") names(data.hdfs) <- names(iris) head(data.hdfs)
## Sepal.Length Sepal.Width Petal.Length Petal.Width Species ## 1 5.1 3.5 1.4 0.2 setosa ## 2 4.9 3.0 1.4 0.2 setosa ## 3 4.7 3.2 1.3 0.2 setosa ## 4 4.6 3.1 1.5 0.2 setosa ## 5 5.0 3.6 1.4 0.2 setosa ## 6 5.4 3.9 1.7 0.4 setosa
HDFSにデータを書き出す
./tmp/output_irisにデータを書き出す
write.hdfs(iris, "tmp/output_iris")
$ hadoop fs -cat ./tmp/output_iris | head 5.1 3.5 1.4 0.2 setosa 4.9 3 1.4 0.2 setosa 4.7 3.2 1.3 0.2 setosa 4.6 3.1 1.5 0.2 setosa 5 3.6 1.4 0.2 setosa 5.4 3.9 1.7 0.4 setosa 4.6 3.4 1.4 0.3 setosa 5 3.4 1.5 0.2 setosa 4.4 2.9 1.4 0.2 setosa 4.9 3.1 1.5 0.1 setosa
Hiveを実行して、その結果を読み込む
irisのデータが入っているiris_tableからのデータ読み込み
data.hive <- read.hive("select * from iris_table") names(data.hive) <- names(iris) head(data.hive)
## Sepal.Length Sepal.Width Petal.Length Petal.Width Species ## 1 5.1 3.5 1.4 0.2 setosa ## 2 4.9 3.0 1.4 0.2 setosa ## 3 4.7 3.2 1.3 0.2 setosa ## 4 4.6 3.1 1.5 0.2 setosa ## 5 5.0 3.6 1.4 0.2 setosa ## 6 5.4 3.9 1.7 0.4 setosa