---
#Front matter (metadata).
authors:
- name: "Srikanth Manne"
email: "srikanth.manne@in.ibm.com"
- name: "Manoj Jahgirdar"
email: "manoj.jahgirdar@in.ibm.com"
- name: "Rahul Reddy Ravipally"
email: "raravi86@in.ibm.com"
- name: "Manjula G. Hosurmath"
email: "mhosurma@in.ibm.com"
completed_date: 2020-09-21
check_date: 2021-09-21
draft: true
excerpt: In this tutorial, learn how to perform DML operations with CockroachDB hosted on Red Hat® Marketplace using the Python runtime and a Jupyter notebook.
abstract: In this tutorial, learn how to perform DML operations with CockroachDB hosted on Red Hat® Marketplace using the Python runtime and a Jupyter notebook.
meta_keywords: 'CockroachDB operator, Red hat Marketplace
related_content:
- type: tutorials
slug: get-started-using-a-cockroachdb-operator-hosted-on-red-hat-marketplace
- type: tutorials
slug: store-and-query-unstructured-json-data-using-cockroachdb-hosted-on-rhm
- type: patterns
slug: build-library-management-system-cockroachdb-red-hat-marketplace
meta_keywords: 'CockroachDB operator, Red hat Marketplace'
primary_tag: databases
tags:
- databases
- containers
related_links:
- title: '"CockroackDB on Red Hat Marketplace'
url: 'https://marketplace.redhat.com/en-us/products/cockroachdb-operator'
- title: Red Hat Marketplace
url: '"https://marketplace.redhat.com/'
collections:
- "red-hat-marketplace"
subtitle: Perform DML Operations with CockroachDB hosted on Red Hat Marketplace
title: Perform DML Operations with CockroachDB
meta_title: Perform DML Operations with CockroachDB hosted on Red Hat Marketplace
---
The SQL commands that deal with data manipulation are categorised under Data Manipulation Language (DML). The commands include CREATE, INSERT, UPDATE, READ & DELETE a table. In this tutorial, we demonstrate working with these commands on a CockroachDB hosted on Red Hat Marketplace using the Python runtime and a Jupyter notebook. CockroachDB is an elastic SQL database that easily scales transactions for your apps and services.
When you have completed this tutorial, you will understand how to:
- Install a CockroachDB Operator from Red Hat Marketplace on a OpenShift Cluster
- Create a CockroachDB cluster instance
- Create a user and database in CockroachDB
- Perform DML operations on CockroachDB
- Manage a CockroachDB cluster from the Cluster Overview page <--EM: in the RH OpenShift console?-->
Completing this tutorial should take about 30 minutes.
- Red Hat Marketplace Account.
- Red Hat OpenShift Cluster.
- OpenShift container & kubectl CLI.
- Access to a Jupyter Notebook. You can install a Jupyter Notebook from python-pip or use a tool such as Anaconda to open the Jupyter Notebook.
Follow the steps in this tutorial to deploy a CockroachDB Operator from Red Hat Marketplace on an OpenShift Cluster:
Once you have successfully set up a CockroachDB Operator on an OpenShift Cluster you can create a database.
Now, let's create a user
and a database
.
-
Run the following command to spin up a CockroachDB client:
$ kubectl run -it --rm cockroach-client \ --image=cockroachdb/cockroach \ --restart=Never \ --command -- \ ./cockroach sql --insecure --host=example-cockroachdb-public.cockroachdb-test
This should run the CockroachDB client and take you to a
SQL Command Prompt
as shown. If you don't see a command prompt, try pressing Enter.root@example-cockroachdb-public.cockroachdb-test:26257/defaultdb>
-
From the CockroachDB client, run the following commands:
-
Create a user
maxroach
as follows:root@example-cockroachdb-public.cockroachdb-test:26257/defaultdb> CREATE USER IF NOT EXISTS maxroach; CREATE USER 1 Time: 9.580878ms
-
Create a database
bank
as follows:root@example-cockroachdb-public.cockroachdb-test:26257/defaultdb> CREATE DATABASE bank; CREATE DATABASE Time: 14.449525ms
-
Give our user,
maxroach
, permission to update our database,bank
as follows:root@example-cockroachdb-public.cockroachdb-test:26257/defaultdb> GRANT ALL ON DATABASE bank TO maxroach; GRANT Time: 9.308095ms
At this point, you should have a
user
and adatabase
.- Type
\q
to quit the client console as shown:
root@example-cockroachdb-public.cockroachdb-test:26257/defaultdb> \q pod "cockroach-client" deleted $
-
The following steps show you how to view the results of the commands you ran in the earlier steps via the admin console
. You can access the console at localhost with port forwarding.
Note: You need to be logged in to your OpenShift Cluster with the OC login that you accessed in Step 2: Connect to the OpenShift cluster in your CLI.
-
Run the following command to port forward
8080
:$ kubectl port-forward example-cockroachdb-0 8080
Forwarding from 127.0.0.1:8080 -> 8080 Forwarding from [::1]:8080 -> 8080
-
Visit http://localhost:8080 on your browser as shown. The page should load the cluster overview.
-
Click on
databases
to view theemployees
database that you created earlier.
Once you have the CockroachDB up and running and your user and database created, you can now perform DML Operations on CockroachDB in a Python runtime using a Jupyter Notebook.
-
In your terminal, run the following command to port forward the
26257
port from the CockroachDB database instance. You will use this port in your Jupyter Notebook to establish a connection with the CockroachDB database instance.$ kubectl port-forward example-cockroachdb-0 26257
Forwarding from 127.0.0.1:26257 -> 26257 Forwarding from [::1]:26257 -> 26257
-
Download and open the following Jupyter notebook dml-operations-with-cockroachdb.ipynb in your local machine.
-
Select the Cell tab and click Run All.
Follow the notebook instructions for more details on what is happening in each cell.
After you execute the notebook, from your terminal, verify the table in the ClockroachDB instance through the CockroachDB client.
-
In your terminal, run the following command to spin up a CockroachDB client:
$ kubectl run -it --rm cockroach-client \ --image=cockroachdb/cockroach \ --restart=Never \ --command -- \ ./cockroach sql --insecure --host=example-cockroachdb-public.cockroachdb-test
-
This should run the CockroachDB client and take you to a
SQL Command Prompt
as shown. If you don't see a command prompt, press Enter.root@example-cockroachdb-public.cockroachdb-test:26257/defaultdb>
-
From the CockroachDB client, run the following commands to view
user
,database
andtable
which was created with the Jupyter Notebook:-
View
users
by running theSHOW users;
command:root@example-cockroachdb-public.cockroachdb-test:26257/defaultdb> SHOW users; user_name `-------------` cpuser maxroach root (3 rows) Time: 3.037641ms
-
-
View
databases
by running theSHOW databases;
command as follows:root@example-cockroachdb-public.cockroachdb-test:26257/defaultdb> SHOW databases; database_name `-----------------` bank defaultdb postgres system (4 rows) Time: 2.890031ms
-
To view the tables present in
bank
database, run theUSE bank;
command to switch tobank
database. Run\d
command to view thetables
as follows:root@example-cockroachdb-public.cockroachdb-test:26257/defaultdb> USE bank; SET Time: 11.83841ms root@example-cockroachdb-public.cockroachdb-test:26257/bank> \d table_name `----------------------` accounts jsontbl test_bank_customer (3 rows) Time: 3.684617ms
-
Finally, to view the accounts table, use the
SELECT
command:root@example-cockroachdb-public.cockroachdb-test:26257/defaultdb> SELECT * from accounts;
This tutorial showed you how to perform DML operations on CockroachDB Operator using python runtime and Jupyter Notebook.
You can refer the following documentation from Cockroach labs to learn more about the operator and its features.