Rhive is a package that uses hive high-performance queries to expand R computing capabilities. It can easily call hql in the r environment, and can also use R objects and functions in hive. Theoretically, the data processing capacity can be expanded infinitely on the hive platform, coupled with the R environment, which is a perfect environment for Big Data Analysis and mining.
Environment Configuration
(The configuration is done by a colleague and only some details are recorded)
Rhive depends on rserve, so there are some changes when installing R:
./configure --disable-nls --enable-R-shlib
make
make install
Enable-r-shlib is to install R as a dynamic library, so that packages such as rserve dependent on the R dynamic library can be installed, but the disadvantage is thatPerformance drops by about 20%.
Rserve Installation
Normal R package installation:
install.packages('rJava')
install.packages('Rserve')
Under the rsever installation user, create a directory, create the rserv. conf file, and write ''remote enable'' to save and exit.
Run the SCP-R command to copy the rserve package installed on the master node and the rserv. conf file to all slave nodes,
scp -r /data2/soft/R2.15/library/Rserve slave1:/data2/soft/R2.15/library/
scp -r /data2/soft/R2.15/library/Rserve slave2:/data2/soft/R2.15/library/
scp -r /data2/soft/R2.15/library/Rserve slave3:/data2/soft/R2.15/library/
scp -r /data2/soft/Rserv.conf slave1:/data2/soft/
scp -r /data2/soft/Rserv.conf slave2:/data2/soft/
scp -r /data2/soft/Rserv.conf slave3:/data2/soft/
Start rserve on all nodes
Rserve--RS-conf /data2/soft/Rserv.conf
telnet slave1 6311
Telnet all slave nodes on the master node. If rsrv0103qap1 is displayed, the connection is successful.
Rhive Installation
Installation Package, create a directory on the master node and all slave nodes, and grant Read and Write Permissions
R CMD INSTALL RHive_0.0-7.tar.gz
cd /data2/soft/
mkdir -p rhive/data
Configure environment variables for hadoop users on Master nodes and all slave nodes
vi .bash_profile
export RHIVE_DATA=/data2/soft/R/rhive/data
Run the SCP-R command to copy the rhive package installed on the master node to all slave nodes,
scp -r /data2/soft/R2.15/library/RHive slave1:/data2/soft/R2.15/library/
scp -r /data2/soft/R2.15/library/RHive slave2:/data2/soft/R2.15/library/
scp -r /data2/soft/R2.15/library/RHive slave3:/data2/soft/R2.15/library/
Check whether the jar package in the HDFS file system has read and write permissions.
hadoop fs -ls /rhive/lib
Finally, start the hive remote service: rhive connects to hiveserver through thrift. You need to start the background thrift service, that is, start the hive remote service on the hive client.
nohup hive --service hiveserver &
Complete.
Rhive-API
Functions used to obtain table information from hive, such
- Rhive. List. Tables: obtains the table name list. The pattern parameter (Regular Expression) is supported, similar to the show table
- Rhive. DESC. Table: Table description. DESC table in hive
- Rhive. exist. Table:
Simple rhive Application
Load the rhive package, connect to hive, and obtain data:
library(RHive)
rhive.connect(host ='host_ip')
d <- rhive.query('select * from emp limit 1000')
class(d)
m <- rhive.block.sample(data_sku, percent =0.0001, seed =0)
rhive.close()
Generally, the host has been configured in the system, so you can directlyrhive.connect()Connect. Remember to haverhive.close()Operation. The hive query statement is used to load the target data in hive to the r environment, and the returned D is a dataframe.
Actually,rhive.queryThere are many practical uses, generally hive operations can be used, such as changing scheme:
rhive.query('use scheme1')
rhive.query('show tables')
rhive.query('drop table emp')
However, when the data volume is large, you need to userhive.big.queryAnd set the memlimit parameter.
To store objects in R to hive by creating tables, you need to use
rhive.write.table(dat, tablename ='usertable', sep =',')
Then, use hive statements such as join to obtain the relevant modeling data. As a matter of fact, it should be clear to those who need it. These rhive functions are enough to be interesting.