首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Deedle From Database,什么是Schema?

Deedle From Database,什么是Schema?
EN

Stack Overflow用户
提问于 2016-07-30 05:51:42
回答 1查看 404关注 0票数 1

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

我使用以下代码将SQL Table绑定到Deedle Frame:

代码语言:javascript
复制
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中一样。

EN

回答 1

Stack Overflow用户

发布于 2016-07-30 09:50:46

使用Frame.ofRecords,您可以将表提取到数据帧中,然后对其行或列进行操作。在本例中,我有一个非常简单的表。这是针对SQL Server的,但我假设MySQL也会有同样的效果。如果您在问题中提供更多详细信息,则可以缩小解决方案的范围。

这是按ID编制索引的表,ID为Int64:

您可以使用行或列:

代码语言:javascript
复制
#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

将得到以下输出:

代码语言:javascript
复制
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. 使用类似于上面的query { } expression来缩小数据库服务器上的数据集,并只将结果的一部分转换为数据帧。您可以进行非常复杂的转换,因此您最终可能甚至不需要数据帧。这基本上就是Linq2Sql。
  2. 在Deedle中使用lazy loading。这与序列一起工作,因此您可以获得几个序列,并重新组装专为这类事情设计的dataframe.
  3. Use Big Deedle
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/38667991

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档