Impala - 将整个 table 的 NULL 替换为零
Impala - replace NULL to zero for entire table
嗨专家那里-
是否可以将 impala 中的整个 table 的 null 替换为零?
到目前为止,我只找到了 coalesce 或 case - when,这允许我逐列更改..但是我在 table 中有超过 210 + 列,所以我正在寻找更有效的方法..
SELECT合并(table1.column1,0)
从 table1
提前致谢!
如果需要替换 200 列中的 NA 而无需键入 COALESCE(table1.column1,0)
200 多次,这并不能回答一般性问题。
但我从您的个人资料中可以看出您可能使用 R。我也是,我也有同样的问题:我需要用 200 列替换 table 中的 NA。
我的解决方案是将 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()
结果(参见 docs)
如果需要在hive中查询,可以这样提取代码:
# 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)
嗨专家那里-
是否可以将 impala 中的整个 table 的 null 替换为零? 到目前为止,我只找到了 coalesce 或 case - when,这允许我逐列更改..但是我在 table 中有超过 210 + 列,所以我正在寻找更有效的方法..
SELECT合并(table1.column1,0) 从 table1
提前致谢!
如果需要替换 200 列中的 NA 而无需键入 COALESCE(table1.column1,0)
200 多次,这并不能回答一般性问题。
但我从您的个人资料中可以看出您可能使用 R。我也是,我也有同样的问题:我需要用 200 列替换 table 中的 NA。
我的解决方案是将 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()
结果(参见 docs)
如果需要在hive中查询,可以这样提取代码:
# 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)