RHive tutorial - apply functions and
RHive supports the use of R’s syntax and characteristics by connecting to
Hive for massive data processing.
Hence RHive supports several Functions similar to apply-like Functions
provided in R,
and supports the making of map/reduce code by writing scripts in a similar
way as Hadoop streaming.
These Functions and features will be expanded upon in future versions.
rhive.napply and rhive.sapply are the same Function. Their only difference is
whether the returned value is numeric or character type.
napply’s argument Function must return a numeric type. sapply’s argument
Function returns a character type.
The two Functions’ arguments take** table names, R Functions that run for
each record, and columns.
In rhive.napply/rhive.sapply, the number of arguments after the “Function to
be applied” must equal to the number of arguments of the “Function to be
Under the surface, these Functions are actually using RHive’s UDF support
feature, so if you already know how Rhive’s UDF works then you will easily
understand this as well.
The following example of uses the two Functions.
First make a table for testing.
The following is an example of using rhive.napply.
The following is an example of using rhive.sapply.
Do note when using these Functions that these Functions do not return
data.frame but return the name of the table temporarily made within the
The user should reprocess then delete these returned tables which the
Functions made as results of processing.
This is because it is generally impossible to receive massive data through
standard output or data.frame.
rhive.mrapply, rhive.mapapply, rhive.reduceapply
These Functions have names similar to the ones mentioned before, but they
actually make Hadoop’s map/reduce into a form that resembles using Hadoop
You can use these Functions to implement wordcount which is frequently
seen in Hadoop streaming examples. Users who wish to write code in
traditional Map/Reduce style will need these Functions.
It is very easy to use them.
rhive.mapapply takes the tables and columns inputted as arguments and runs
them with another Functions inputted as an argument but only runs it with**
rhive.reduceapply only performs a reduce.
And rhive.mrapply performs both map and reduce.
Use rhive.mapapply if you are making something that only requires the map
Use rhive.reduceapply if you are making something that only requires the
Use rhive.mrapply if you need both procedures.
You’ll probably find yourself using rhive.mrapply more often than not.
The following is a wordcount example using rhive.mrapply.
First let’s make a dataset for applying wordcount.
We’ll be using a text web browser called lynx to crawl the R introduction page
and save it to a Hive table.
First install lynx.
If you have a different text file and prefer this rather than installing lynx, then
that is fine as well.
Save the downloaded file to a Hive table.
The RHive code that performs a wordcount on the text file called “rintro” is as
The above example is similar to a Map/Reduce code in Hadoop streaming
style, but rhive.mrapply in the last part of the example is probably unfamiliar
RHive also processes Map/Reduce by using Hive.
Thus input must receive table name and column as basic input parameters.
And because it is quite difficult to automatically find out the inputs and outputs
of each step of map and reduce, the user must assign which are the inputs
and outputs and what names will be used for aliases.
rhive.mrapply("rintro", map, reduce, c("rowname", "rawtext"), c("word","one"),
by="word", c("word","one"), c("word","count"))
The first argument, “rintro”, is the name of the table to be processed.
The map and reduce coming thereafter are each symbols of the R Functions,
respectively processing map and reduce. The c("rowname", "rawtext") after
that are the “rintro” table’s columns that will be taken as arguments for map
Function, and its first value is the column to be used as key, the second is the
column to be used as its value.
The fifth argument, c(“word”, “one”), refers to map’s output and the sixth
argument, by=”word”, refers to the column to be aggregated among map’s
output. The seventh and the eighth are respectively the input and output of
Confusion may ensue with so many arguments, but they are necessary for
The future will see many improvements for this Function..
Under the surface, Rhive.mrapply actually processes map/reduce-related
tasks by making Hive SQL.
This means the user does not have to use rhive.mrapply to process above
examples but the user can directly use the Function provided by RHive and
But this too consists of an unfamiliar and difficult syntax, so RHive came to
support Functions such as these.
rhive.mapapply and rhive.reduceapply are used in almost the same way as
rhive.mrapply so this tutorial omits explaining it.
If you already have a Map/Reduce module with Hadoop streaming or Hadoop
library used and wish to convert it to RHive, there may be many differences
during the conversion process.
RHive does not replace Hadoop streaming, nor does it replace Hadoop library.
But it is merely a convenient measure for helping R users approach Hadoop
and Hive. Should you be attempting to take a high-performance
Map/Reduce module or binary executable file written in C/C++ (and others)
and attempt to convert it into map/reduce and running it, then Hadoop library
and Hadoop streaming may yet be a better choice.