mysql - 来自数据库的 Deedle Frame,什么是 Schema?

标签 mysql sql-server database f# deedle

我是 Deedle 的新手,在文档中我找不到如何解决我的问题。

我使用以下代码将 SQL 表绑定(bind)到 Deedle 框架:

namespace teste

open FSharp.Data.Sql
open Deedle
open System.Linq

module DatabaseService =

    [<Literal>]
    let connectionString = "Data Source=*********;Initial Catalog=*******;Persist Security Info=True;User ID=sa;Password=****";

    type bd = SqlDataProvider<
                ConnectionString = connectionString,
                DatabaseVendor = Common.DatabaseProviderTypes.MSSQLSERVER >

    type Database() =

        static member contextDbo() =
            bd.GetDataContext().Dbo

        static member acAgregations() =
            Database.contextDbo().AcAgregations |> Frame.ofRecords

        static member acBusyHourDefinition() =
            Database.contextDbo().AcBusyHourDefinition
            |> Frame.ofRecords "alternative_reference_table_scan", "formula"]

        static member acBusyHourDefinitionFilterByTimeAgregationTipe(value:int) = 
            Database.acBusyHourDefinition()
            |> Frame.getRows

这些东西工作正常,因为我无法理解 Data Frame Schema,令我惊讶的是,这不是表的表示。

我的问题是:

如何按行而不是按列访问我的数据库元素(列是 Deedle 默认值)?我知道文档中显示的内容,但不幸的是,无法识别列名称,如 the CSV example in Deedle Website 中所示。 .

最佳答案

使用 Frame.ofRecords,您可以将表提取到数据框中,然后对其行或列进行操作。在这种情况下,我有一个非常简单的表。这是针对 SQL Server 的,但我认为 MySQL 也能正常工作。如果您在问题中提供更多详细信息,则可以缩小解决方案的范围。

这是一张表,由 ID 索引,它是 Int64:

enter image description here

您可以使用行或列:

#if INTERACTIVE
#load @"..\..\FSLAB\packages\FsLab\FsLab.fsx"
#r "System.Data.Linq.dll"
#r "FSharp.Data.TypeProviders.dll"
#endif

//open FSharp.Data
//open System.Data.Linq
open Microsoft.FSharp.Data.TypeProviders
open Deedle

[<Literal>]
let connectionString1 = @"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=C:\Users\userName\Documents\tes.sdf.mdf"

type dbSchema = SqlDataConnection<connectionString1>
let dbx = dbSchema.GetDataContext()

let table1 = dbx.Table_1


query { for row in table1 do
        select row} |> Seq.takeWhile (fun x -> x.ID < 10L) |> Seq.toList
// check if we can connect to the DB.

let df = table1 |> Frame.ofRecords // pull the table into a df
let df = df.IndexRows<System.Int64>("ID") // if you need an index
df.GetRows(2L) // Get the second row, but this can be any kind of index/key 
df.["Number"].GetSlice(Some 2L, Some 5L) // get  the 2nd to 5th row from the Number column

将为您提供以下输出:

val it : Series<System.Int64,float> = 
2 -> 2 

> 
val it : Series<System.Int64,float> = 
2 -> 2 
3 -> 3 
4 -> 4 
5 -> 5 

取决于您要做什么 Selecting Specific Rows in Deedle也可能有效。

编辑

从您的评论来看,您似乎正在使用一些大表。取决于您拥有多少内存以及您仍然可以加载它的表有多大。如果不是,这些是您可以在增加复杂性时做的一些事情:

  1. 使用查询{} expression像上面一样缩小数据库服务器上的数据集并将结果的一部分转换为数据框。您可以进行非常复杂的转换,因此您最终甚至可能不需要数据框。这基本上是 Linq2Sql。

  2. 使用 lazy loading在迪德尔。这适用于系列,因此您可以获得一些系列并重新组装数据框。

  3. 使用 Big Deedle这是为这类事情设计的。

关于mysql - 来自数据库的 Deedle Frame,什么是 Schema?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38667991/

相关文章:

sql-server - 在 SQL Server 2008 R2 中开始调试时发生错误,错误 HRESULT E_FAIL 已从对 COM 组件的调用返回。 (mscorlib)

.Net 将 NULL 值从变量值插入到 SQL Server 数据库中

sql - 在 IBM DB2 中选择列的子集

database - Oracle 索引勉强加速聚合计算

php 分页完整页面列表而不是带有下一个和上一个的小列表

python - 在哪里可以获得 apache2/module 的 mod_wsgi.so 文件?

mysql - 如何在 rsyslog 中仅插入来自常规日志的 mac 地址?

sql - 在sql server中插入阿拉伯语单词

sql-server - 数据库设计

mysql - 了解 MySQL 的 Web 应用程序权限