generated from rpkgs/rpkg.template
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
245 additions
and
81 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
#' @rdname open_mysql | ||
#' @export | ||
open_mariadb <- function(dbname=1, dbinfo=NULL) { | ||
dbinfo = dbinfo %||% get_dbInfo() | ||
|
||
if (is.numeric(dbname)) dbname = dbinfo$dbname[dbname] | ||
bold(ok(sprintf("[info] opening db: %s", dbname))) | ||
|
||
# dev = odbc::odbc() | ||
# odbc::odbcListDrivers() | ||
dev = RMariaDB::MariaDB() | ||
port = 3306 | ||
if (!is.null(dbinfo$port)) port = dbinfo$port | ||
|
||
con <- dbConnect(dev, | ||
load_data_local_infile = TRUE, | ||
host=dbinfo$host, port = port, | ||
user=dbinfo$user, | ||
password=as.character(dbinfo$pwd), | ||
database = dbname, | ||
dbname = dbname) | ||
|
||
str(dbGetInfo(con)) | ||
return(con) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
#' Con database connection | ||
#' | ||
#' @examples | ||
#' \dontrun{ | ||
#' dbinfo <- get_dbInfo() # see which db to read | ||
#' open_mysql() | ||
#' open_mariadb() | ||
#' } | ||
#' | ||
#' @export | ||
#' @import DBI | ||
open_mysql <- function(dbname = 1, dbinfo = NULL) { | ||
dbinfo <- dbinfo %||% get_dbInfo() | ||
|
||
if (is.numeric(dbname)) dbname <- dbinfo$dbname[dbname] | ||
bold(Ipaper::ok(sprintf("[info] opening db: %s", dbname))) | ||
|
||
dev <- RMySQL::MySQL() | ||
# dev = odbc::odbc() | ||
# odbc::odbcListDrivers() | ||
port <- 3306 | ||
if (!is.null(dbinfo$port)) port <- dbinfo$port | ||
|
||
con <- dbConnect(dev, | ||
# driver = "MySQL ODBC 8.1 ANSI Driver", | ||
host = dbinfo$host, port = port, | ||
user = dbinfo$user, | ||
password = as.character(dbinfo$pwd), | ||
dbname = dbname | ||
) | ||
return(con) | ||
} | ||
|
||
|
||
#' @import DBI dplyr crayon | ||
#' @importMethodsFrom DBI dbSendQuery | ||
#' @importMethodsFrom RMySQL dbSendQuery | ||
setMethod( | ||
"dbSendQuery", c("MySQLConnection", "character"), | ||
# import S4 method from RMySQL | ||
function(conn, statement, ...) { | ||
RMySQL:::checkValid(conn) | ||
|
||
rsId <- .Call(RMySQL:::RS_MySQL_exec, conn@Id, as.character(statement)) | ||
new("MySQLResult", Id = rsId) | ||
} | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
--- | ||
output: github_document | ||
--- | ||
|
||
```{r, echo = FALSE} | ||
knitr::opts_chunk$set( | ||
collapse = TRUE, | ||
comment = "#", | ||
fig.width = 10, fig.height = 5, | ||
fig.align = "center", | ||
fig.path = "man/Figure/", | ||
dev = 'svg' | ||
) | ||
``` | ||
|
||
# tidydb2 | ||
|
||
<!-- badges: start --> | ||
[![R-CMD-check](https://github.com/rpkgs/tidydb2/actions/workflows/R-CMD-check.yaml/badge.svg)](https://github.com/rpkgs/tidydb2/actions/workflows/R-CMD-check.yaml) | ||
[![codecov](https://codecov.io/gh/rpkgs/tidydb2/branch/master/graph/badge.svg)](https://app.codecov.io/gh/rpkgs/tidydb2) | ||
<!-- [![CRAN](http://www.r-pkg.org/badges/version/tidydb2)](https://cran.r-project.org/package=tidydb2) --> | ||
<!-- [![total](http://cranlogs.r-pkg.org/badges/grand-total/tidydb2)](https://www.rpackages.io/package/tidydb2) --> | ||
<!-- [![monthly](http://cranlogs.r-pkg.org/badges/tidydb2)](https://www.rpackages.io/package/tidydb2) --> | ||
<!-- badges: end --> | ||
|
||
## Goals | ||
|
||
> 更便捷的访问数据库 | ||
建立气象数据库,一行命令查询数据。 | ||
|
||
## TODO | ||
|
||
- [ ] 读取数据的性能测试 | ||
|
||
- [ ] 添加写入数据的函数 | ||
|
||
## Installation | ||
|
||
```r | ||
remotes::install_github("rpkgs/tidydb2") | ||
``` | ||
|
||
## Example | ||
|
||
This is a basic example which shows you how to solve a common problem: | ||
|
||
```{r} | ||
library(tidydb2) | ||
library(dplyr) | ||
library(Ipaper) | ||
## hourly气象数据 | ||
dbinfo <- get_dbInfo("duckdb") | ||
f <- dbinfo$hourly_2020_2022$db | ||
db <- dbase$new(f) | ||
db | ||
site0 = 50246L | ||
date0 <- make_datetime(2022, 1, 1, 0) | ||
# 0.1s可以读回数据, 这里应该是UTC时间 | ||
system.time( | ||
d <- db$tbl |> | ||
filter(site == site0, date > date0) |> | ||
select(date, PRS, TEM, RHU, tigan) |> | ||
collect() | ||
) | ||
print(d) | ||
db$close() | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,35 @@ | ||
library(dplyr) | ||
library(duckdb) | ||
library(Ipaper) | ||
|
||
## hourly气象数据 | ||
dbinfo <- get_dbInfo("duckdb") | ||
f = dbinfo$hourly_2020_2022$db | ||
db = dbase$new(f) | ||
db | ||
|
||
site0 = 50246L | ||
date0 <- make_datetime(2022, 1, 1, 0) | ||
|
||
# 0.1s可以读回数据, 这里应该是UTC时间 | ||
system.time( | ||
d <- db$tbl |> | ||
filter(site == site0, date > date0) |> | ||
select(date, PRS, TEM, RHU, tigan) |> | ||
collect() | ||
) | ||
|
||
print(d) | ||
db$close() | ||
|
||
|
||
d <- db$read_data(site == 50246L) # first time about 20s, 16s可以读进来所有数据 | ||
d <- db$read_data(site == 50246L) # 0.6s | ||
d <- db$read_data(site == 50246L & date > date0) # 0.6s | ||
# db$close() | ||
|
||
## 径流数据 | ||
f = "z:/DATA/China/ChinaRunoff_latest/ChinaWater_latest/back_up/ChinaWater_river (20200603H22-20210116H14).db" | ||
db <- dbase$new(f) | ||
db | ||
db$read_data(stcd == 10701210L) | ||
|