rstudio找不到r低版本_R本地连接Hive(Mac详解)

目录准备工作确认hive server2的ip、端口和访问权限Mac本地安装RstudioRstudio安装jar包创建R连接hive脚本函数测试调用R脚本的函数1 准备工作1.1 确认hive server2的ip、端口和访问权限找维护hadoop的同事, 确认hive server2的ip和端口确认hive server的ip在本地能否ping通打开终端, 输入ping ip地址, 如出现下图...
摘要由CSDN通过智能技术生成

目录

  1. 准备工作
    1. 确认hive server2的ip、端口和访问权限
    2. Mac本地安装Rstudio
    3. Rstudio安装jar包
  2. 创建R连接hive脚本函数
  3. 测试
  4. 调用R脚本的函数

1 准备工作

1.1 确认hive server2的ip、端口和访问权限

  1. 找维护hadoop的同事, 确认hive server2的ip和端口
  2. 确认hive server的ip在本地能否ping通

打开终端, 输入ping ip地址, 如出现下图则表示能够ping通,

86f783e2ccf53fc43ab72004cca20f7c.png

3. 再次确认hive server2的端口在本地能否访问

这里需要用到telnet, 因此先在终端配置telnet

  • 打开终端, 输入
/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
  • 下载成功后, 输入
brew install telnet
  • 使用telnet确认hive server2的端口在本地能否访问, 因此继续在终端输入
telnet ip 端口

如果出现Connected to ip.的提示则说明可访问啦, 反之如果一直显示Trying ip...则说明不可访问, 要么是ip和端口错误, 要么是可能没有访问的权限, 需找hadoop同事确认清楚, 或者申请权限去~

1.2 Mac本地安装Rstudio

首先说明: R是运行环境, Rstudio是开发工具(为了方便使用R语言而生).

因此需先后安装:

1) R语言环境: https://cran.r-project.org/

2) Rstudio: https://www.rstudio.com/products/rstudio/download/

1.3 Rstudio安装jar包

在Rconsole输入

install.packages('DBI')
install.packages('rJava')
install.packages('RJDBC')
devtools::install_github("lengyuyeke/RHJDBC") #注意: 有的Rstudio版本不支持安装RHJDBC,直接github上安装可解决版本限制问题

2 创建R连接hive脚本函数

可将以下另存为一个R脚本, 比如起名叫function.R, 之后每次连接hive的时候, 直接调用本地的该脚本即可, 避免每次都写一大坨.

options(java.parameters = "-Xmx8g") # set jvm
library(DBI)
library(RJDBC)
# 连接 Hive
init_jdbc <- function() {
  require(RHJDBC)
  cp <- dir(system.file("java", package = "RHJDBC"), full.names = TRUE) # you can use your own jdbc driver
  .jinit(classpath = cp) # init
  drv <- JDBC("org.apache.hive.jdbc.HiveDriver") # set driver
  con <- dbConnect(drv,
                   "jdbc:hive2://ip:终端/default",
                   "username",
                   "password"
  ) # set connection,url,username,password
  class(con) <- "JHDBCConnection" # change the class of con
  con
}

# 查询 Hive
get_hive_query <- function(query) {
  print(query)
  conn <- init_jdbc()
  data <- try(dbGetQuery(conn, query), silent = TRUE)
  dbDisconnect(conn)
  if (class(data) %in% c("error", "try-error")) {
    return()
  }
  if (nrow(data) > 0) {
    colnames(data) <- gsub("(.*).(.*)", "2", colnames(data))
    data.table::setDT(data)
    data
  }
}

3 测试

dat<- get_hive_query("select * from tmp.test") 

4 调用R脚本的函数

source("//Users/hanqianyun/Desktop/function.R")
dat<- get_hive_query("select * from tmp.test") 

over~

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值