将整个表的null替换为零

14ifxucb  于 2021-06-26  发布在  Impala
关注(0)|答案(1)|浏览(384)

嗨,Maven们-
对于impala中的整个表,是否仍有将null替换为零的方法?到目前为止,我只找到了coalesce或case-when,它允许我逐列更改..但是我的表中有超过210+列,所以如果可能的话,我正在寻找更有效的东西。。
从表1中选择coalesce(表1.column1,0)
提前谢谢!

83qze16e

83qze16e1#

如果需要在200列中替换nas而不必键入 COALESCE(table1.column1,0) 200多次。
但我可以从你的个人资料看出你可能用r。我也是,我有同样的问题:我需要用200列替换表中的nas。
我的解决方案是使用implyr和r。


# 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

你也可以 compute() 或者 collect() 你的结果取决于你需要什么(见文档)
如果需要配置单元中的查询,可以通过以下方式提取代码:


# 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)

相关问题