Integrating R with Cloudera Impala for Real-Time Queries on Hadoop
Join the DZone community and get the full member experience.Join For Free
cloudera impala supports low-latency, interactive queries on hadoop data sets either stored in hadoop distributed file system (hdfs) or hbase, the distributed nosql database for hadoop. impala’s notion is to use hadoop as a storage engine but move away from mapreduce algorithms. instead, impala uses distributed queries, a concept inherited from massive parallel processing databases. as a result, impala supports sql-like query languange (in the same way way as apache hive), but can execute the queries 10-100 times fasters than hive that converts them into mapreduce. you can find more details on impala in one of the previous posts .
r is one of the most popular open source statistical computing and graphical software. it can work with various data sources from comma separated files to web contents referred by urls to relational databases to nosql (e.g. mongodb or cassandra) and hadoop .
thanks to the generic impala odbc driver, r can be integrated with impala, too. the solution will provide fast, interactive queries running on top of hadoop data sets and then the data can be further processed or visualized within r.
cloudera impala odbc drivers
as we can see in the diagram below, impala runs on the top of dataset stored in hdfs or hbase and the users can interact with it in multiple ways.
one option is to use impala-shell which is part of the impala package and provides a command line interface. other option is to use hue (cloduera’s hadoop user experience product) that is a web browser based ui offering a query editor among other functions that is capable of run queries against pig, hive or impala.the third option is to use odbc driver and connect some of the well-known popular bi tools to impala.
cloudera provides connectors for some of the most popular leading analytics and data visualization tools such as tableau, qlikview or microstrategy. it can also offer a generic odbc driver that can be used to connect various tools. this is the software component that we will use in the post to demonstrate how to integrate r with cloudera impala.
install r, rstudio server, impala odbc and rodbc
impala installation was covered in this post . to install r on a linux environment (for now fedora 19 will be used ) we need to execute the following commands:
# install epel package - epel stands for extra package for enterprise linux $ sudo rpm -ivh http://mirror.chpc.utah.edu/pub/epel/5/x86_64/epel-release-5-4.noarch.rpm $ sudo yum install r ================================================================================ package arch version repository size ================================================================================ updating: r x86_64 3.0.2-1.el6 epel 20 k updating for dependencies: r-core x86_64 3.0.2-1.el6 epel 46 m r-core-devel x86_64 3.0.2-1.el6 epel 90 k r-devel x86_64 3.0.2-1.el6 epel 19 k r-java x86_64 3.0.2-1.el6 epel 20 k r-java-devel x86_64 3.0.2-1.el6 epel 20 k librmath x86_64 3.0.2-1.el6 epel 116 k librmath-devel x86_64 3.0.2-1.el6 epel 24 k transaction summary ================================================================================ upgrade 8 package(s)
r comes with a command line interpreter but if you want to have a more convenient development environment, you may prefer to use rstudio . rstudio has a desktop version as well as a web browser based alternative called rstudio server. they can be downloaded for free from rstudio website . we will use rstudio server in this post.
to install rstudio server, you need to execute the following command:
$ sudo yum install --nogpgcheck rstudio-server-0.97.551-x86_64.rpm ================================================================================ package arch version repository size ================================================================================ installing: rstudio-server x86_64 0.97.551-1 /rstudio-server-0.97.551-x86_64 96 m ... transaction summary =================================================================== install 3 package(s)
to ensure that impala odbc driver will work and rodbc package can be installed within r (as it will be shown later on in this post), you also need to install unixodbc and unixodbc-devel packages:
$ sudo yum install unixodbc $ sudo yum install unixodbc-devel
finally you have to install cloudera impala odbc driver. you can download it from cloudera website , as of writing the post the latest version is 2.5 (the driver file name is clouderaimpalaodbc-184.108.40.2065-1.el6.x86_64.rpm). to install impala odbc driver, you need to run the following command after downloading the driver:
$ yum --nogpgcheck localinstall clouderaimpalaodbc-220.127.116.115-1.el6.x86_64.rpm
impala odbc driver requires a couple of files configured properly (the driver package has templates files embedded that needs to be edited and copied to the correct directory). the two key configuration files are odbc.init and cloudera.impalaodbc.ini.
odbc.ini should look something like this:
[impala] # description: dsn description. # this key is not necessary and is only to give a description of the data source. description=cloudera odbc driver for impala (64-bit) dsn # driver: the location where the odbc driver is installed to. driver=/opt/cloudera/impalaodbc/lib/64/libclouderaimpalaodbc64.so # values for host, port, krbfqdn, and krbservicename should be set here. # they can also be specified on the connection string. host=localhost port=21050 database=default
in cloudera.impalaodbc.ini configuration file we have the following settings:
# simbadn / unixodbc odbcinstlib=libodbcinst.so
in addition, we need to define the environment variables as follows:
$ export ld_library_path=/usr/local/lib:/opt/cloudera/impalaodbc/lib/64 $ export odbcini=/etc/odbc.ini $ export simbadn=/etc/cloudera.impalaodbc.ini
the final step is to install rodbc package for r. you can do it using r command line tool:
$ r >install.packages("rodbc")
analyzing hadoop datasets with r and impala
now we are ready to start analyzing our hadoop data set with r and impala. we will demonstrate how they work together using stock price information. you can download e.g. google stock prices from http://finance.yahoo.com (symbol: goog). once you have the spreadsheet downloaded, you need to remove the first line (header) from the file and then load it into hdfs using hadoop file system shell.
$ hadoop fs -mkdir /user/cloudera/stock $ hadoop fs -put google.csv /user/cloudera/stock $ hadoop fs -ls /user/cloudera/stock found 1 items -rw-r--r-- 3 cloudera cloudera 126379 2013-11-22 12:22 /user/cloudera/stock/google.csv
now we can login to impala shell to create our table. impala has a sql-like query language so you can use the familiar create table command. the external clause indicates that the physical data files are managed outside impala; even if you drop the table, the files will be kept in the hdfs directory.
after the table is created, we can run show tables statement to verify if the table is accessible from impala. we can also run a select statement from impala-shell to display a couple of rows from the stock table.
$ impala-shell [localhost.localdomain:21000] > create external table stock (stock_date string, stock_open float, stock_high float, stock_low float, stock_close_ float, stock_volume int, stock_adjclose float) row format delimited fields terminated by ',' lines terminated by '\n' location '/user/cloudera/stock/'; ... [localhost.localdomain:21000] > show tables; query: show tables +-------+ | name | +-------+ | stock | +-------+ returned 1 row(s) in 0.01s [localhost.localdomain:21000] > select * from stock limit 3; ... +------------+-------------------+-------------------+-------------------+-------------------+--------------+-------------------+ | stock_date | stock_open | stock_high | stock_low | stock_close_ | stock_volume | stock_adjclose | +------------+-------------------+-------------------+-------------------+-------------------+--------------+-------------------+ | 2013-11-21 | 1027 | 1038.31005859375 | 1026 | 1034.069946289062 | 1091800 | 1034.069946289062 | | 2013-11-20 | 1029.949951171875 | 1033.359985351562 | 1020.359985351562 | 1022.309997558594 | 963700 | 1022.309997558594 | | 2013-11-19 | 1031.719970703125 | 1034.75 | 1023.049987792969 | 1025.199951171875 | 1116400 | 1025.199951171875 | +------------+-------------------+-------------------+-------------------+-------------------+--------------+-------------------+ returned 3 row(s) in 0.37s
the next step is to start r command line interpreter. in order to run the same impala select statement from r, we need to execute the following commands from r:
$ r > library("rodbc"); > conn <- odbcconnect("impala") > result <- sqlquery(conn, "select * from stock limit 3") > result stock_date stock_open stock_high stock_low stock_close_ stock_volume 1 2013-11-21 1027.00 1038.31 1026.00 1034.07 1091800 2 2013-11-20 1029.95 1033.36 1020.36 1022.31 963700 3 2013-11-19 1031.72 1034.75 1023.05 1025.20 1116400 stock_adjclose 1 1034.07 2 1022.31 3 1025.20
as mentioned above, if you want to use a more convenient r development environment with various advanced features such as debugging, package management, file system navigation, etc. then rstudio is an excellent choice. it can be run as a desktop application or via a web browser if you have installed rstudio server. in case of the second option, rstudio can be accessed via http://hostname:8787 and you can login using your linux username and password.
before you use rstudio from your browser, you also need to set the following environment variables in .renviron file n your home directory:
$ cat .renviron ld_library_path=/usr/local/lib:/opt/cloudera/impalaodbc/lib/64 odbcini=/etc/odbc.ini simbaini=/etc/cloudera.impalaodbc.ini
now you can login to rstudio and execute the same r commands as we have shown from the command line interpreter, see the figure below.
you can also plot a graphical representation of your data set, as show below. the diagram illustrates a line chart for google stock prices in 2013:
the actual r commands to generate this plot are as follows:
> library("rodbc"); > conn <- odbcconnect("impala") > result <- sqlquery(conn, "select stock_date, stock_close from stock where stock_date > '2013' order by stock_date asc limit 300") > result stock_date stock_close 1 2013-01-02 723.25 2 2013-01-03 723.67 3 2013-01-04 737.97 ... > plot(result$stock_close, lwd="1", xlab="days", ylab="price (usd)") > lines(result$stock_close, lwd="2") > axis(1, result$stock_date, labels=result$stock_date)
cloudera impala is an exciting new technology to provide real-time, interactive queries in hadoop environment. it supports odbc connectors and this makes it possible to integrate it with many popular bi tools and statistical software such as r. together r and impala provide an excellent combination for data analyst to process massive data sets efficiently and they can also support graphical representation of the result sets.
Published at DZone with permission of Istvan Szegedi, DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.