/usr/share/doc/pycassa-doc/html/tutorial.html is in pycassa-doc 1.11.0-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 | <!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/html; charset=utf-8" />
<title>Tutorial — pycassa 1.11.0 documentation</title>
<link rel="stylesheet" href="_static/default.css" type="text/css" />
<link rel="stylesheet" href="_static/pygments.css" type="text/css" />
<script type="text/javascript">
var DOCUMENTATION_OPTIONS = {
URL_ROOT: './',
VERSION: '1.11.0',
COLLAPSE_INDEX: false,
FILE_SUFFIX: '.html',
HAS_SOURCE: true
};
</script>
<script type="text/javascript" src="_static/jquery.js"></script>
<script type="text/javascript" src="_static/underscore.js"></script>
<script type="text/javascript" src="_static/doctools.js"></script>
<link rel="shortcut icon" href="_static/favicon.ico"/>
<link rel="top" title="pycassa 1.11.0 documentation" href="index.html" />
<link rel="next" title="Twissandra Example" href="example/index.html" />
<link rel="prev" title="Installing" href="installation.html" />
</head>
<body>
<div class="related">
<h3>Navigation</h3>
<ul>
<li class="right" style="margin-right: 10px">
<a href="genindex.html" title="General Index"
accesskey="I">index</a></li>
<li class="right" >
<a href="py-modindex.html" title="Python Module Index"
>modules</a> |</li>
<li class="right" >
<a href="example/index.html" title="Twissandra Example"
accesskey="N">next</a> |</li>
<li class="right" >
<a href="installation.html" title="Installing"
accesskey="P">previous</a> |</li>
<li><a href="index.html">pycassa 1.11.0 documentation</a> »</li>
</ul>
</div>
<div class="document">
<div class="documentwrapper">
<div class="bodywrapper">
<div class="body">
<div class="section" id="tutorial">
<h1>Tutorial<a class="headerlink" href="#tutorial" title="Permalink to this headline">¶</a></h1>
<p>This tutorial is intended as an introduction to working with
Cassandra and <strong>pycassa</strong>.</p>
<div class="toctree-wrapper compound">
<ul class="simple">
</ul>
</div>
<div class="section" id="prerequisites">
<h2>Prerequisites<a class="headerlink" href="#prerequisites" title="Permalink to this headline">¶</a></h2>
<p>Before we start, make sure that you have <strong>pycassa</strong>
<a class="reference internal" href="installation.html"><em>installed</em></a>. In the Python shell, the following
should run without raising an exception:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="kn">import</span> <span class="nn">pycassa</span>
</pre></div>
</div>
<p>This tutorial also assumes that a Cassandra instance is running on the
default host and port. Read the <a class="reference external" href="http://www.datastax.com/docs/0.7/getting_started/index">instructions for getting started
with Cassandra</a> if
you need help with this.</p>
<p>You can start Cassandra like so:</p>
<div class="highlight-bash"><div class="highlight"><pre><span class="nv">$ </span><span class="nb">pwd</span>
~/cassandra
<span class="nv">$ </span>bin/cassandra -f
</pre></div>
</div>
</div>
<div class="section" id="creating-a-keyspace-and-column-families">
<h2>Creating a Keyspace and Column Families<a class="headerlink" href="#creating-a-keyspace-and-column-families" title="Permalink to this headline">¶</a></h2>
<p>We need to create a keyspace and some column families to work with. There
are two good ways to do this: using cassandra-cli, or using pycassaShell. Both
are documented below.</p>
<div class="section" id="using-cassandra-cli">
<h3>Using cassandra-cli<a class="headerlink" href="#using-cassandra-cli" title="Permalink to this headline">¶</a></h3>
<p>The cassandra-cli utility is included with Cassandra. It allows you to create
and modify the schema, explore or modify data, and examine a few things about
your cluster. Here’s how to create the keyspace and column family we need
for this tutorial:</p>
<div class="highlight-none"><div class="highlight"><pre>user@~ $ cassandra-cli
Welcome to cassandra CLI.
Type 'help;' or '?' for help. Type 'quit;' or 'exit;' to quit.
[default@unknown] connect localhost/9160;
Connected to: "Test Cluster" on localhost/9160
[default@unknown] create keyspace Keyspace1;
4f9e42c4-645e-11e0-ad9e-e700f669bcfc
Waiting for schema agreement...
... schemas agree across the cluster
[default@unknown] use Keyspace1;
Authenticated to keyspace: Keyspace1
[default@Keyspace1] create column family ColumnFamily1;
632cf985-645e-11e0-ad9e-e700f669bcfc
Waiting for schema agreement...
... schemas agree across the cluster
[default@Keyspace1] quit;
user@~ $
</pre></div>
</div>
<p>This connects to a local instance of Cassandra and creates a keyspace
named ‘Keyspace1’ with a column family named ‘ColumnFamily1’.</p>
<p>You can find further <a class="reference external" href="http://www.datastax.com/docs/1.1/dml/using_cli">documentation for the CLI online</a>.</p>
</div>
<div class="section" id="using-pycassashell">
<h3>Using pycassaShell<a class="headerlink" href="#using-pycassashell" title="Permalink to this headline">¶</a></h3>
<p><a class="reference internal" href="assorted/pycassa_shell.html#pycassa-shell"><em>pycassaShell</em></a> is an interactive Python shell that is included
with <strong>pycassa</strong>. Upon starting, it sets up many of the objects that
you typically work with when using <strong>pycassa</strong>. It provides most of the
functionality that cassandra-cli does, but also gives you a full Python
environment to work with.</p>
<p>Here’s how to create the keyspace and column family:</p>
<div class="highlight-none"><div class="highlight"><pre>user@~ $ pycassaShell
----------------------------------
Cassandra Interactive Python Shell
----------------------------------
Keyspace: None
Host: localhost:9160
ColumnFamily instances are only available if a keyspace is specified with -k/--keyspace
Schema definition tools and cluster information are available through SYSTEM_MANAGER.
</pre></div>
</div>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="n">SYSTEM_MANAGER</span><span class="o">.</span><span class="n">create_keyspace</span><span class="p">(</span><span class="s">'Keyspace1'</span><span class="p">,</span> <span class="n">strategy_options</span><span class="o">=</span><span class="p">{</span><span class="s">"replication_factor"</span><span class="p">:</span> <span class="s">"1"</span><span class="p">})</span>
<span class="gp">>>> </span><span class="n">SYSTEM_MANAGER</span><span class="o">.</span><span class="n">create_column_family</span><span class="p">(</span><span class="s">'Keyspace1'</span><span class="p">,</span> <span class="s">'ColumnFamily1'</span><span class="p">)</span>
</pre></div>
</div>
</div>
</div>
<div class="section" id="connecting-to-cassandra">
<h2>Connecting to Cassandra<a class="headerlink" href="#connecting-to-cassandra" title="Permalink to this headline">¶</a></h2>
<p>The first step when working with <strong>pycassa</strong> is to connect to the
running cassandra instance:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="kn">from</span> <span class="nn">pycassa.pool</span> <span class="kn">import</span> <span class="n">ConnectionPool</span>
<span class="gp">>>> </span><span class="n">pool</span> <span class="o">=</span> <span class="n">ConnectionPool</span><span class="p">(</span><span class="s">'Keyspace1'</span><span class="p">)</span>
</pre></div>
</div>
<p>The above code will connect by default to <tt class="docutils literal"><span class="pre">localhost:9160</span></tt>. We can
also specify the host (or hosts) and port explicitly as follows:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="n">pool</span> <span class="o">=</span> <span class="n">ConnectionPool</span><span class="p">(</span><span class="s">'Keyspace1'</span><span class="p">,</span> <span class="p">[</span><span class="s">'localhost:9160'</span><span class="p">])</span>
</pre></div>
</div>
<p>This creates a small connection pool for use with a
<a class="reference internal" href="api/pycassa/columnfamily.html#pycassa.columnfamily.ColumnFamily" title="pycassa.columnfamily.ColumnFamily"><tt class="xref py py-class docutils literal"><span class="pre">ColumnFamily</span></tt></a> . See <a class="reference internal" href="#connection-pooling">Connection Pooling</a>
for more details.</p>
</div>
<div class="section" id="getting-a-columnfamily">
<h2>Getting a ColumnFamily<a class="headerlink" href="#getting-a-columnfamily" title="Permalink to this headline">¶</a></h2>
<p>A column family is a collection of rows and columns in Cassandra,
and can be thought of as roughly the equivalent of a table in a
relational database. We’ll use one of the column families that
are included in the default schema file:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="kn">from</span> <span class="nn">pycassa.pool</span> <span class="kn">import</span> <span class="n">ConnectionPool</span>
<span class="gp">>>> </span><span class="kn">from</span> <span class="nn">pycassa.columnfamily</span> <span class="kn">import</span> <span class="n">ColumnFamily</span>
<span class="go">>>></span>
<span class="gp">>>> </span><span class="n">pool</span> <span class="o">=</span> <span class="n">ConnectionPool</span><span class="p">(</span><span class="s">'Keyspace1'</span><span class="p">)</span>
<span class="gp">>>> </span><span class="n">col_fam</span> <span class="o">=</span> <span class="n">ColumnFamily</span><span class="p">(</span><span class="n">pool</span><span class="p">,</span> <span class="s">'ColumnFamily1'</span><span class="p">)</span>
</pre></div>
</div>
<p>If you get an error about the keyspace or column family not
existing, make sure you created the keyspace and column family as
shown above.</p>
</div>
<div class="section" id="inserting-data">
<h2>Inserting Data<a class="headerlink" href="#inserting-data" title="Permalink to this headline">¶</a></h2>
<p>To insert a row into a column family we can use the
<a class="reference internal" href="api/pycassa/columnfamily.html#pycassa.columnfamily.ColumnFamily.insert" title="pycassa.columnfamily.ColumnFamily.insert"><tt class="xref py py-meth docutils literal"><span class="pre">insert()</span></tt></a> method:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="n">col_fam</span><span class="o">.</span><span class="n">insert</span><span class="p">(</span><span class="s">'row_key'</span><span class="p">,</span> <span class="p">{</span><span class="s">'col_name'</span><span class="p">:</span> <span class="s">'col_val'</span><span class="p">})</span>
<span class="go">1354459123410932</span>
</pre></div>
</div>
<p>We can also insert more than one column at a time:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="n">col_fam</span><span class="o">.</span><span class="n">insert</span><span class="p">(</span><span class="s">'row_key'</span><span class="p">,</span> <span class="p">{</span><span class="s">'col_name'</span><span class="p">:</span><span class="s">'col_val'</span><span class="p">,</span> <span class="s">'col_name2'</span><span class="p">:</span><span class="s">'col_val2'</span><span class="p">})</span>
<span class="go">1354459123410932</span>
</pre></div>
</div>
<p>And we can insert more than one row at a time:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="n">col_fam</span><span class="o">.</span><span class="n">batch_insert</span><span class="p">({</span><span class="s">'row1'</span><span class="p">:</span> <span class="p">{</span><span class="s">'name1'</span><span class="p">:</span> <span class="s">'val1'</span><span class="p">,</span> <span class="s">'name2'</span><span class="p">:</span> <span class="s">'val2'</span><span class="p">},</span>
<span class="gp">... </span> <span class="s">'row2'</span><span class="p">:</span> <span class="p">{</span><span class="s">'foo'</span><span class="p">:</span> <span class="s">'bar'</span><span class="p">}})</span>
<span class="go">1354491238721387</span>
</pre></div>
</div>
</div>
<div class="section" id="getting-data">
<h2>Getting Data<a class="headerlink" href="#getting-data" title="Permalink to this headline">¶</a></h2>
<p>There are many more ways to get data out of Cassandra than there are
to insert data.</p>
<p>The simplest way to get data is to use
<a class="reference internal" href="api/pycassa/columnfamily.html#pycassa.columnfamily.ColumnFamily.get" title="pycassa.columnfamily.ColumnFamily.get"><tt class="xref py py-meth docutils literal"><span class="pre">get()</span></tt></a>:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="n">col_fam</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s">'row_key'</span><span class="p">)</span>
<span class="go">{'col_name': 'col_val', 'col_name2': 'col_val2'}</span>
</pre></div>
</div>
<p>Without any other arguments, <a class="reference internal" href="api/pycassa/columnfamily.html#pycassa.columnfamily.ColumnFamily.get" title="pycassa.columnfamily.ColumnFamily.get"><tt class="xref py py-meth docutils literal"><span class="pre">get()</span></tt></a>
returns every column in the row (up to <cite>column_count</cite>, which defaults to 100).
If you only want a few of the columns and you know them by name, you can
specify them using a <cite>columns</cite> argument:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="n">col_fam</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s">'row_key'</span><span class="p">,</span> <span class="n">columns</span><span class="o">=</span><span class="p">[</span><span class="s">'col_name'</span><span class="p">,</span> <span class="s">'col_name2'</span><span class="p">])</span>
<span class="go">{'col_name': 'col_val', 'col_name2': 'col_val2'}</span>
</pre></div>
</div>
<p>We may also get a slice (or subrange) of the columns in a row. To do this,
use the <cite>column_start</cite> and <cite>column_finish</cite> parameters. One or both of these may
be left empty to allow the slice to extend to one or both ends.
Note that <cite>column_finish</cite> is inclusive.</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="mi">10</span><span class="p">):</span>
<span class="gp">... </span> <span class="n">col_fam</span><span class="o">.</span><span class="n">insert</span><span class="p">(</span><span class="s">'row_key'</span><span class="p">,</span> <span class="p">{</span><span class="nb">str</span><span class="p">(</span><span class="n">i</span><span class="p">):</span> <span class="s">'val'</span><span class="p">})</span>
<span class="gp">...</span>
<span class="go">1302542571215334</span>
<span class="go">1302542571218485</span>
<span class="go">1302542571220599</span>
<span class="go">1302542571221991</span>
<span class="go">1302542571223388</span>
<span class="go">1302542571224629</span>
<span class="go">1302542571225859</span>
<span class="go">1302542571227029</span>
<span class="go">1302542571228472</span>
<span class="gp">>>> </span><span class="n">col_fam</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s">'row_key'</span><span class="p">,</span> <span class="n">column_start</span><span class="o">=</span><span class="s">'5'</span><span class="p">,</span> <span class="n">column_finish</span><span class="o">=</span><span class="s">'7'</span><span class="p">)</span>
<span class="go">{'5': 'val', '6': 'val', '7': 'val'}</span>
</pre></div>
</div>
<p>Sometimes you want to get columns in reverse sorted order. A common
example of this is getting the last N columns from a row that
represents a timeline. To do this, set <cite>column_reversed</cite> to <tt class="docutils literal"><span class="pre">True</span></tt>.
If you think of the columns as being sorted from left to right, when
<cite>column_reversed</cite> is <tt class="docutils literal"><span class="pre">True</span></tt>, <cite>column_start</cite> will determine the right
end of the range while <cite>column_finish</cite> will determine the left.</p>
<p>Here’s an example of getting the last three columns in a row:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="n">col_fam</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s">'row_key'</span><span class="p">,</span> <span class="n">column_reversed</span><span class="o">=</span><span class="bp">True</span><span class="p">,</span> <span class="n">column_count</span><span class="o">=</span><span class="mi">3</span><span class="p">)</span>
<span class="go">{'9': 'val', '8': 'val', '7': 'val'}</span>
</pre></div>
</div>
<p>There are a few ways to get multiple rows at the same time.
The first is to specify them by name using
<a class="reference internal" href="api/pycassa/columnfamily.html#pycassa.columnfamily.ColumnFamily.multiget" title="pycassa.columnfamily.ColumnFamily.multiget"><tt class="xref py py-meth docutils literal"><span class="pre">multiget()</span></tt></a>:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="n">col_fam</span><span class="o">.</span><span class="n">multiget</span><span class="p">([</span><span class="s">'row1'</span><span class="p">,</span> <span class="s">'row2'</span><span class="p">])</span>
<span class="go">{'row1': {'name1': 'val1', 'name2': 'val2'}, 'row_key2': {'foo': 'bar'}}</span>
</pre></div>
</div>
<p>Another way is to get a range of keys at once by using
<a class="reference internal" href="api/pycassa/columnfamily.html#pycassa.columnfamily.ColumnFamily.get_range" title="pycassa.columnfamily.ColumnFamily.get_range"><tt class="xref py py-meth docutils literal"><span class="pre">get_range()</span></tt></a>. The parameter
<cite>finish</cite> is also inclusive here, too. Assuming we’ve inserted some rows
with keys ‘row_key1’ through ‘row_key9’, we can do this:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="n">result</span> <span class="o">=</span> <span class="n">col_fam</span><span class="o">.</span><span class="n">get_range</span><span class="p">(</span><span class="n">start</span><span class="o">=</span><span class="s">'row_key5'</span><span class="p">,</span> <span class="n">finish</span><span class="o">=</span><span class="s">'row_key7'</span><span class="p">)</span>
<span class="gp">>>> </span><span class="k">for</span> <span class="n">key</span><span class="p">,</span> <span class="n">columns</span> <span class="ow">in</span> <span class="n">result</span><span class="p">:</span>
<span class="gp">... </span> <span class="k">print</span> <span class="n">key</span><span class="p">,</span> <span class="s">'=>'</span><span class="p">,</span> <span class="n">columns</span>
<span class="gp">...</span>
<span class="go">'row_key5' => {'name':'val'}</span>
<span class="go">'row_key6' => {'name':'val'}</span>
<span class="go">'row_key7' => {'name':'val'}</span>
</pre></div>
</div>
<div class="admonition note">
<p class="first admonition-title">Note</p>
<p class="last">Cassandra must be using an OrderPreservingPartitioner for you to be
able to get a meaningful range of rows; the default, RandomPartitioner,
stores rows in the order of the MD5 hash of their keys. See
<a class="reference external" href="http://www.datastax.com/docs/1.1/cluster_architecture/partitioning">http://www.datastax.com/docs/1.1/cluster_architecture/partitioning</a>.</p>
</div>
<p>The last way to get multiple rows at a time is to take advantage of
secondary indexes by using <a class="reference internal" href="api/pycassa/columnfamily.html#pycassa.columnfamily.ColumnFamily.get_indexed_slices" title="pycassa.columnfamily.ColumnFamily.get_indexed_slices"><tt class="xref py py-meth docutils literal"><span class="pre">get_indexed_slices()</span></tt></a>,
which is described in the <a class="reference internal" href="assorted/secondary_indexes.html#secondary-indexes"><em>Secondary Indexes</em></a> section.</p>
<p>It’s also possible to specify a set of columns or a slice for
<a class="reference internal" href="api/pycassa/columnfamily.html#pycassa.columnfamily.ColumnFamily.multiget" title="pycassa.columnfamily.ColumnFamily.multiget"><tt class="xref py py-meth docutils literal"><span class="pre">multiget()</span></tt></a> and
<a class="reference internal" href="api/pycassa/columnfamily.html#pycassa.columnfamily.ColumnFamily.get_range" title="pycassa.columnfamily.ColumnFamily.get_range"><tt class="xref py py-meth docutils literal"><span class="pre">get_range()</span></tt></a> just like we did for
<a class="reference internal" href="api/pycassa/columnfamily.html#pycassa.columnfamily.ColumnFamily.get" title="pycassa.columnfamily.ColumnFamily.get"><tt class="xref py py-meth docutils literal"><span class="pre">get()</span></tt></a>.</p>
</div>
<div class="section" id="counting">
<h2>Counting<a class="headerlink" href="#counting" title="Permalink to this headline">¶</a></h2>
<p>If you just want to know how many columns are in a row, you can use
<a class="reference internal" href="api/pycassa/columnfamily.html#pycassa.columnfamily.ColumnFamily.get_count" title="pycassa.columnfamily.ColumnFamily.get_count"><tt class="xref py py-meth docutils literal"><span class="pre">get_count()</span></tt></a>:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="n">col_fam</span><span class="o">.</span><span class="n">get_count</span><span class="p">(</span><span class="s">'row_key'</span><span class="p">)</span>
<span class="go">3</span>
</pre></div>
</div>
<p>If you only want to get a count of the number of columns that are inside
of a slice or have particular names, you can do that as well:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="n">col_fam</span><span class="o">.</span><span class="n">get_count</span><span class="p">(</span><span class="s">'row_key'</span><span class="p">,</span> <span class="n">columns</span><span class="o">=</span><span class="p">[</span><span class="s">'foo'</span><span class="p">,</span> <span class="s">'bar'</span><span class="p">])</span>
<span class="go">2</span>
<span class="gp">>>> </span><span class="n">col_fam</span><span class="o">.</span><span class="n">get_count</span><span class="p">(</span><span class="s">'row_key'</span><span class="p">,</span> <span class="n">column_start</span><span class="o">=</span><span class="s">'foo'</span><span class="p">)</span>
<span class="go">3</span>
</pre></div>
</div>
<p>You can also do this in parallel for multiple rows using
<a class="reference internal" href="api/pycassa/columnfamily.html#pycassa.columnfamily.ColumnFamily.multiget_count" title="pycassa.columnfamily.ColumnFamily.multiget_count"><tt class="xref py py-meth docutils literal"><span class="pre">multiget_count()</span></tt></a>:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="n">col_fam</span><span class="o">.</span><span class="n">multiget_count</span><span class="p">([</span><span class="s">'fib0'</span><span class="p">,</span> <span class="s">'fib1'</span><span class="p">,</span> <span class="s">'fib2'</span><span class="p">,</span> <span class="s">'fib3'</span><span class="p">,</span> <span class="s">'fib4'</span><span class="p">])</span>
<span class="go">{'fib0': 1, 'fib1': 1, 'fib2': 2, 'fib3': 3, 'fib4': 5'}</span>
</pre></div>
</div>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="n">col_fam</span><span class="o">.</span><span class="n">multiget_count</span><span class="p">([</span><span class="s">'fib0'</span><span class="p">,</span> <span class="s">'fib1'</span><span class="p">,</span> <span class="s">'fib2'</span><span class="p">,</span> <span class="s">'fib3'</span><span class="p">,</span> <span class="s">'fib4'</span><span class="p">],</span>
<span class="gp">... </span> <span class="n">columns</span><span class="o">=</span><span class="p">[</span><span class="s">'col1'</span><span class="p">,</span> <span class="s">'col2'</span><span class="p">,</span> <span class="s">'col3'</span><span class="p">])</span>
<span class="go">{'fib0': 1, 'fib1': 1, 'fib2': 2, 'fib3': 3, 'fib4': 3'}</span>
</pre></div>
</div>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="n">col_fam</span><span class="o">.</span><span class="n">multiget_count</span><span class="p">([</span><span class="s">'fib0'</span><span class="p">,</span> <span class="s">'fib1'</span><span class="p">,</span> <span class="s">'fib2'</span><span class="p">,</span> <span class="s">'fib3'</span><span class="p">,</span> <span class="s">'fib4'</span><span class="p">],</span>
<span class="gp">... </span> <span class="n">column_start</span><span class="o">=</span><span class="s">'col1'</span><span class="p">,</span> <span class="n">column_finish</span><span class="o">=</span><span class="s">'col3'</span><span class="p">)</span>
<span class="go">{'fib0': 1, 'fib1': 1, 'fib2': 2, 'fib3': 3, 'fib4': 3'}</span>
</pre></div>
</div>
</div>
<div class="section" id="typed-column-names-and-values">
<h2>Typed Column Names and Values<a class="headerlink" href="#typed-column-names-and-values" title="Permalink to this headline">¶</a></h2>
<p>Within a column family, column names have a specified <cite>comparator type</cite>
which controls how they are sorted. Column values and row keys may also
have a <cite>validation class</cite>, which validates that inserted values are
the correct type.</p>
<p>The different types available include ASCII strings, integers, dates,
UTF8, raw bytes, UUIDs, and more. See <a class="reference internal" href="api/pycassa/types.html#module-pycassa.types" title="pycassa.types"><tt class="xref py py-mod docutils literal"><span class="pre">pycassa.types</span></tt></a> for a full
list.</p>
<p>Cassandra requires you to pack column names and values into a format it can
understand by using something like <tt class="xref py py-meth docutils literal"><span class="pre">struct.pack()</span></tt>. Fortunately,
when <strong>pycassa</strong> sees that a column family has a particular comparator type
or validation class, it knows to pack and unpack these data types automatically
for you. So, if we want to write to the StandardInt column family, which has
an IntegerType comparator, we can do the following:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="n">col_fam</span> <span class="o">=</span> <span class="n">pycassa</span><span class="o">.</span><span class="n">ColumnFamily</span><span class="p">(</span><span class="n">pool</span><span class="p">,</span> <span class="s">'StandardInt'</span><span class="p">)</span>
<span class="gp">>>> </span><span class="n">col_fam</span><span class="o">.</span><span class="n">insert</span><span class="p">(</span><span class="s">'row_key'</span><span class="p">,</span> <span class="p">{</span><span class="mi">42</span><span class="p">:</span> <span class="s">'some_val'</span><span class="p">})</span>
<span class="go">1354491238721387</span>
<span class="gp">>>> </span><span class="n">col_fam</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s">'row_key'</span><span class="p">)</span>
<span class="go">{42: 'some_val'}</span>
</pre></div>
</div>
<p>Notice that 42 is an integer here, not a string.</p>
<p>As mentioned above, Cassandra also offers validators on column values and keys
with the same set of types. Column value validators can be set for an entire
column family, for individual columns, or both. <strong>pycassa</strong> knows to pack these
column values automatically too. Suppose we have a <cite>Users</cite> column family with
two columns, <tt class="docutils literal"><span class="pre">name</span></tt> and <tt class="docutils literal"><span class="pre">age</span></tt>, with types UTF8Type and IntegerType:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="n">col_fam</span> <span class="o">=</span> <span class="n">pycassa</span><span class="o">.</span><span class="n">ColumnFamily</span><span class="p">(</span><span class="n">pool</span><span class="p">,</span> <span class="s">'Users'</span><span class="p">)</span>
<span class="gp">>>> </span><span class="n">col_fam</span><span class="o">.</span><span class="n">insert</span><span class="p">(</span><span class="s">'thobbs'</span><span class="p">,</span> <span class="p">{</span><span class="s">'name'</span><span class="p">:</span> <span class="s">'Tyler'</span><span class="p">,</span> <span class="s">'age'</span><span class="p">:</span> <span class="mi">24</span><span class="p">})</span>
<span class="go">1354491238782746</span>
<span class="gp">>>> </span><span class="n">col_fam</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s">'thobbs'</span><span class="p">)</span>
<span class="go">{'name': 'Tyler', 'age': 24}</span>
</pre></div>
</div>
<p>Of course, if <strong>pycassa</strong>‘s automatic behavior isn’t working for you, you
can turn it off or change it using <a class="reference internal" href="api/pycassa/columnfamily.html#pycassa.columnfamily.ColumnFamily.autopack_names" title="pycassa.columnfamily.ColumnFamily.autopack_names"><tt class="xref py py-attr docutils literal"><span class="pre">autopack_names</span></tt></a>,
<a class="reference internal" href="api/pycassa/columnfamily.html#pycassa.columnfamily.ColumnFamily.autopack_values" title="pycassa.columnfamily.ColumnFamily.autopack_values"><tt class="xref py py-attr docutils literal"><span class="pre">autopack_values</span></tt></a>, <a class="reference internal" href="api/pycassa/columnfamily.html#pycassa.columnfamily.ColumnFamily.column_name_class" title="pycassa.columnfamily.ColumnFamily.column_name_class"><tt class="xref py py-attr docutils literal"><span class="pre">column_name_class</span></tt></a>,
<a class="reference internal" href="api/pycassa/columnfamily.html#pycassa.columnfamily.ColumnFamily.default_validation_class" title="pycassa.columnfamily.ColumnFamily.default_validation_class"><tt class="xref py py-attr docutils literal"><span class="pre">default_validation_class</span></tt></a>, and so on.</p>
</div>
<div class="section" id="connection-pooling">
<h2>Connection Pooling<a class="headerlink" href="#connection-pooling" title="Permalink to this headline">¶</a></h2>
<p>Pycassa uses connection pools to maintain connections to Cassandra servers.
The <a class="reference internal" href="api/pycassa/pool.html#pycassa.pool.ConnectionPool" title="pycassa.pool.ConnectionPool"><tt class="xref py py-class docutils literal"><span class="pre">ConnectionPool</span></tt></a> class is used to create the connection
pool. After creating the pool, it may be used to create multiple
<a class="reference internal" href="api/pycassa/columnfamily.html#pycassa.columnfamily.ColumnFamily" title="pycassa.columnfamily.ColumnFamily"><tt class="xref py py-class docutils literal"><span class="pre">ColumnFamily</span></tt></a> objects.</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="n">pool</span> <span class="o">=</span> <span class="n">pycassa</span><span class="o">.</span><span class="n">ConnectionPool</span><span class="p">(</span><span class="s">'Keyspace1'</span><span class="p">,</span> <span class="n">pool_size</span><span class="o">=</span><span class="mi">20</span><span class="p">)</span>
<span class="gp">>>> </span><span class="n">standard_cf</span> <span class="o">=</span> <span class="n">pycassa</span><span class="o">.</span><span class="n">ColumnFamily</span><span class="p">(</span><span class="n">pool</span><span class="p">,</span> <span class="s">'Standard1'</span><span class="p">)</span>
<span class="gp">>>> </span><span class="n">standard_cf</span><span class="o">.</span><span class="n">insert</span><span class="p">(</span><span class="s">'key'</span><span class="p">,</span> <span class="p">{</span><span class="s">'col'</span><span class="p">:</span> <span class="s">'val'</span><span class="p">})</span>
<span class="go">1354491238782746</span>
<span class="gp">>>> </span><span class="n">super_cf</span> <span class="o">=</span> <span class="n">pycassa</span><span class="o">.</span><span class="n">ColumnFamily</span><span class="p">(</span><span class="n">pool</span><span class="p">,</span> <span class="s">'Super1'</span><span class="p">)</span>
<span class="gp">>>> </span><span class="n">super_cf</span><span class="o">.</span><span class="n">insert</span><span class="p">(</span><span class="s">'key2'</span><span class="p">,</span> <span class="p">{</span><span class="s">'column'</span> <span class="p">:</span> <span class="p">{</span><span class="s">'col'</span><span class="p">:</span> <span class="s">'val'</span><span class="p">}})</span>
<span class="go">1354491239779182</span>
<span class="gp">>>> </span><span class="n">standard_cf</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s">'key'</span><span class="p">)</span>
<span class="go">{'col': 'val'}</span>
<span class="gp">>>> </span><span class="n">pool</span><span class="o">.</span><span class="n">dispose</span><span class="p">()</span>
</pre></div>
</div>
<p>Automatic retries (or “failover”) happen by default with ConectionPools.
This means that if any operation fails, it will be transparently retried
on other servers until it succeeds or a maximum number of failures is reached.</p>
</div>
</div>
</div>
</div>
</div>
<div class="sphinxsidebar">
<div class="sphinxsidebarwrapper">
<h3><a href="index.html">Table Of Contents</a></h3>
<ul>
<li><a class="reference internal" href="#">Tutorial</a><ul>
<li><a class="reference internal" href="#prerequisites">Prerequisites</a></li>
<li><a class="reference internal" href="#creating-a-keyspace-and-column-families">Creating a Keyspace and Column Families</a><ul>
<li><a class="reference internal" href="#using-cassandra-cli">Using cassandra-cli</a></li>
<li><a class="reference internal" href="#using-pycassashell">Using pycassaShell</a></li>
</ul>
</li>
<li><a class="reference internal" href="#connecting-to-cassandra">Connecting to Cassandra</a></li>
<li><a class="reference internal" href="#getting-a-columnfamily">Getting a ColumnFamily</a></li>
<li><a class="reference internal" href="#inserting-data">Inserting Data</a></li>
<li><a class="reference internal" href="#getting-data">Getting Data</a></li>
<li><a class="reference internal" href="#counting">Counting</a></li>
<li><a class="reference internal" href="#typed-column-names-and-values">Typed Column Names and Values</a></li>
<li><a class="reference internal" href="#connection-pooling">Connection Pooling</a></li>
</ul>
</li>
</ul>
<h4>Previous topic</h4>
<p class="topless"><a href="installation.html"
title="previous chapter">Installing</a></p>
<h4>Next topic</h4>
<p class="topless"><a href="example/index.html"
title="next chapter">Twissandra Example</a></p>
<h3>This Page</h3>
<ul class="this-page-menu">
<li><a href="_sources/tutorial.txt"
rel="nofollow">Show Source</a></li>
</ul>
<div id="searchbox" style="display: none">
<h3>Quick search</h3>
<form class="search" action="search.html" method="get">
<input type="text" name="q" />
<input type="submit" value="Go" />
<input type="hidden" name="check_keywords" value="yes" />
<input type="hidden" name="area" value="default" />
</form>
<p class="searchtip" style="font-size: 90%">
Enter search terms or a module, class or function name.
</p>
</div>
<script type="text/javascript">$('#searchbox').show(0);</script>
</div>
</div>
<div class="clearer"></div>
</div>
<div class="related">
<h3>Navigation</h3>
<ul>
<li class="right" style="margin-right: 10px">
<a href="genindex.html" title="General Index"
>index</a></li>
<li class="right" >
<a href="py-modindex.html" title="Python Module Index"
>modules</a> |</li>
<li class="right" >
<a href="example/index.html" title="Twissandra Example"
>next</a> |</li>
<li class="right" >
<a href="installation.html" title="Installing"
>previous</a> |</li>
<li><a href="index.html">pycassa 1.11.0 documentation</a> »</li>
</ul>
</div>
<div class="footer">
Created using <a href="http://sphinx-doc.org/">Sphinx</a> 1.2.2.
</div>
</body>
</html>
|