Alim Hasanov
Alim Hasanov

Reputation: 197

kdb/q: Query multiple handles with hopen

I would like to be able to query several handles at once, where the tables have the same format like:

handles: 8000,8001,8003 tables: foo

Want to do something like:

x:hopen `8000`8001`8003
x select from foo col1,col2

So i get rows from each foo table on each handle.

Is there a way to achieve this?

Thank you

Upvotes: 2

Views: 1481

Answers (3)

effbiae
effbiae

Reputation: 1157

Same as other answers, but more complicated using set (neg h) rather than get (h)

The cookbook/load-balancing helps in this example, too.

q)h:hopen each 8000 8001 8002
q)h
476 480 484i
q)r:(0#0i)!()               /dictionary of handles and results

Set the callback for the response from the servers

q).z.ps:{@[`r;.z.w;:;x]}

Send a "set" query to each handle

q)(neg h)@\:({(neg .z.w)value"select col1,col2 from foo"};`)

Wait until all messages have a response

q)h .\:()

Finally, putting the result together

q)raze r h

The only advantage is concurrency.

As @AlexanderBelopolsky pointed out, don't forget

q)hclose each h

Upvotes: 0

Alexander Belopolsky
Alexander Belopolsky

Reputation: 2268

If you are not planning to reuse the handles, you can do

q)raze`::8000`::8001`::8003@\:"select from foo col1,col2"

Upvotes: 1

jomahony
jomahony

Reputation: 1692

Use 'each' to hopen each handle

  q)h:hopen each 8000 8001 8002
  q)h
  476 480 484i

Use apply each-left to send the same query to each server

  q)r:h@\:"select col1,col2 from foo"
  q)r
  +`col1`col2!(1 2;2 3)
  +`col1`col2!(1 2;2 3)
  +`col1`col2!(1 2;2 3)

Then you'll have to raze the result:

 q)show res:raze r
col1 col2
--------- 
 1    2
 2    3
 1    2
 2    3
 1    2
 2    3

Upvotes: 7

Related Questions