1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
|
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=9"/>
<title>WiredTiger: Join cursors in Java</title>
<link href="tabs.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript" src="dynsections.js"></script>
<link href="navtree.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="resize.js"></script>
<script type="text/javascript" src="navtreedata.js"></script>
<script type="text/javascript" src="navtree.js"></script>
<script type="text/javascript">
$(document).ready(initResizable);
</script>
<link href="doxygen.css" rel="stylesheet" type="text/css" />
<link href="wiredtiger.css" rel="stylesheet" type="text/css"/>
</head>
<body>
<div id="top"><!-- do not remove this div, it is closed by doxygen! -->
<div id="titlearea">
<table cellspacing="0" cellpadding="0">
<tbody>
<tr style="height: 56px;">
<td id="projectlogo"><a href="http://wiredtiger.com/"><img alt="Logo" src="LogoFinal-header.png" alt="WiredTiger" /></a></td>
<td style="padding-left: 0.5em;">
<div id="projectname">
 <span id="projectnumber">Version 3.2.1</span>
</div>
<div id="projectbrief"><!-- 3.2.1 --></div>
</td>
</tr>
</tbody>
</table>
</div>
<div class="banner">
<a href="https://github.com/wiredtiger/wiredtiger">Fork me on GitHub</a>
<a class="last" href="http://groups.google.com/group/wiredtiger-users">Join my user group</a>
</div>
<!-- end header part -->
<!-- Generated by Doxygen 1.8.13 -->
<script type="text/javascript" src="menudata.js"></script>
<script type="text/javascript" src="menu.js"></script>
<script type="text/javascript">
$(function() {
initMenu('',false,false,'search.php','Search');
});
</script>
<div id="main-nav"></div>
</div><!-- top -->
<div id="side-nav" class="ui-resizable side-nav-resizable">
<div id="nav-tree">
<div id="nav-tree-contents">
<div id="nav-sync" class="sync"></div>
</div>
</div>
<div id="splitbar" style="-moz-user-select:none;"
class="ui-resizable-handle">
</div>
</div>
<script type="text/javascript">
$(document).ready(function(){initNavTree('cursor_join_lang_java.html','');});
</script>
<div id="doc-content">
<div class="header">
<div class="headertitle">
<div class="title">Join cursors in Java </div> </div>
</div><!--header-->
<div class="contents">
<div class="textblock"><p>Join cursors provide a way to iterate over a subset of a table, where the subset is specified by relationships with reference cursors.</p>
<p>A join cursor is created with Session.open_cursor using a <code>"join:table:<name>"</code> URI prefix. Then reference cursors are positioned to keys on indices and joined to the join cursor using Session.join calls. The result is a join cursor that can be iterated to satisfy the join equation.</p>
<p>Here is an example using join cursors:</p>
<div class="fragment"><div class="line"> <span class="comment">/* Open cursors needed by the join. */</span></div><div class="line"> join_cursor = session.<a class="code" href="struct_w_t___s_e_s_s_i_o_n.html#afb5b4a69c2c5cafe411b2b04fdc1c75d">open_cursor</a>(</div><div class="line"> <span class="stringliteral">"join:table:poptable"</span>, null, null);</div><div class="line"> country_cursor = session.<a class="code" href="struct_w_t___s_e_s_s_i_o_n.html#afb5b4a69c2c5cafe411b2b04fdc1c75d">open_cursor</a>(</div><div class="line"> <span class="stringliteral">"index:poptable:country"</span>, null, null);</div><div class="line"> year_cursor = session.<a class="code" href="struct_w_t___s_e_s_s_i_o_n.html#afb5b4a69c2c5cafe411b2b04fdc1c75d">open_cursor</a>(</div><div class="line"> <span class="stringliteral">"index:poptable:immutable_year"</span>, null, null);</div><div class="line"></div><div class="line"> <span class="comment">/* select values WHERE country == "AU" AND year > 1900 */</span></div><div class="line"> country_cursor.putKeyString(<span class="stringliteral">"AU"</span>);</div><div class="line"> ret = country_cursor.search();</div><div class="line"> session.<a class="code" href="struct_w_t___s_e_s_s_i_o_n.html#ae0ab118df83d173c6a20eb1ea3f3fd84">join</a>(join_cursor, country_cursor, <span class="stringliteral">"compare=eq,count=10"</span>);</div><div class="line"> year_cursor.putKeyShort((<span class="keywordtype">short</span>)1900);</div><div class="line"> ret = year_cursor.search();</div><div class="line"> session.<a class="code" href="struct_w_t___s_e_s_s_i_o_n.html#ae0ab118df83d173c6a20eb1ea3f3fd84">join</a>(join_cursor, year_cursor,</div><div class="line"> <span class="stringliteral">"compare=gt,count=10,strategy=bloom"</span>);</div><div class="line"></div><div class="line"> <span class="comment">/* List the values that are joined */</span></div><div class="line"> <span class="keywordflow">while</span> ((ret = join_cursor.next()) == 0) {</div><div class="line"> recno = join_cursor.getKeyRecord();</div><div class="line"> country = join_cursor.getValueString();</div><div class="line"> year = join_cursor.getValueShort();</div><div class="line"> population = join_cursor.getValueLong();</div><div class="line"> System.out.print(<span class="stringliteral">"ID "</span> + recno);</div><div class="line"> System.out.println( <span class="stringliteral">": country "</span> + country + <span class="stringliteral">", year "</span> + year +</div><div class="line"> <span class="stringliteral">", population "</span> + population);</div><div class="line"> }</div></div><!-- fragment --><p> Joins support various comparison operators: <code>"eq"</code>, <code>"gt"</code>, <code>"ge"</code>, <code>"lt"</code>, <code>"le"</code>. Ranges with lower and upper bounds can also be specified, by joining two cursors on the same index, for example, one with <code>"compare=ge"</code> and another <code>"compare=lt"</code>. In addition to joining indices, the main table can be joined so that a range of primary keys can be specified.</p>
<p>By default, a join cursor returns a conjunction, that is, all keys that satisfy all the joined comparisons. By specifying a configuration with <code>"operation=or"</code>, a join cursor will return a disjunction, or all keys that satisfy at least one of the joined comparisons. More complex joins can be composed by specifying another join cursor as the reference cursor in a join call.</p>
<p>Here is an example using these concepts to show a conjunction of a disjunction:</p>
<div class="fragment"><div class="line"> <span class="comment">/* Open cursors needed by the join. */</span></div><div class="line"> join_cursor = session.<a class="code" href="struct_w_t___s_e_s_s_i_o_n.html#afb5b4a69c2c5cafe411b2b04fdc1c75d">open_cursor</a>(</div><div class="line"> <span class="stringliteral">"join:table:poptable"</span>, null, null);</div><div class="line"> subjoin_cursor = session.<a class="code" href="struct_w_t___s_e_s_s_i_o_n.html#afb5b4a69c2c5cafe411b2b04fdc1c75d">open_cursor</a>(</div><div class="line"> <span class="stringliteral">"join:table:poptable"</span>, null, null);</div><div class="line"> country_cursor = session.<a class="code" href="struct_w_t___s_e_s_s_i_o_n.html#afb5b4a69c2c5cafe411b2b04fdc1c75d">open_cursor</a>(</div><div class="line"> <span class="stringliteral">"index:poptable:country"</span>, null, null);</div><div class="line"> country_cursor2 = session.<a class="code" href="struct_w_t___s_e_s_s_i_o_n.html#afb5b4a69c2c5cafe411b2b04fdc1c75d">open_cursor</a>(</div><div class="line"> <span class="stringliteral">"index:poptable:country"</span>, null, null);</div><div class="line"> year_cursor = session.<a class="code" href="struct_w_t___s_e_s_s_i_o_n.html#afb5b4a69c2c5cafe411b2b04fdc1c75d">open_cursor</a>(</div><div class="line"> <span class="stringliteral">"index:poptable:immutable_year"</span>, null, null);</div><div class="line"></div><div class="line"> <span class="comment">/*</span></div><div class="line"><span class="comment"> * select values WHERE (country == "AU" OR country == "UK")</span></div><div class="line"><span class="comment"> * AND year > 1900</span></div><div class="line"><span class="comment"> *</span></div><div class="line"><span class="comment"> * First, set up the join representing the country clause.</span></div><div class="line"><span class="comment"> */</span></div><div class="line"> country_cursor.putKeyString(<span class="stringliteral">"AU"</span>);</div><div class="line"> ret = country_cursor.search();</div><div class="line"> ret = session.<a class="code" href="struct_w_t___s_e_s_s_i_o_n.html#ae0ab118df83d173c6a20eb1ea3f3fd84">join</a>(subjoin_cursor, country_cursor,</div><div class="line"> <span class="stringliteral">"operation=or,compare=eq,count=10"</span>);</div><div class="line"> country_cursor2.putKeyString(<span class="stringliteral">"UK"</span>);</div><div class="line"> ret = country_cursor2.search();</div><div class="line"> ret = session.<a class="code" href="struct_w_t___s_e_s_s_i_o_n.html#ae0ab118df83d173c6a20eb1ea3f3fd84">join</a>(subjoin_cursor, country_cursor2,</div><div class="line"> <span class="stringliteral">"operation=or,compare=eq,count=10"</span>);</div><div class="line"></div><div class="line"> <span class="comment">/* Join that to the top join, and add the year clause */</span></div><div class="line"> ret = session.<a class="code" href="struct_w_t___s_e_s_s_i_o_n.html#ae0ab118df83d173c6a20eb1ea3f3fd84">join</a>(join_cursor, subjoin_cursor, null);</div><div class="line"> year_cursor.putKeyShort((<span class="keywordtype">short</span>)1900);</div><div class="line"> ret = year_cursor.search();</div><div class="line"> ret = session.<a class="code" href="struct_w_t___s_e_s_s_i_o_n.html#ae0ab118df83d173c6a20eb1ea3f3fd84">join</a>(join_cursor, year_cursor,</div><div class="line"> <span class="stringliteral">"compare=gt,count=10,strategy=bloom"</span>);</div><div class="line"></div><div class="line"> <span class="comment">/* List the values that are joined */</span></div><div class="line"> <span class="keywordflow">while</span> ((ret = join_cursor.next()) == 0) {</div><div class="line"> recno = join_cursor.getKeyRecord();</div><div class="line"> country = join_cursor.getValueString();</div><div class="line"> year = join_cursor.getValueShort();</div><div class="line"> population = join_cursor.getValueLong();</div><div class="line"> System.out.print(<span class="stringliteral">"ID "</span> + recno);</div><div class="line"> System.out.println( <span class="stringliteral">": country "</span> + country + <span class="stringliteral">", year "</span> + year +</div><div class="line"> <span class="stringliteral">", population "</span> + population);</div><div class="line"> }</div></div><!-- fragment --><p> All the joins should be done on the join cursor before Cursor.next is called. Calling Cursor.next on a join cursor for the first time populates any bloom filters and performs other initialization. The join cursor's key is the primary key (the key for the main table), and its value is the entire set of values of the main table. A join cursor can be created with a projection by appending <code>"(col1,col2,...)"</code> to the URI if a different set of values is needed.</p>
<p>Keys returned from the join cursor are ordered according to the first reference cursor joined. For example, if an index cursor was joined first, that index determines the order of results. If the join cursor uses disjunctions, then the ordering of all joins determines the order. The first join in a conjunctive join, or all joins in a disjunctive join, are distinctive in that they are iterated internally as the cursor join returns values in order. Any bloom filters specified on the joins that are used for iteration are not useful, and are silently ignored.</p>
<p>When disjunctions are used where the sets of keys overlap on these 'iteration joins', a join cursor will return duplicates. A join cursor never returns duplicates unless <code>"operation=or"</code> is used in a join configuration, or unless the first joined cursor is itself a join cursor that would return duplicates. </p>
</div></div><!-- contents -->
</div><!-- doc-content -->
<!-- start footer part -->
<div id="nav-path" class="navpath"><!-- id is needed for treeview function! -->
<ul>
<li class="navelem"><a class="el" href="index.html">Reference Guide</a></li><li class="navelem"><a class="el" href="programming_lang_java.html">Writing WiredTiger applications in Java</a></li>
<li class="footer">Copyright (c) 2008-2019 MongoDB, Inc. All rights reserved. Contact <a href="mailto:info@wiredtiger.com">info@wiredtiger.com</a> for more information.</li>
</ul>
</div>
</body>
</html>
|