<small id='1hPph'></small><noframes id='1hPph'>

      <bdo id='1hPph'></bdo><ul id='1hPph'></ul>

    <legend id='1hPph'><style id='1hPph'><dir id='1hPph'><q id='1hPph'></q></dir></style></legend>
    <i id='1hPph'><tr id='1hPph'><dt id='1hPph'><q id='1hPph'><span id='1hPph'><b id='1hPph'><form id='1hPph'><ins id='1hPph'></ins><ul id='1hPph'></ul><sub id='1hPph'></sub></form><legend id='1hPph'></legend><bdo id='1hPph'><pre id='1hPph'><center id='1hPph'></center></pre></bdo></b><th id='1hPph'></th></span></q></dt></tr></i><div id='1hPph'><tfoot id='1hPph'></tfoot><dl id='1hPph'><fieldset id='1hPph'></fieldset></dl></div>
    <tfoot id='1hPph'></tfoot>

    1. Slick 中如何使用反应流来插入数据

      时间:2023-06-25
      <legend id='zDhfb'><style id='zDhfb'><dir id='zDhfb'><q id='zDhfb'></q></dir></style></legend>
        <tbody id='zDhfb'></tbody>
      <tfoot id='zDhfb'></tfoot>

    2. <small id='zDhfb'></small><noframes id='zDhfb'>

          • <bdo id='zDhfb'></bdo><ul id='zDhfb'></ul>

                <i id='zDhfb'><tr id='zDhfb'><dt id='zDhfb'><q id='zDhfb'><span id='zDhfb'><b id='zDhfb'><form id='zDhfb'><ins id='zDhfb'></ins><ul id='zDhfb'></ul><sub id='zDhfb'></sub></form><legend id='zDhfb'></legend><bdo id='zDhfb'><pre id='zDhfb'><center id='zDhfb'></center></pre></bdo></b><th id='zDhfb'></th></span></q></dt></tr></i><div id='zDhfb'><tfoot id='zDhfb'></tfoot><dl id='zDhfb'><fieldset id='zDhfb'></fieldset></dl></div>
                本文介绍了Slick 中如何使用反应流来插入数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                在 Slick 的文档中,提供了使用反应流的示例仅用于读取数据作为 DatabasePublisher 的一种方式.但是,当您想根据插入率将数据库用作 Sink 和 backpreasure 时会发生什么?

                In Slick's documentation examples for using Reactive Streams are presented just for reading data as a means of a DatabasePublisher. But what happens when you want to use your database as a Sink and backpreasure based on your insertion rate?

                我已经寻找了等效的DatabaseSubscriber,但它不存在.所以问题是,如果我有来源,请说:

                I've looked for equivalent DatabaseSubscriber but it doesn't exist. So the question is, if I have a Source, say:

                <代码>val 源 = 源(0 到 100)

                我怎样才能用 Slick 构造一个 Sink 来将这些值写入一个带有模式的表中:

                how can I crete a Sink with Slick that writes those values into a table with schema:

                <代码>创建表 NumberTable(值 INT)

                推荐答案

                Serial Inserts

                最简单的方法是在一个接收器.foreach.

                The easiest way would be to do inserts within a Sink.foreach.

                假设您已经使用了模式代码生成和进一步假设您的表名为NumberTable"

                Assuming you've used the schema code generation and further assuming your table is named "NumberTable"

                //Tables file was auto-generated by the schema code generation
                import Tables.{Numbertable, NumbertableRow} 
                
                val numberTableDB = Database forConfig "NumberTableConfig"
                

                我们可以写一个函数来进行插入

                We can write a function that does the insertion

                def insertIntoDb(num : Int) = 
                  numberTableDB run (Numbertable += NumbertableRow(num))
                

                那个函数可以放在Sink中

                And that function can be placed in the Sink

                val insertSink = Sink[Int] foreach insertIntoDb
                
                Source(0 to 100) runWith insertSink
                

                批量插入

                您可以通过一次批处理 N 个插入来进一步扩展 Sink 方法:

                You could further extend the Sink methodology by batching N inserts at a time:

                def batchInsertIntoDb(nums : Seq[Int]) = 
                  numberTableDB run (Numbertable ++= nums.map(NumbertableRow.apply))
                
                val batchInsertSink = Sink[Seq[Int]] foreach batchInsertIntoDb
                

                这个批处理 Sink 可以由一个 Flow 提供,它进行批量分组:

                This batched Sink can be fed by a Flow which does the batch grouping:

                val batchSize = 10
                
                Source(0 to 100).via(Flow[Int].grouped(batchSize))
                                .runWith(batchInsertSink)
                

                这篇关于Slick 中如何使用反应流来插入数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                上一篇:SQL Server 2005“公共"数据库角色似乎不适用? 下一篇:Alfresco : 在此服务器上找不到 Alfresco 存储库

                相关文章

                <small id='6fFOP'></small><noframes id='6fFOP'>

                1. <tfoot id='6fFOP'></tfoot>
                  1. <i id='6fFOP'><tr id='6fFOP'><dt id='6fFOP'><q id='6fFOP'><span id='6fFOP'><b id='6fFOP'><form id='6fFOP'><ins id='6fFOP'></ins><ul id='6fFOP'></ul><sub id='6fFOP'></sub></form><legend id='6fFOP'></legend><bdo id='6fFOP'><pre id='6fFOP'><center id='6fFOP'></center></pre></bdo></b><th id='6fFOP'></th></span></q></dt></tr></i><div id='6fFOP'><tfoot id='6fFOP'></tfoot><dl id='6fFOP'><fieldset id='6fFOP'></fieldset></dl></div>

                    • <bdo id='6fFOP'></bdo><ul id='6fFOP'></ul>

                    <legend id='6fFOP'><style id='6fFOP'><dir id='6fFOP'><q id='6fFOP'></q></dir></style></legend>