`
winzenghua
  • 浏览: 1357923 次
  • 性别: Icon_minigender_2
  • 来自: 广州
文章分类
社区版块
存档分类
最新评论

如何使用pipeline function获得实时输出

阅读更多

如何使用 pipelinefunction 获得实时输

create type lookup_row as

record ( idx number, text varchar2(20) );

create type lookups_tab as table of lookup_row;

create or replace function Lookups_Fn

return lookups_tab

pipelined

is

v_row lookup_row;

begin

for j in 1..10

loop

v_row :=

case j

when 1 then lookup_row ( 1, 'one' )

when 2 then lookup_row ( 2, 'TWO' )

when 3 then lookup_row ( 3, 'three' )

when 4 then lookup_row ( 4, 'FOUR' )

when 5 then lookup_row ( 5, 'five' )

when 6 then lookup_row ( 6, 'SIX' )

when 7 then lookup_row ( 7, 'seven' )

else lookup_row ( j, 'other' )

end;

pipe row ( v_row );

end loop;

return;

end Lookups_Fn ;

很多人都知道,在普通的函数中,使用 dbms_output 输出的信息,需要在服务器执行完整个函数后一次性的返回给客户端。但你如果 需要在客户端实时的输出函数执行过程中的一些信息,在 Oracle 9i 以后则可以使用管道函数 (pipeline function)

PIPELINED (关键字)表明这是一个管道函数,管道函数的返回值类型必须为集合,在函数中, PIPE ROW 语句被用来返回该集合的单个元素,函数则以一个空的 RETURN 语句结束,以表明它已经完成。

create or replace type MsgType as table of varchar2(4000);

/

create or replace function f_pipeline_test

return MsgType

PIPELINED

as

begin

for i in 1 .. 10

loop

pipe row( 'Iteration ' || i || ' at ' || systimestamp );

dbms_lock.sleep(1);

end loop;

pipe row( 'All done!' );

return;

e nd;

/

sql*plus 中执行该函数,大家需要首先设置 arraysize 1 ,否则服务器会按照默认的 15 来向客户端返回信息,这会影响我们的测试效果。

SQL> set arraysize 1

SQL> select * from table( f_pipeline_test );

COLUMN_VALUE

-----------------------------------------------------

Iteration 1 at 14-FEB-08 02.13.18.273988 000 PM +08:00

Iteration 2 at 14-FEB-08 02.13.19.275988000 PM +08:00

Iteration 3 at 14-FEB-08 02.13.20.277767000 PM +08:00

Iteration 4 at 14-FEB-08 02.13.21.279591000 PM +08:00

Iteration 5 at 14-FEB-08 02.13.22.281366000 PM +08:00

Iteration 6 at 14-FEB-08 0 2.13.23.283189000 PM +08:00

Iteration 7 at 14-FEB-08 02.13.24.283965000 PM +08:00

Iteration 8 at 14-FEB-08 02.13.25.285785000 PM +08:00

Iteration 9 at 14-FEB-08 02.13.26.286570000 PM +08:00

Iteration 10 at 14-FEB-08 02.13.27.288387000 PM +08:00

All done!

11 rows selected.

如果要在 pipeline 中执行 DML 操作,则必须使用自治事务,否则会报 ORA-14551 错误

create or replace function f_pipeline_testdml

return MsgType

PIPELINED

as

begin

for i in 1 .. 10

loop

insert into test values(1);

pipe row( 'insert into test val ues( ' || i || ') success at ' || systimestamp );

dbms_lock.sleep(1);

end loop;

pipe row( 'All done!' );

return;

end;

/

SQL> select * from table( f_pipeline_testdml );

select * from table( f_pipeline_testdml )

*

ERROR at line 1:

ORA-14551: cannot perform a DML operation inside a query

ORA-06512: at "NING.F_PIPELINE_TESTDML", line 8

create or replace function f_pipeline_testdml

return MsgType

PIPELINED

as

pragma autonomous_transaction;

begin

for i in 1 .. 10

loop

insert into test values(1);

commit;

pipe row( 'insert values ' || i || ' success at ' || systimestamp );

dbms_lock.sleep(1);

end loop;

pipe row( 'All done!' );

return;

end;

/

SQL> select * from table( f_pipeline_testdml );

COLUMN_V ALUE

--------------------------------------------------------------------------------

insert values 1 success at 14-FEB-08 02.16.47.855158000 PM +08:00

insert values 2 success at 14-FEB-08 02.16.48.865559000 PM +08:00

insert values 3 success at 14-FE B-08 02.16.49.867377000 PM +08:00

insert values 4 success at 14-FEB-08 02.16.50.873154000 PM +08:00

insert values 5 success at 14-FEB-08 02.16.51.874942000 PM +08:00

insert values 6 success at 14-FEB-08 02.16.52.880781000 PM +08:00

insert values 7 success at 14-FEB-08 02.16.53.882543000 PM +08:00

insert values 8 success at 14-FEB-08 02.16.54.894348000 PM +08:00

insert values 9 success at 14-FEB-08 02.16.55.896153000 PM +08:00

insert values 10 success at 14-FEB-08 02.16.56.9019 04000 PM +08:00

All done!

11 rows selected.

Oracle 9205 及其之后的版本中,在 pipeline function 中使用自治事务,则必须在 pipe row 之前提交或者回滚事务,否则会报 ORA-06519 错误。

create or replace function f_pipeline_testdml

return MsgType

PIPELINED

as

pragma autonomous_transaction;

begin

for i i n 1 .. 10

loop

insert into test values(1);

pipe row( 'insert values ' || i || ' success at ' || systimestamp );

dbms_lock.sleep(1);

end loop;

pipe row( 'All done!' );

commit;

return;

end;

/

SQL> select * from table( f_pipeline_testdml );

select * from table( f_pipeline_testdml )

*

ERROR at line 1:

ORA-06519: active autonomous transaction detected and rolled back

ORA-06512: at "NING.F_PIPELINE_TESTDML", line 10

此处是由于在 9205 中修复 Bug 2711518 导致了自治事务的行为有所改变。如果系统从 9205 之前的版本升级到之后 的版本,需要保证 pipeline function 的行为和以前版本一致, Oracle 提供了一个 10946 事件来设置和以前版本的兼容性,如果在管道函数中使用了 select for update cursor ,则必须设置 event 回归以前的特性,否则即使在 pipe row 之前 commit 也会导致出现 ORA-1002 错误。

ALTER SYSTEM SET EVENT = "10946 trace name context forever, level 8" scope=spfile;

分享到:
评论

相关推荐

    Pipeline:MyFirstPipeLine

    3. **Function接口**: `java.util.function.Function`是Java 8提供的一个关键接口,用于表示接受一个参数并产生一个结果的函数。它可以作为管道中的处理步骤。 4. **Compose和Chain**: 在Java 8中,可以通过`and...

    PipePie:回调管道

    派介绍PipePie 是一个通用工具,用于将一组回调应用于某些输入数据以获得某些输出数据。 use Toobo \ PipePie \ Pipeline ;$ pipeline = ( new Pipeline ()) -> pipe ( function ( $ carry ) { return $ carry . 'B'...

    2009 达内Unix学习笔记

    本来命令是通过键盘得到输入的,但是用小于号,就能够使命令从文件中得到输入。 \ 表示未写完,回车换行再继续。 * 匹配零个或者多个字符。 ? 匹配一个字符。 [] 匹配中括号里的内容[a-z][A-Z][0-9]。 ! 事件...

    第二课:mongodb企业级应用管理1

    MongoDB 是一个流行的开源NoSQL数据库系统,以其灵活性和高性能在企业级应用中得到广泛应用。在本课程“第二课:mongodb企业级应用管理1”中,我们将深入探讨MongoDB的几个核心特性,包括聚合操作、复制集群以及选举...

    前端开源库-gulp-helptext.zip

    **gulp-help-text** 在前端开发领域,自动化工具的...通过它的使用,前端项目不仅在代码层面实现了自动化,也在文档和协作方面得到了提升。熟练掌握`gulp`及其插件,对于优化前端开发流程,提升工作效率具有重要意义。

    worker.v2i.yolov5pytorch.zip

    7. **Training Pipeline**:数据集的划分和训练过程可能使用了PyTorch的DataLoader,以并行加载和预处理数据,加速训练速度。 8. **Inference**:在推理阶段,经过训练的模型可以快速地在未知图像上进行目标检测,...

    HLSL基础教程 DirectX

    1. 将输入位置通过变换矩阵进行变换,得到输出位置。 2. 将全局颜色赋值给输出颜色。 通过以上步骤,我们完成了HLSL着色器的基本编写流程。需要注意的是,实际编写过程中还需要考虑更多的细节,如错误处理、性能...

    参考资料-管道基础.zip

    8. **函数式编程**:在函数式编程语言中,函数组合(Function Composition)类似于管道,一个函数的输出可以作为另一个函数的输入,形成链式调用。 9. **微服务架构**:在微服务架构中,API网关或服务网格可以实现...

    js代码-考试 柯里化

    - **部分应用**:柯里化的核心在于部分应用(Partial Application),即将一个函数的某些参数提前传入,得到一个新的函数,新函数等待剩余参数的传入。 - **函数工厂**:柯里化函数可以看作是创建其他函数的工厂,...

    脚本:Shell脚本

    1. **管道(Pipeline)**:使用`|`可以连接多个命令,将前一个命令的输出作为后一个命令的输入。 2. **数组**:在Bash中,可以使用数组变量来存储一组值,如`my_array=("apple" "banana" "orange")`。 3. **函数**...

    cesium模型转换以及加载

    为了减小文件大小并提高加载速度,可以使用 `gltf-pipeline` 对 `.gltf` 文件进行压缩处理: ```bash gltf-pipeline -i model.gltf -o modelDraco.gltf -d ``` 这里 `-d` 参数表示启用 Draco 压缩。 **2.1.3 使用...

    pymongo中group by的操作方法教程

    `pipeline`中的各个阶段是按照顺序执行的,这意味着前一个阶段的输出作为后一个阶段的输入。这使得我们可以构建出复杂的查询流程。 ##### 示例:单键分组 假设我们需要统计某个时间区间内每个用户的订单数量。首先...

Global site tag (gtag.js) - Google Analytics