#!/usr/bin/env python # coding: utf-8 # # # Aerospike Notebooks Readme/Tips # # Here are some tips and tricks for ease of use and productive experience with Aerospike notebooks. #
# This notebook requires Aerospike database running on localhost and that python and the Aerospike python client have been installed (`pip install aerospike`). Visit [Aerospike notebooks repo](https://github.com/aerospike-examples/interactive-notebooks) for additional details and the docker container. # ## Learn about Jupyter Notebook # The Jupyter Notebook provides "a web-based application suitable for capturing the whole computation process: developing, documenting, and executing code, as well as communicating the results". New to notebooks? Here is [one source](https://jupyter-notebook.readthedocs.io/en/stable/examples/Notebook/examples_index.html) to learn more about the Jupyter Notebook. # ## Find and run Aerospike notebook. # Visit [Aerospike notebooks repo](https://github.com/aerospike-examples/interactive-notebooks) to find additional Aerospike notebooks. To run another notebook, download the notebook from the repo to your local machine, and then click on File->Open, and select Upload. # ## Access shell commands # # Use the "!" line magic and "%%bash" cell magic to access shell commands. That is, you can access a shell command on any line by prefixing it with a "!", and an entire cell can have bash shell commands if it starts with "%%bash". Here are some examples: # In[6]: # Accessing shell commands get_ipython().system('ps') get_ipython().system('whoami') # Start the Aerospike database. get_ipython().system('asd >& /dev/null') # In[7]: get_ipython().run_cell_magic('bash', '', '# bash cell\n# Check if the Aerospike database is running.\npgrep -x asd >/dev/null && echo "Aerospike database is running" || echo "**Aerospike database is not running!**"\nps -axu | grep asd\n') # ### Shell commands from Java kernel # The Java kernel currently does not have a robust command shell access. The line magic %sh is limited in what it supports. For example, tools like aql or asadm that take arguments do not work, for example: ‘aql -c “select * from test.demo”’. Actions such as changing the server config or tailing server log may not be possible through the Java kernel’s %sh escape. # # There are multiple ways to access a functional shell from a Java notebook: # # 1. Use a terminal window. From the directory view in the notebook (if in a notebook view, launch File->Open), there is a New menu on the top right side. Select New->Terminal for a fully functional shell. # # 2. Use Python kernel’s shell magic. Start a new Python notebook (File->New Notebook->Python 3) and use the “!!” cell magic or “!” line magic to access shell commands. The Python kernel’s shell access works better than Java kernel’s at this point. # ## Examine server log # It is useful to examine the server log. Assuming it is located at /var/log/aerospike/aerospike.log, and you have the permissions, you can run the following to view the last 10 lines of the log. (Adjust the log path to your setting.) # In[8]: # View the last 10 lines of the log: get_ipython().system('echo "End of server log:"; tail -10 /var/log/aerospike/aerospike.log') # ## View database state. # # The command line tool "aql" can be very handy to examine the data and metadata in the database. For a more complete description of the capabilities, see the [doc](https://www.aerospike.com/docs/tools/aql/index.html). Assuming the database has namespace "test", the following commands can be executed. # In[9]: # Insert a record in set "demo" in namsepace "test" with Primary Key (PK) 1 and a bin or field "testbin" # with value "hello world!". get_ipython().system('aql -c "INSERT INTO test.demo (PK, \'testbin\') VALUES (1, \'hello world!\')"') # View all records in the set. get_ipython().system('aql -c "SELECT * FROM test.demo"') # Delete the record get_ipython().system('aql -c "DELETE FROM test.demo WHERE PK = 1"') get_ipython().system('aql -c "SELECT * FROM test.demo"') # ## View cluster state. # Another useful utility is asadm which can be used to view various aspects of the database cluster. For a more complete description of its capabilities, see the [doc](https://www.aerospike.com/docs/tools/asadm/index.html). # In[10]: # Show the features enabled in this database. get_ipython().system('asadm -e "features"') # Display summary info for the cluster get_ipython().system('asadm -e "summary"') # View the config get_ipython().system('asadm -e "show config"') # ## Next steps # Visit [Aerospike notebooks repo](https://github.com/aerospike-examples/interactive-notebooks) to run additional Aerospike notebooks. To run a different notebook, download the notebook from the repo to your local machine, and then click on File->Open, and select Upload.