# libraries
library(pacman)
p_load(implyr
,odbc
,tidyverse
)
# connect to database
con <- src_impala(drv = odbc::odbc(),
HOST="host.xxx.x",
PORT=12345,
...)
# connect to table
table1 <- tbl(con,table1)
# get columns with I want to replace NAs in, in my case it's numeric tables
numeric_columns_to_replace_NAs <- table1 %>%
select_if(is.numeric) %>%
colnames
# the command which replaces all NAs with 0's
query <- table1 %>%
mutate_at(.vars = vars(numeric_columns_to_replace_NAs),
.funs = funs(coalesce(.,0)))
# run command - this will print to the console
query
# some object with the class "tbl_impala"
query_object <- query %>%
collapse() %>%
.[2]
# actual query which can be passed to hive via R.
sql_query <- query_object[[1]]$x %>%
as.character()
# create new table
new_query <- paste0("CREATE TABLE table2 AS ",sql_query) %>%
str_replace_all(pattern = "'",replacement = '"') # this cleans up the code so it works
# if you want text without characters like \n in there.
cat(sql_query)
1条答案
按热度按时间83qze16e1#
如果需要在200列中替换nas而不必键入
COALESCE(table1.column1,0)
200多次。但我可以从你的个人资料看出你可能用r。我也是,我有同样的问题:我需要用200列替换表中的nas。
我的解决方案是使用implyr和r。
你也可以
compute()
或者collect()
你的结果取决于你需要什么(见文档)如果需要配置单元中的查询,可以通过以下方式提取代码: