Skip to content

Commit

Permalink
add README example
Browse files Browse the repository at this point in the history
  • Loading branch information
kongdd committed Dec 15, 2023
1 parent c620688 commit bb4f282
Show file tree
Hide file tree
Showing 9 changed files with 245 additions and 81 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/R-CMD-check.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ jobs:
fail-fast: false
matrix:
config:
- {os: macos-latest, r: 'release'}
# - {os: macos-latest, r: 'release'}
- {os: windows-latest, r: 'release'}
# - {os: ubuntu-latest, r: 'devel', http-user-agent: 'release'}
- {os: ubuntu-latest, r: 'release'}
Expand Down
57 changes: 0 additions & 57 deletions R/open_dbase.R

This file was deleted.

25 changes: 25 additions & 0 deletions R/open_mariadb.R
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)
}
47 changes: 47 additions & 0 deletions R/open_mysql.R
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)
}
)
14 changes: 0 additions & 14 deletions R/tool_dbase.R
Original file line number Diff line number Diff line change
@@ -1,17 +1,3 @@

#' @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)
}
)

#' @export
db_info <- function(con) {
str(dbGetInfo(con))
Expand Down
72 changes: 72 additions & 0 deletions README.Rmd
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()
```
78 changes: 76 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@

# 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) -->
Expand All @@ -16,9 +18,9 @@

## TODO

- [ ] 读取数据的性能测试
- [ ] 读取数据的性能测试

- [ ] 添加写入数据的函数
- [ ] 添加写入数据的函数

## Installation

Expand All @@ -32,4 +34,76 @@ This is a basic example which shows you how to solve a common problem:

``` r
library(tidydb2)
library(dplyr)
#
# Attaching package: 'dplyr'
# The following objects are masked from 'package:stats':
#
# filter, lag
# The following objects are masked from 'package:base':
#
# intersect, setdiff, setequal, union
library(Ipaper)
# Loading required package: magrittr
# Registered S3 method overwritten by 'Ipaper':
# method from
# print.data.table data.table

## hourly气象数据
dbinfo <- get_dbInfo("duckdb")
f <- dbinfo$hourly_2020_2022$db
db <- dbase$new(f)
db
# [db ]: z:/DATA/China/ChinaMet_hourly_mete2000/data/China_Mete2000_hourly_full_2020-2022_tidy.duckdb
# [size ]: 4133.5 Mb
# [Opened Table]: China_Mete2000_hourly_full_2020-2022
# [ALL Tables]: China_Mete2000_hourly_full_2020-2022
# # Source: table<China_Mete2000_hourly_full_2020-2022> [?? x 20]
# # Database: DuckDB v0.9.2 [hydro@Windows 10 x64:R 4.3.2/z:/DATA/China/ChinaMet_hourly_mete2000/data/China_Mete2000_hourly_full_2020-2022_tidy.duckdb]
# date site PRE_1h PRS PRS_Max PRS_Min PRS_Sea RHU RHU_Min
# <dttm> <int> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
# 1 2020-01-01 00:00:00 45011 NA NA NA NA 1028. NA NA
# 2 2020-01-01 00:00:00 50136 0 971. 971. 970. 1032. 68 67
# 3 2020-01-01 00:00:00 50137 0 989. 989. 988. 1030. 70 67
# 4 2020-01-01 00:00:00 50246 0 977. 977. 977. 1027. 53 47
# 5 2020-01-01 00:00:00 50247 0 960. 960. 960. 1032. 68 68
# 6 2020-01-01 00:00:00 50349 0 960. 960. 959. 1027 44 43
# 7 2020-01-01 00:00:00 50353 0 999. 999. 998. 1024. 68 68
# 8 2020-01-01 00:00:00 50425 0 959. 959. 958. 1041. 69 68
# 9 2020-01-01 00:00:00 50431 0 940. 940. 939. 1040. 68 67
# 10 2020-01-01 00:00:00 50434 0 937 937 937. 1039. 67 67
# # ℹ more rows
# # ℹ 11 more variables: TEM <dbl>, TEM_Max <dbl>, TEM_Min <dbl>, tigan <dbl>,
# # WIN_D_Avg_2mi <dbl>, WIN_D_INST_Max <dbl>, WIN_D_S_Max <dbl>,
# # WIN_S_Avg_2mi <dbl>, WIN_S_Inst_Max <dbl>, WIN_S_Max <dbl>, windpower <dbl>

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()
)
# user system elapsed
# 0.64 0.30 3.03

print(d)
# # A tibble: 8,759 × 5
# date PRS TEM RHU tigan
# <dttm> <dbl> <dbl> <dbl> <dbl>
# 1 2022-01-01 01:00:00 978 -31.1 71 -36.3
# 2 2022-01-01 02:00:00 977. -29.9 70 -35.0
# 3 2022-01-01 03:00:00 976. -28.6 69 -33.6
# 4 2022-01-01 04:00:00 975. -26.6 66 -31.5
# 5 2022-01-01 05:00:00 974. -25.7 63 -30.6
# 6 2022-01-01 06:00:00 973. -24.7 61 -29.5
# 7 2022-01-01 07:00:00 973. -24.4 61 -29.2
# 8 2022-01-01 08:00:00 973. -24.8 63 -29.6
# 9 2022-01-01 09:00:00 972. -25.1 65 -29.9
# 10 2022-01-01 10:00:00 972. -25.2 72 -29.8
# # ℹ 8,749 more rows
db$close()
```
12 changes: 6 additions & 6 deletions man/open_mysql.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

19 changes: 18 additions & 1 deletion scripts/test_dbase.R
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)

0 comments on commit bb4f282

Please sign in to comment.