DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Join us tomorrow at 1 PM EST: "3-Step Approach to Comprehensive Runtime Application Security"
Save your seat
  1. DZone
  2. Data Engineering
  3. Big Data
  4. R and Hadoop Data Analytics - RHadoop

R and Hadoop Data Analytics - RHadoop

Istvan Szegedi user avatar by
Istvan Szegedi
·
Feb. 27, 13 · Interview
Like (0)
Save
Tweet
Share
14.32K Views

Join the DZone community and get the full member experience.

Join For Free

introduction

r is a programming language and a software suite used for data analysis, statistical computing and data visualization. it is highly extensible and has object oriented features and strong graphical capabilities. at its heart r is an interpreted language and comes with a command line interpreter - available for linux, windows and mac machines - but there are ides as well to support development like rstudio or jgr. r and hadoop can complement each other very well, they are a natural match in big data analytics and visualization. one of the most well-known r packages to support hadoop functionalities is rhadoop that was developed by revolutionanalytics.

installing rhadoop

rhadoop is a collection of three r packages: rmr, rhdfs and rhbase. rmr package provides hadoop mapreduce functionality in r, rhdfs provides hdfs file management in r and rhbase provides hbase database management from within r. to install these r packages, first we need to install r base package. on ubuntu 12.04 lts we can do it running:
$ sudo apt-get install r-base
then we need to install rhadoop packages with their dependencies.  rmr requires rcpp, rjsonio, digest, functional, stringr and plyr, while rhdfs requires rjava. as part of the installation, we need to reconfigure java for rjava package and we also need to set hadoop_cmd variable for rhdfs package. the installation requires the corresponding tar.gz archives to be downloaded and then we can run r cmd install command with sudo privileges.
sudo r cmd install rcpp rcpp_0.10.2.tar.gz
sudo r cmd install rjsonio rjsonio_1.0-1.tar.gz
sudo r cmd install digest digest_0.6.2.tar.gz
sudo r cmd install functional functional_0.1.tar.gz
sudo r cmd install stringr stringr_0.6.2.tar.g
sudo r cmd install plyr plyr_1.8.tar.gz
sudo r cmd install rmr rmr2_2.0.2.tar.gz

sudo java_home=/home/istvan/jdk1.6.0_38/jre r cmd javareconf
sudo r cmd install rjava rjava_0.9-3.tar.gz 
sudo hadoop_cmd=/home/istvan/hadoop/bin/hadoop r cmd install rhdfs rhdfs_1.0.5.tar.gz 
sudo r cmd install rhdfs rhdfs_1.0.5.tar.gz

getting started with rhadoop

in principle, rhadoop mapreduce is a similar operation to r lapply function that applies a function over a list or vector. without mapreduce function we could write a simple r code to double all the numbers from 1 to 100:
> ints = 1:100
> doubleints = sapply(ints, function(x) 2*x)
> head(doubleints)
[1]  2  4  6  8 10 12
with rhadoop rmr package we could use mapreduce function to implement the same calculations - see doubleints.r script:
sys.setenv(hadoop_home="/home/istvan/hadoop")
sys.setenv(hadoop_cmd="/home/istvan/hadoop/bin/hadoop")

library(rmr2)
library(rhdfs)

ints = to.dfs(1:100)
calc = mapreduce(input = ints,
                   map = function(k, v) cbind(v, 2*v))

from.dfs(calc)

$val
         v    
  [1,]   1   2
  [2,]   2   4
  [3,]   3   6
  [4,]   4   8
  [5,]   5  10
.....
if we want to run hdfs filesystem commands from r, we first need to initialize rhdfs using hdfs.init() function, then we can run the well-known ls, rm, mkdir, stat, etc commands:
> hdfs.init()
> hdfs.ls("/tmp")
  permission  owner      group size          modtime               file
1 drwxr-xr-x istvan supergroup    0 2013-02-25 21:59    /tmp/rtmpc94l4r
2 drwxr-xr-x istvan supergroup    0 2013-02-25 21:49 /tmp/hadoop-istvan
> hdfs.stat("/tmp")
      perms isdir block replication  owner      group size              modtime path
1 rwxr-xr-x  true     0           0 istvan supergroup    0 45124-08-29 23:58:48 /tmp

data analysis with rhadoop

the following example demonstrates how to use rhadoop for data analysis. let us assume that we need to determine how many countries have greater gdp than apple inc.'s revenue in 2012. (it was 156,508 millions usd, see more details http://www.google.com/finance?q=nasdaq%3aaapl&fstype=ii&ei=5eeruccpb8kowaokqq ) gdp data can be downloaded from worldbank data catalog site. the data needs to be adjusted to be suitable for mapreduce algorithm. the final format that we used for data analysis is as follows (where the last column is the gdp of the given country in millions usd):
country code,number,country name,gdp,
usa,1,united states,14991300
chn,2,china,7318499
jpn,3,japan,5867154
deu,4,germany,3600833
fra,5,france,2773032
....
the gdp.r script looks like this:
sys.setenv(hadoop_home="/home/istvan/hadoop")
sys.setenv(hadoop_cmd="/home/istvan/hadoop/bin/hadoop")

library(rmr2)
library(rhdfs)

setwd("/home/istvan/rhadoop/blogs/")
gdp <- read.csv("gdp_converted.csv")
head(gdp)

hdfs.init()
gdp.values <- to.dfs(gdp)

# aapl revenue in 2012 in millions usd
aaplrevenue = 156508

gdp.map.fn <- function(k,v) {
key <- ifelse(v[4] < aaplrevenue, "less", "greater")
keyval(key, 1)
}

count.reduce.fn <- function(k,v) {
keyval(k, length(v))
}

count <- mapreduce(input=gdp.values,
                   map = gdp.map.fn,
                   reduce = count.reduce.fn)

from.dfs(count)
r will initiate a hadoop streaming job to process the data using mapreduce algorithm.
packagejobjar: [/tmp/rtmp4llujl/rmr-local-env1e025ac0444f, /tmp/rtmp4llujl/rmr-global-env1e027a86f559, /tmp/rtmp4llujl/rmr-streaming-map1e0214a61fa5, /tmp/rtmp4llujl/rmr-streaming-reduce1e026da4f6c9, /tmp/hadoop-istvan/hadoop-unjar1158187086349476064/] [] /tmp/streamjob430878637581358129.jar tmpdir=null
13/02/25 22:28:12 info mapred.fileinputformat: total input paths to process : 1
13/02/25 22:28:12 info streaming.streamjob: getlocaldirs(): [/tmp/hadoop-istvan/mapred/local]
13/02/25 22:28:12 info streaming.streamjob: running job: job_201302252148_0006
13/02/25 22:28:12 info streaming.streamjob: to kill this job, run:
13/02/25 22:28:12 info streaming.streamjob: /home/istvan/hadoop-1.0.4/libexec/../bin/hadoop job  -dmapred.job.tracker=localhost:9001 -kill job_201302252148_0006
13/02/25 22:28:12 info streaming.streamjob: tracking url: http://localhost:50030/jobdetails.jsp?jobid=job_201302252148_0006
13/02/25 22:28:13 info streaming.streamjob:  map 0%  reduce 0%
13/02/25 22:28:25 info streaming.streamjob:  map 100%  reduce 0%
13/02/25 22:28:37 info streaming.streamjob:  map 100%  reduce 100%
13/02/25 22:28:43 info streaming.streamjob: job complete: job_201302252148_0006
13/02/25 22:28:43 info streaming.streamjob: output: /tmp/rtmp4llujl/file1e025f146f8f
then we will get the data saying how many countries have greater and how many contries have less gdp than apple inc.'s revenue in year 2012. the result is that 55 countries had greater gdp than apple and 138 countries had less.
$key
   gdp      
1  "greater"
56 "less"   

$val
[1]  55 138
the following screenshot from rstudio shows the histogram of gdps - there are 15 countries having more than 1,000 millions usd gdp; 1 country is in the range of 14,000 - 15,000 millions usd, 1 country is in the range of 7,000 - 8,000 millions usd and 1 country is in the range of 5,000 - 6,000 usd. gdp-histogram

conclusion

if someone needs to combine strong data analytics and visualization features  with big data capabilities supported by hadoop, it is certainly worth to have a closer look at rhadoop features. it has packages to integrate r with mapreduce, hdfs and hbase, the key components of the hadoop ecosystem. for more details, please read the r and hadoop big data analytics whitepaper.
R (programming language) Big data hadoop Analytics

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Insight Into Developing Quarkus-Based Microservices
  • How To Generate Code Coverage Report Using JaCoCo-Maven Plugin
  • NEXT.JS 13: Be Dynamic Without Limits
  • When AI Strengthens Good Old Chatbots: A Brief History of Conversational AI

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: