title | summary | toc |
---|---|---|
Build a Test App |
Follow this tutorial to quickly learn how to create a CockroachDB database and connect to it from a client application. |
false |
This page is CockroachDB's Hello, World! tutorial. It walks you through creating a database, granting privileges on the database to a user, and then connecting with that user from your preferred language to execute basic statements as well as more complex transactions.
Make sure you have already:
- Installed CockroachDB
- Started a local cluster in insecure mode
- Installed a client driver
As the root
user, use the built-in SQL client to create a bank
database and grant privileges to the maxroach
user. The privileges will enable the user to execute statements in the next steps.
$ cockroach sql -e 'CREATE DATABASE bank'
$ cockroach sql -e 'GRANT ALL ON DATABASE bank TO maxroach'
As the maxroach
user, use the built-in SQL client to create an accounts
table in the new database.
$ cockroach sql --database=bank --user=maxroach -e 'CREATE TABLE accounts (id INT PRIMARY KEY, balance INT)'
As the maxroach
user, connect from your preferred language, insert a few rows into the accounts
table, and read and print the rows.
{% include app/basic-sample.py %}
The balance printout will look like this:
Initial balances:
['1', '1000']
['2', '250']
{% include app/basic-sample.rb %}
{% include app/basic-sample.go %}
{% include app/BasicSample.java %}
{% include app/basic-sample.js %}
{% include app/basic-sample.cpp %}
{% include app/basic-sample.clj %}
{% include app/basic-sample.php %}
{% include app/basic-sample.rs %}
As the maxroach
user, connect again from your preferred language, but this time execute a batch of statements as an atomic transaction, where all included statements are either commited or aborted.
{{site.data.alerts.callout_info}}Because the CockroachDB transaction model requires the client to initiate retries in the case of contention, CockroachDB provides a generic retry function that runs inside a transaction and retries it as needed. You can copy and paste the retry function from here into your code. For more details, see Transaction Retries.{{site.data.alerts.end}}
{% include app/txn-sample.py %}
Coming soon.
For Go, the CockroachDB retry function is in the crdb
package of the CockroachDB Go client. You can clone the library into your $GOPATH
as follows:
$ mkdir -p $GOPATH/github.com/cockroachdb
$ cd $GOPATH/github.com/cockroachdb
$ git clone [email protected]:cockroachdb/cockroach-go.git
{% include app/txn-sample.go %}
Coming soon.
{% include app/txn-sample.js %}
{% include app/txn-sample.cpp %}
{% include app/txn-sample.clj %}
{% include app/txn-sample.php %}
{% include app/txn-sample.rs %}
Use a local cluster to explore the following core CockroachDB features:
<script> $(document).ready(function(){ var $filter_button = $('.filter-button'); $filter_button.on('click', function(){ var language = $(this).data('language'), $current_tab = $('.filter-button.current'), $current_content = $('.filter-content.current'); //remove current class from tab and content $current_tab.removeClass('current'); $current_content.removeClass('current'); //add current class to clicked button and corresponding content block $('.filter-button[data-language="'+language+'"]').addClass('current'); $('.filter-content[data-language="'+language+'"]').addClass('current'); }); }); </script>