1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195
| library(ROracle) library(stringr) library(data.table) library(pipeR) library(mongolite)
Sys.setenv(TZ = "Asia/Taipei", ORA_SDTZ = "Asia/Taipei")
numRows <- 5e7
host <- "192.168.0.120" port <- 1521 sid <- "orcl" connectString <- paste0("(DESCRIPTION=", "(ADDRESS=(PROTOCOL=TCP)(HOST=", host, ")(PORT=", port, "))", "(CONNECT_DATA=(SID=", sid, ")))") con <- dbConnect(dbDriver("Oracle"), username = "system", password = "qscf12356", dbname = connectString)
diff_seconds_90days <- difftime(Sys.time(), Sys.Date()-90, units = "secs") %>>% as.integer sizePerGroup <- sample(11:2500, ceiling(numRows / mean(1:2500)) * 1.2, TRUE) %>>% `[`(. > 10) %>>% `[`(1:which(cumsum(.) >= numRows)[1]) sizePerGroup[length(sizePerGroup)] <- sizePerGroup[length(sizePerGroup)] + numRows - sum(sizePerGroup)
var2 <- sprintf("A%05i", 1:length(sizePerGroup)) var3 <- sprintf("B%03i", 1:800) dat <- mapply(function(ss, v2value){ as.data.table(list(t = Sys.time() - sample(diff_seconds_90days, 1, TRUE), v2 = v2value, v3 = sample(var3, ss, TRUE), v4 = rnorm(ss))) }, sizePerGroup, var2, SIMPLIFY = FALSE) %>>% rbindlist %>>% setnames(toupper(names(.)))
st <- proc.time()
dbWriteTable(con, "TEST_BIG_TABLE", dat) proc.time() - st
cntDF <- dbGetQuery(con, "SELECT COUNT(*) ROWCNT FROM TEST_BIG_TABLE") cat(sprintf("%8i", cntDF$ROWCNT))
dbDisconnect(con)
rm(dat, var2, var3, sizePerGroup) gc()
paras <- list(sprintf("B%03i", 1:800), seq.Date(Sys.Date()-90, Sys.Date(), "days") %>>% format("'%Y-%m-%d %H:%M:%S'")) parasName <- paste0(":para", 1:length(paras))
batchSize <- 200
groupbyVars <- "t,v2"
sql <- paste("SELECT tbl.* FROM TEST_BIG_TABLE tbl WHERE tbl.v3 in :para1", "AND tbl.t >= to_date(:para2, 'YYYY-MM-DD HH24:MI:SS')", "AND tbl.t < to_date(:para2, 'YYYY-MM-DD HH24:MI:SS') + 1", sep = "\n")
stopifnot(all(unique(str_extract_all(sql, ":para\\d")[[1]]) %in% parasName))
paras1_set <- split(paras[[1]], rep(1:(ceiling(length(paras[[1]]) / batchSize)), each = batchSize, length = length(paras[[1]]))) %>>% sapply(function(set) paste0("('", paste0(set, collapse = "','"), "')"))
loc <- rep(1, length(paras))
st <- proc.time() contUpload <- TRUE while(contUpload){ message(sprintf("Now loc is %s ...", paste0(loc, collapse = ","))) sql_implement <- sql for (i in 2:length(paras)) sql_implement <- str_replace_all(sql_implement, paste0(":para", i), paras[[i]][loc[i]]) sql_implement <- str_replace_all(sql_implement, ":para1", paras1_set[loc[1]]) con <- dbConnect(dbDriver("Oracle"), username = "system", password = "qscf12356", dbname = connectString) oraDT <- dbGetQuery(con, sql_implement) %>>% data.table %>>% setnames(tolower(names(.))) dbDisconnect(con) if (nrow(oraDT) > 0){ groupbyVarsSplit <- str_split(groupbyVars, ",")[[1]] bindingVars <- setdiff(names(oraDT), groupbyVarsSplit) expr <- paste(bindingVars, paste0("list(", bindingVars, ")"), sep = "=") %>>% c("numRecords=.N") %>>% paste(collapse = ",") %>>% (paste0(".(", ., ")")) mongoDT <- oraDT[ , eval(parse(text = expr)), by = groupbyVars] rm(oraDT); gc() ignoreOutput <- capture.output(mongoConn <- mongo("test_big_table", "big_table", "mongodb://drill:drill@192.168.0.128:27017")) timeClassNames <- c("Date", "POSIXct", "POSIXt", "POSIXlt") timeCol <- sapply(mongoDT, function(x) any(class(x) %in% timeClassNames)) %>>% (names(.[.])) groupbyVars2 <- setdiff(groupbyVarsSplit, timeCol) fieldStr <- sprintf('{"_id": 0, "numRecords": 1, %s}', paste(sprintf('"%s": 1', groupbyVars2), collapse = ",")) groupbyVarSet <- sapply(groupbyVars2, function(x) unique(mongoDT[[x]]) %>>% (paste0("\"", ., "\"")) %>>% paste(collapse = ",")) queryInfo <- paste0("\"", groupbyVars2 ,"\": {\"$in\": [", groupbyVarSet, "]}") %>>% paste(collapse = ",") %>>% (paste0("{", ., "}")) chkDT <- mongoConn$find(queryInfo, fieldStr) if (nrow(chkDT) > 0) { uploadDT <- merge(mongoDT, chkDT, all.x = TRUE, by = groupbyVars2, suffixes = c("", ".chk")) %>>% `[`(numRecords != numRecords.chk | is.na(numRecords.chk)) %>>% `[`(j = numRecords.chk := NULL) } else { uploadDT <- mongoDT } rm(mongoDT); gc() numFail <- 0 if (nrow(uploadDT) > 0){ mapply(function(i, j) paras[[i]][j], 2:length(paras), tail(loc, length(loc)-1)) %>>% paste(collapse = ",") %>>% (paste0("para1: \"", paras1_set[loc[1]], "\",", .)) %>>% sprintf(fmt = "Now upload data with set: %s") %>>% message while (numFail <= 10) { uploadStatus <- mongoConn$insert(uploadDT) if (uploadStatus) break Sys.sleep(0.1) numFail <- numFail + 1 } if (numFail > 10) { mongoConn$drop() write(sprintf("Fail to upload the data with sql:\n%s", sql_implement), 'fail_upload_DB', append = file.exists('fail_upload_DB')) } } rm(uploadDT); gc() remove(mongoConn) }
loc[1] <- loc[1] + 1 if (loc[1] > length(paras1_set)){ loc[1] <- 1 loc[2] <- loc[2] + 1 } for (i in 2:length(loc)){ if (loc[i] > length(paras[[i]])){ loc[i] <- 1 if (i == length(loc)){ contUpload <- FALSE } else { loc[i+1] <- loc[i+1] + 1 } } } } proc.time() - st
mongoConn <- mongo("test_big_table", "big_table", "mongodb://drill:drill@192.168.0.128:27017")
sprintf("%6i", mongoConn$count())
outDT <- mongoConn$find(limit = 50) %>>% data.table outDT_trans <- outDT[ , .(v3 = unlist(v3), v4 = unlist(v4)), by = "t,v2"]
remove(mongoConn)
|