fetch()

Read(323) Label: fetch,

Here’s how to use fetch() function.

cs .fetch()

Description:

Fetch one or more records from a cursor/distributed cursor.

Syntax:

cs.fetch(n;x)

Note:

Using a cursor, the function fetches n records, or fetches records continuously till the value of expression x (Here x doesn’t return a boolean value) changes or becomes true (Here x returns a boolean value), and then returns them as a sequence/record sequence/table sequence or returns null if the cursor has already been fetched out. It is often used to retrieve a large amount of data in batches. When both n and x are omitted, the function returns all records in a cursor and then closes the cursor. Only one of the parameters n and x is valid.

Parameters:

cs

A cursor/distributed cursor

n

An integer

x

Grouping expression, according to which cs is sorted. With x, n will be ignored

Options:

@0

Won’t actually fetched out the selected data from the cursor. The option enables an action functionally equivalent to copying the data; it doesn’t support parameter x.

@x

Close the cursor after data is fetched.

@o

Return a table sequence

Return value:

   A sequence/record sequence/table sequence

Example:

 

A

 

1

=demo.cursor("select * from EMPLOYEE order by SALARY desc")

Return a cursor of retrieved data sorted by SALARY.

2

=A1.fetch@0(3)

Select top 3 highest-paid employees.

3

=A1.derive(interval@y(BIRTHDAY,HIREDATE):EntryAge, age(HIREDATE):WorkAge)

IMG_256

With @0 option used in A2, A3 fetches data from a complete cursor

4

=A1.fetch()

 

5

=demo.cursor("select * from EMPLOYEE order by SALARY desc")

 

6

=A1.fetch(;SALARY)

Stop data retrieval when SALARY change to select a group of highest- and equally-paid employees..

7

=A1.fetch()

Return the remaining records in the cursor.

8

=demo.cursor("select * from EMPLOYEE order by SALARY desc")

 

9

=A5.fetch@x(3)

Close the cursor after data is fetched.

10

=demo.cursor("select * from EMPLOYEE order by SALARY desc")

 

11

=A7.fetch(3;SALARY==13000)

Stop data retrieval when the value of the expression is true

 

 

A

 

1

[192.168.0.110:8281,192.168.18.143:8281]

Nodes

2

=file@0("emp.ctx", A1)

emp.ctx is a distributed file across the nodes; node 192.168.0.110 holds a part where EID is from 1 to 250 and node 192.168.18.143 holds another part where EID is from 251 to 500

3

=A2.create()

Open a distributed composite table

4

=A3.cursor()

Return a distributed cursor

5

=A4.fetch()

Fetch records form the distributed cursor

Related functions:

db.cursor()

cs.skip()

ch .fetch()

Description:

Fetch and store the existing data in a channel.

Syntax:

ch.fetch()

Note:

The function fetches and stores the existing data in channel ch. It is a set function.

Parameters:

ch

Channel

Return value:

Channel

Example:

 

A

 

1

=demo.query("select * from SALES")

 

2

=channel()

Create a channel

3

=A2.select(ORDERID>100)

Attach ch.select() operation to the channel

4

=A2.fetch()

Attach ch.fetch() function that gets the final result set to A2’s channel to fetch and store the existing data in the channel

5

=A1.push(A2)

Push data in A1’s sequence into the channel

6

=A2.result()

Get the result of performing the operation from the channel

mcs .fetch()

Description:

Get records from a multicursor.

Syntax:

mcs.fetch()

Note:

The function gets all records from a multicursor, which is similar to record fetching from an ordinary cursor. The order of the resulting records could be different from their orginal order.

Parameters:

mcs

Multicursor

Return value:

Record sequence

Example:

 

A

 

1

=file("D:/txt_files/orders.txt").cursor@m()

Return a multicursor

2

=A1.fetch()

Fetch records from a multicursor

3

=file("D://tb1.txt").import()

tb1.txt contains 10w rows

4

=A3.cursor()

Generate an ordinary cursor

5

=A4.fetch(15000)

Fetch the first 15000 rows

6

=A3.cursor@m(10)

Generate a multicursor

7

=A6.fetch(15000)

Fetch the first 15000 rows

Here the fetched records are different from those in A5. With a multicursor, data is fetched from each cursor and then unioned; while with an ordinary cursor, data is sequentially fetched