Skip to content

Latest commit

 

History

History
254 lines (186 loc) · 7.16 KB

build-a-test-app.md

File metadata and controls

254 lines (186 loc) · 7.16 KB
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.

Before You Begin

Make sure you have already:

Step 1. Create a database and grant privileges

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'

Step 2. Create a table in the new database

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)' 

Step 3. Execute basic statements from a client

As the maxroach user, connect from your preferred language, insert a few rows into the accounts table, and read and print the rows.

Python Ruby Go Java Node.js C++ Clojure PHP Rust
**Python**
{% include app/basic-sample.py %}

The balance printout will look like this:

Initial balances:
['1', '1000']
['2', '250']
**Ruby**
{% include app/basic-sample.rb %}
**Go**
{% include app/basic-sample.go %}
**Java**
{% include app/BasicSample.java %}
**Node.js**
{% include app/basic-sample.js %}
**C++**
{% include app/basic-sample.cpp %}
**Clojure**
{% include app/basic-sample.clj %}
**PHP**
{% include app/basic-sample.php %}
**Rust**
{% include app/basic-sample.rs %}

Step 4. Execute transactions from a client

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}}

Python Ruby Go Java Node.js C++ Clojure PHP Rust
**Python**
{% include app/txn-sample.py %}
**Ruby**

Coming soon.

**Go**

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 %}
**Java**

Coming soon.

**Node.js**
{% include app/txn-sample.js %}
**C++**
{% include app/txn-sample.cpp %}
**Clojure**
{% include app/txn-sample.clj %}
**PHP**
{% include app/txn-sample.php %}
**Rust**
{% include app/txn-sample.rs %}

What's Next?

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>