其他分享
首页 > 其他分享> > 52.R连接Hive与Impala

52.R连接Hive与Impala

作者:互联网

52.1 演示环境介绍

52.2 操作演示

驱动包准备

[ec2-user@ip-168-33-22-56 hive-test]$ wget https://downloads.cloudera.com/connectors/hive_jdbc_2.5.19.1053.zip
[ec2-user@ip-168-33-22-56 hive-test]$ wget https://downloads.cloudera.com/connectors/impala_jdbc_2.5.41.1061.zip
[ec2-user@ip-168-33-22-56 hive-test]$ unzip hive_jdbc_2.5.19.1053.zip
[ec2-user@ip-168-33-22-56 2.5.19.1053 GA]$ cd 2.5.19.1053\ GA/
[ec2-user@ip-168-33-22-56 2.5.19.1053 GA]$ ll
total 17112
-rw-rw-r-- 1 ec2-user ec2-user 8414253 Jul  7 16:19 Cloudera_HiveJDBC41.zip
-rw-rw-r-- 1 ec2-user ec2-user 8398497 Jul  7 16:20 Cloudera_HiveJDBC4.zip
-r--r--r-- 1 ec2-user ec2-user  559084 Jul  4 11:30 Cloudera-JDBC-Driver-for-Apache-Hive-Install-Guide.pdf
-r--r--r-- 1 ec2-user ec2-user  117144 Jul  4 11:30 Cloudera-JDBC-Driver-for-Apache-Hive-Release-Notes.pdf
-rw-rw-r-- 1 ec2-user ec2-user   22065 Aug 10 06:26 Cloudera-JDBC-Driver-for-Apache-Hive-Release-Notes.txt
[ec2-user@ip-168-33-22-56 2.5.19.1053 GA]$
[ec2-user@ip-168-33-22-56 2.5.19.1053 GA]$ unzip Cloudera_HiveJDBC41.zip 
…
  inflating: commons-codec-1.3.jar   
  inflating: commons-logging-1.1.1.jar  
  inflating: HiveJDBC41.jar          
  inflating: hive_metastore.jar      
  inflating: hive_service.jar        
  inflating: httpclient-4.1.3.jar    
  inflating: httpcore-4.1.3.jar      
  inflating: libfb303-0.9.0.jar      
  inflating: libthrift-0.9.0.jar     
  inflating: log4j-1.2.14.jar        
  inflating: ql.jar                  
  inflating: slf4j-api-1.5.11.jar    
  inflating: slf4j-log4j12-1.5.11.jar  
  inflating: TCLIServiceClient.jar   
  inflating: zookeeper-3.4.6.jar     
[ec2-user@ip-168-33-22-56 2.5.19.1053 GA]$
[ec2-user@ip-168-33-22-56 2.5.19.1053 GA]$ mv *.jar /home/ec2-user/hivedriver

命令行测试

> install.packages("DBI")
> install.packages("RJDBC")
> install.packages("rJava")
[ec2-user@ip-168-33-22-56 hive-test]$ vim TestHive.R 
library("DBI")
library("rJava")
library("RJDBC")

hive.class.path = list.files(path=c("/opt/cloudera/parcels/CDH/lib/hive/lib"),pattern="jar", full.names=T);

cp = c(hive.class.path)
.jinit(classpath=cp,parameters="-Djavax.security.auth.useSubjectCredsOnly=false")

drv <- JDBC("org.apache.hive.jdbc.HiveDriver", "/home/ec2-user/hive-test/hivedriver/HiveJDBC41.jar",identifier.quote="`")
conn <- dbConnect(drv, "jdbc:hive2://ip-168-33-22-56.ap-southeast-1.compute.internal:10000")
show_databases <- dbGetQuery(conn, "show databases")
show_databases

tableCount <- dbGetQuery(conn, "select count(*) from tpcds_parquet.call_center")
tableCount
[ec2-user@ip-168-33-22-56 hive-test]$ Rscript TestHive.R
Loading required package: methods
…
  database_name
1       default
2 tpcds_parquet
3    tpcds_text
  _c0
1  24
[ec2-user@ip-168-33-22-56 hive-test]$
[ec2-user@ip-168-33-22-56 hive-test]$ vim TestImpala.R 
library("DBI")
library("rJava")
library("RJDBC")

impala.class.path = list.files(path=c("/home/ec2-user/hive-test/impaladriver"),pattern="jar", full.names=T)

cp = c(impala.class.path)
.jinit(classpath=cp,parameters="-Djavax.security.auth.useSubjectCredsOnly=false")

drv <- JDBC("com.cloudera.impala.jdbc41.Driver", "/home/ec2-user/hive-test/impaladriver/ImpalaJDBC41.jar",identifier.quote="`")
conn <- dbConnect(drv, "jdbc:impala://ip-168-33-22-103.ap-southeast-1.compute.internal:21050")
show_databases <- dbGetQuery(conn, "show databases")
show_databases

tableCount <- dbGetQuery(conn, "select count(*) from tpcds_parquet.call_center")
tableCount
[ec2-user@ip-168-33-22-56 hive-test]$ Rscript TestImpala.R 
Loading required package: methods
              name                                      comment
1 _impala_builtins System database for Impala builtin functions
2          default                        Default Hive database
3    tpcds_parquet                                             
4       tpcds_text                                             
  expr_0
1     24
[ec2-user@ip-168-33-22-56 hive-test]$ 

CDSW测试

install.packages("DBI")
install.packages("RJDBC")
install.packages("rJava")
library("DBI")
library("rJava")
library("RJDBC")

hive.class.path = list.files(path=c("/opt/cloudera/parcels/CDH/lib/hive/lib"),pattern="jar", full.names=T)

cp = c(hive.class.path)
.jinit(classpath=cp,parameters="-Djavax.security.auth.useSubjectCredsOnly=false")

drv <- JDBC("org.apache.hive.jdbc.HiveDriver", "/driver/hivedriver/HiveJDBC41.jar",identifier.quote="`")
conn <- dbConnect(drv, "jdbc:hive2://ip-168-33-22-56.ap-southeast-1.compute.internal:10000")
show_databases <- dbGetQuery(conn, "show databases")
show_databases

tableCount <- dbGetQuery(conn, "select count(*) from tpcds_parquet.call_center")
tableCount
library("DBI")
library("rJava")
library("RJDBC")

impala.class.path = list.files(path=c("/home/ec2-user/hive-test/impaladriver"),pattern="jar", full.names=T)

cp = c(impala.class.path)
.jinit(classpath=cp,parameters="-Djavax.security.auth.useSubjectCredsOnly=false")

drv <- JDBC("com.cloudera.impala.jdbc41.Driver", "/driver/impaladriver/ImpalaJDBC41.jar",identifier.quote="`")
conn <- dbConnect(drv, "jdbc:impala://ip-168-33-22-103.ap-southeast-1.compute.internal:21050")
show_databases <- dbGetQuery(conn, "show databases")
show_databases

tableCount <- dbGetQuery(conn, "select count(*) from tpcds_parquet.call_center")
tableCount

常见问题

cpp flags   : '-I/usr/java/jdk1.6.0_31/jre/../include -I/usr/java/jdk1.6.0_31/jre/../include/linux'
java libs   : '-L/usr/java/jdk1.6.0_31/jre/lib/amd64/server -ljvm'
configure: error: One or more Java configuration variables are not set.
Make sure R is configured with full Java support (including JDK). Run
R CMD javareconf
as root to add Java support to R.

If you don't have root privileges, run
R CMD javareconf -e
to set all Java-related variables and then install rJava.

ERROR: configuration failed for package ‘rJava’
* removing ‘/usr/local/R-3.4.2/lib64/R/library/rJava’
[root@ip-168-33-22-56 ec2-user]# source /etc/profile
[root@ip-168-33-22-56 ec2-user]# R CMD javareconf   
…
JAVA_HOME        : /usr/java/jdk1.7.0_67-cloudera/jre
Java library path: $(JAVA_HOME)/lib/amd64/server
JNI cpp flags    : -I$(JAVA_HOME)/../include -I$(JAVA_HOME)/../include/linux
JNI linker flags : -L$(JAVA_HOME)/lib/amd64/server -ljvm
Updating Java configuration in /usr/local/R-3.4.2/lib64/R
Done.
[root@ip-168-33-22-56 ec2-user]# 

大数据视频推荐:
CSDN
大数据语音推荐:
企业级大数据技术应用
大数据机器学习案例之推荐系统
自然语言处理
大数据基础
人工智能:深度学习入门到精通

标签:22,56,jar,hive,52,user,Hive,Impala,ec2
来源: https://blog.csdn.net/m0_47454596/article/details/120664425