来自数据库的Deedle框架,Schema是什么?
我是Deedle的新手,在文档中找不到如何解决我的问题。来自数据库的Deedle框架,Schema是什么?
我用下面的代码的SQL表绑定到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
这些东西都工作正常becuse我无法理解的数据帧模式,为我吃惊的是,这是不是代表的表格。
我的问题是:
我怎么能按行而不是列(列是Deedle默认)访问我的数据库元素?我想知道文档中显示的内容,但不幸的是,列名不会被识别,如the CSV example in Deedle Website中所示。
使用Frame.ofRecords
可以将表提取到数据框中,然后对其行或列进行操作。在这种情况下,我有一个非常简单的表格。这是针对SQL Server的,但我认为MySQL的工作原理是一样的。如果您在问题中提供更多详细信息,解决方案可能会缩小范围。
这是表,通过ID索引,这是Int64的:
您可以用行或列的工作:
#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也可能工作。
编辑
从你的评论出现了一些大表进行工作。根据您拥有多少内存以及表格的大小,您仍然可以加载它。如果没有这些都是一些事情,你可以在日益复杂的事:
使用
query { }
expression像上面缩小数据库服务器上的数据集和公正的结果的一部分转换成数据帧。你可以做很复杂的转换,所以你最终甚至可能不需要数据框。这基本上是Linq2Sql。在Deedle中使用lazy loading。这适用于系列,因此您可以获得一些系列并重新组合数据框。
使用Big Deedle这是专为这种事情设计的。
您的答案很有趣,但请保留我的问题,如果您看到,在创建数据框之前您对行有兴趣,问题是:我将不得不阅读所有表,但它很大。我的方法是在框架中添加表引用,因此,在操作时访问框架(制作各种过滤器等),不需要读取数据库并保存在内存中。 –
你知道我怎么可以从你这样的桌子上做这个直接引用? –
@AndréClaudino你确定这是个问题吗?实际上我并没有遍历行,'query {...}'只是一个例子。不,你可能是对的,并且将大量数据转储为df可能会有问题。我会用一些大桌子来测试。但是在这种情况下,您可以使用Linq2Sql(aka查询表达式)或者[Lazy Loading](http://bluemountaincapital.github.io/Deedle/lazysource.html/)。我们可以在[Chat]中讨论(http://chat.*.com/rooms/51909/f) – s952163
因此,您设法将SQL Server连接到我所看到的类型提供程序。您还应该探索[SqlClient](http://fsprojects.github.io/FSharp.Data.SqlClient/)。关于你的问题,如果没有输入/输出和你的期望的例子,有点难说。事实上,Deedle的大部分功能都在**行**上。所以当我使用列时,我经常最终将框架转置。有没有可能你没有分配索引列?说一些类似于:'let df = df.IndexRows(“DateIndex”)'。 –
s952163
事实上,我需要知道的很简单:当我尝试访问需要引用此indez的行时,Deedle追加索引来枚举行,但我不知道是什么。 –
你可以得到索引:'df.RowIndex'和'df.RowIndex.Keys'。 – s952163