### Install Python Libraries Source: https://github.com/singlestore-labs/spaces-notebooks/blob/master/notebooks/getting-started-with-singlestore/notebook.ipynb Installs necessary Python libraries, 'folium' for map visualization and 'shapely' for geospatial operations, using pip. The '--quiet' flag suppresses verbose output during installation. ```bash !pip install folium shapely --quiet ``` -------------------------------- ### Install and Run Pre-commit Hooks Source: https://github.com/singlestore-labs/spaces-notebooks/blob/master/notebooks/notebook-style-guide/notebook.ipynb Instructions for installing the pre-commit Python package and enabling it in the repository. This ensures checks run automatically before commits. ```sh # Install the pre-commit Python package (only needed once) pip3 install pre-commit # Run this command in the repository clone (only needed once) pre-commit install # Run all checks at any time pre-commit run --all-files ``` -------------------------------- ### Insert Sample Customer and Order Data Source: https://github.com/singlestore-labs/spaces-notebooks/blob/master/notebooks/getting-started-with-singlestore/notebook.ipynb Populates the 'customers' and 'orders' tables with sample records. This data is used for subsequent query examples to demonstrate database functionality. ```sql INSERT INTO customers (customer_id, customer_name, country) VALUES (1, "John Doe", "Canada"), (2, "Jane Smith", "Canada"), (3, "Sam Brown", "Canada"), (4, "Lisa White", "Canada"), (5, "Mark Black", "Canada"); INSERT INTO orders (order_id, customer_id, amount, product) VALUES (101, 1, 150.00, "Book"), (102, 2, 200.00, "Pen"), (103, 3, 50.00, "Notebook"), (104, 1, 300.00, "Laptop"), (105, 4, 250.00, "Tablet"); ``` -------------------------------- ### Install SingleStoreDB DataFrame Package Source: https://github.com/singlestore-labs/spaces-notebooks/blob/master/notebooks/getting-started-with-dataframes/notebook.ipynb Installs the necessary Python packages for SingleStoreDB integration with DataFrames, including the client, SQLAlchemy dialect, and Ibis backend. This command ensures all required libraries are available for use. ```bash pip install 'singlestoredb[dataframe]' ``` -------------------------------- ### Import Libraries for Data Handling Source: https://github.com/singlestore-labs/spaces-notebooks/blob/master/notebooks/getting-started-with-notebooks/notebook.ipynb Imports necessary libraries like pandas for data manipulation and shapely for handling geographic data. It also installs the shapely library quietly. ```python !pip3 install shapely --quiet import pandas as pd import shapely.wkt ``` -------------------------------- ### Full-Text Search: Using Operators (+, *, ?) Source: https://github.com/singlestore-labs/spaces-notebooks/blob/master/notebooks/getting-started-with-singlestore/notebook.ipynb Demonstrates advanced full-text search capabilities using operators. It searches for products where 'product' starts with 'oo' (using '?') or the 'description' contains words starting with 'versa' (using '*'). The '+' operator ensures a term must appear. ```SQL %%sql SELECT product FROM orders WHERE MATCH (TABLE orders) AGAINST ("product:(+oo?) OR description:versa*"); ``` -------------------------------- ### Install PyMongo and Import Modules Source: https://github.com/singlestore-labs/spaces-notebooks/blob/master/notebooks/getting-started-with-mongocdc/notebook.ipynb Installs the pymongo library, which is essential for interacting with MongoDB, and imports necessary Python modules like pymongo and random for the replication process. ```python !pip3 install pymongo --quiet import pymongo import random ``` -------------------------------- ### Connect to SingleStoreDB with SQLAlchemy Source: https://github.com/singlestore-labs/spaces-notebooks/blob/master/notebooks/getting-started-with-notebooks/notebook.ipynb Establishes a connection to SingleStoreDB using SQLAlchemy, either via a connection URL or automatically from the SINGLESTOREDB_URL environment variable. ```Python import sqlalchemy as sa # Create a SQLAlchemy engine and connect db_connection = sa.create_engine(connection_url).connect() ``` ```Python import singlestoredb as s2 # Create a SQLAlchemy engine and connect, without having to specify the connection URL db_connection = s2.create_engine().connect() ``` -------------------------------- ### Query Data with SQL Source: https://github.com/singlestore-labs/spaces-notebooks/blob/master/notebooks/getting-started-with-notebooks/notebook.ipynb Executes SQL queries against the SingleStoreDB database. Demonstrates selecting data and using the `result1 <<` syntax to capture query results into a Python variable for further processing. ```SQL SELECT * FROM {{database_name}}.sf_restaurant_scores LIMIT 10; ``` ```SQL SELECT DATE_TRUNC('month', inspection_date) AS month, COUNT(*) AS count_inspection FROM {{database_name}}.sf_restaurant_scores GROUP BY MONTH ORDER BY MONTH DESC; ``` -------------------------------- ### Install and Run Pre-commit Checks (Bash) Source: https://github.com/singlestore-labs/spaces-notebooks/blob/master/CONTRIBUTING.md Commands to install the pre-commit tool and set up Git hooks to run validation and code reformatting checks automatically before commits. Also includes a command to manually run all checks. ```bash pip3 install pre-commit==3.7.1 pre-commit install ``` ```bash pre-commit run --all-files ``` -------------------------------- ### Create and Use Database Source: https://github.com/singlestore-labs/spaces-notebooks/blob/master/notebooks/getting-started-with-notebooks/notebook.ipynb Dynamically creates a database for the notebook or uses the current one if on a shared tier. It demonstrates using Python variables within SQL cells via Jinja templating. ```Python shared_tier_check = %sql show variables like 'is_shared_tier' if not shared_tier_check or shared_tier_check[0][1] == 'OFF': database_name = 'getting_started_notebook' %sql DROP DATABASE IF EXISTS {{database_name}}; %sql CREATE DATABASE {{database_name}}; else: current_database = %sql SELECT DATABASE() as CurrentDatabase database_name = current_database[0][0] ``` -------------------------------- ### Bash: Install and Initialize Pre-commit Hooks Source: https://github.com/singlestore-labs/spaces-notebooks/blob/master/CONTRIBUTING.md This Bash snippet installs a specific version of the `pre-commit` tool using `pip3` and then initializes the pre-commit hooks in the current Git repository. This ensures that validation checks and code reformatting are run automatically before commits. ```bash pip3 install pre-commit==3.7.1 pre-commit install ``` -------------------------------- ### Display Available Fusion SQL Commands Source: https://github.com/singlestore-labs/spaces-notebooks/blob/master/notebooks/getting-started-with-fusion-sql/notebook.ipynb Lists all available Fusion SQL commands. The output can be filtered using the LIKE clause to find specific commands. ```python commands = %sql SHOW FUSION COMMANDS for cmd in commands: print(*cmd, '\n') ``` ```python commands = %sql SHOW FUSION COMMANDS LIKE '%stage%' for cmd in commands: print(*cmd, '\n') ``` -------------------------------- ### Connect to Workspace Database Endpoint Source: https://github.com/singlestore-labs/spaces-notebooks/blob/master/notebooks/getting-started-with-fusion-sql/notebook.ipynb Demonstrates how to establish a database connection to a workspace using its endpoint information retrieved from `SHOW WORKSPACES EXTENDED`. It uses the `singlestoredb` Python client. ```python import singlestoredb as s2 with s2.connect(f'admin:{ password }@{ workspaces[0].Endpoint }:3306') as conn: with conn.cursor() as cur: cur.execute('show databases') for row in cur: print(*row) ``` -------------------------------- ### Establish Database Connection Source: https://github.com/singlestore-labs/spaces-notebooks/blob/master/notebooks/getting-started-with-singlestore/notebook.ipynb Connects to the database using SQLAlchemy. It requires a 'connection_url' variable, which is assumed to be configured elsewhere, to establish the engine for database interactions. ```python from sqlalchemy import * db_connection = create_engine(connection_url) ``` -------------------------------- ### Singlestore Connection API Source: https://github.com/singlestore-labs/spaces-notebooks/blob/master/notebooks/getting-started-with-dataframes/notebook.ipynb Provides documentation for key methods available on a Singlestore connection object (`conn`) and table objects (`phones_tbl`) for data management and inspection. ```APIDOC conn.create_table(name: str, obj: Union[pd.DataFrame, ibis.expr.types.Table], overwrite: bool = False) - Creates a table in the database from a DataFrame or Ibis expression. - Parameters: - name: The name of the table to create. - obj: The data source, either a pandas DataFrame or an Ibis Table expression. - overwrite: If True, drops the table if it already exists before creating. - Returns: An Ibis Table object representing the newly created table. conn.drop_table(name: str) - Drops a table from the database. - Parameters: - name: The name of the table to drop. conn.show.create_table(name: str) - Retrieves the SQL CREATE TABLE statement for a given table. - Parameters: - name: The name of the table. - Returns: A list of dictionaries, where each dictionary contains a 'CreateTable' key with the SQL statement. conn.show.tables() - Lists all tables in the current database. - Returns: A list of dictionaries, each representing a table with its schema information. Table.head(n: int = 5) - Returns the first `n` rows of the table as a pandas DataFrame. - Parameters: - n: The number of rows to return. Table.info() - Displays schema information for the table, including column names, types, and nullability. Table.columns - An attribute that provides a list of column names in the table. Table.schema() - Returns a Schema object representing the table's schema, allowing programmatic access to schema details. ColumnExpression.cast(type: str) - Casts a column expression to a specified data type (e.g., 'timestamp', 'int'). - Parameters: - type: The target data type string. - Returns: A new ColumnExpression with the casted type. ColumnExpression.name(name: str) - Assigns a name to a column expression. - Parameters: - name: The desired name for the column expression. - Returns: A new ColumnExpression with the assigned name. ``` -------------------------------- ### Add Full-Text Index Source: https://github.com/singlestore-labs/spaces-notebooks/blob/master/notebooks/getting-started-with-singlestore/notebook.ipynb Adds a full-text index named 'orders_ft_index' to the 'orders' table, covering the 'product' and 'description' columns using VERSION 2. It then optimizes the table and flushes changes. ```SQL %%sql ALTER TABLE orders ADD FULLTEXT USING VERSION 2 orders_ft_index (product, description); OPTIMIZE TABLE orders FLUSH; ``` -------------------------------- ### Display Orders with Descriptions Source: https://github.com/singlestore-labs/spaces-notebooks/blob/master/notebooks/getting-started-with-singlestore/notebook.ipynb Retrieves all columns from the 'orders' table, including the newly added and populated 'description' column. This step verifies that the descriptions have been correctly updated. ```SQL %%sql SELECT * FROM orders; ``` -------------------------------- ### Inspect Table Info Source: https://github.com/singlestore-labs/spaces-notebooks/blob/master/notebooks/getting-started-with-dataframes/notebook.ipynb Retrieves and displays information about the table created on the Singlestore server, similar to `DataFrame.info()`. Shows column details and types. ```python phones_tbl.info() ``` -------------------------------- ### Save Map to Stage (Python) Source: https://github.com/singlestore-labs/spaces-notebooks/blob/master/notebooks/getting-started-with-singlestore/notebook.ipynb Writes HTML content to a stage file named 'map.html'. This operation requires the 'singlestoredb.notebook' library and is restricted to the Standard Tier. ```python from singlestoredb import notebook as nb if not shared_tier_check or shared_tier_check[0][1] == "OFF": with nb.stage.open("map.html", "w") as st: st.write(html_content) ``` -------------------------------- ### Create a Workspace Group Source: https://github.com/singlestore-labs/spaces-notebooks/blob/master/notebooks/getting-started-with-fusion-sql/notebook.ipynb Executes the Fusion SQL command to create a workspace group with a specified name, region ID, firewall access from all IPs ('0.0.0.0/0'), and a generated password. ```python %%sql CREATE WORKSPACE GROUP '{{ wsg_name }}' IN REGION ID '{{ region_id }}' WITH FIREWALL RANGES '0.0.0.0/0' WITH PASSWORD '{{ password }}' ``` -------------------------------- ### Visualize Data with Plotly Source: https://github.com/singlestore-labs/spaces-notebooks/blob/master/notebooks/getting-started-with-notebooks/notebook.ipynb Uses Plotly Express to create a bar chart visualizing the count of inspections by month. Requires converting the query result to a pandas DataFrame first. ```Python import plotly.express as px px.bar(result1_df, x='month', y='count_inspection', title='Inspections by Month') ``` -------------------------------- ### Connect to SingleStoreDB with Ibis Source: https://github.com/singlestore-labs/spaces-notebooks/blob/master/notebooks/getting-started-with-dataframes/notebook.ipynb Establishes a connection to the SingleStoreDB database using the Ibis library. It utilizes default connection parameters, often sourced from environment variables in notebook environments. ```python conn = ibis.singlestoredb.connect() ``` -------------------------------- ### Database Initialization and Setup Source: https://github.com/singlestore-labs/spaces-notebooks/blob/master/notebooks/inserting-embeddings-from-multiple-models-into-singlestore-using-external-functions/notebook.ipynb This snippet demonstrates the initial steps for setting up a database environment in SingleStoreDB. It includes dropping an existing database if present, creating a new database named 'embeddings_demo', and then switching the current context to this new database for subsequent operations. ```sql %%sql DROP DATABASE IF EXISTS embeddings_demo; CREATE DATABASE embeddings_demo; USE embeddings_demo; ``` -------------------------------- ### Create and Use Database Source: https://github.com/singlestore-labs/spaces-notebooks/blob/master/notebooks/basic-query-examples/notebook.ipynb Creates a new database named 'memsql_example' and sets it as the active database for subsequent operations. This is a foundational step before creating tables or running queries. ```sql CREATE DATABASE memsql_example; USE memsql_example; ``` -------------------------------- ### Fusion SQL Command Syntax: SHOW REGIONS Source: https://github.com/singlestore-labs/spaces-notebooks/blob/master/notebooks/getting-started-with-fusion-sql/notebook.ipynb Specifies the syntax for retrieving region information. It supports filtering with a LIKE pattern, ordering results, and limiting the number of returned regions. ```APIDOC SHOW REGIONS [ LIKE '' ] [ ORDER BY '' [ ASC | DESC ],... ] [ LIMIT ]; ``` -------------------------------- ### Start Pipeline - SQL Source: https://github.com/singlestore-labs/spaces-notebooks/blob/master/notebooks/singlestore-cheat-sheet/notebook.ipynb Initiates a previously defined pipeline to begin loading data. This command starts the data ingestion process. ```SQL START PIPELINE SalesData_Pipeline; ``` -------------------------------- ### Get CREATE TABLE Statement Source: https://github.com/singlestore-labs/spaces-notebooks/blob/master/notebooks/getting-started-with-dataframes/notebook.ipynb Retrieves the SQL `CREATE TABLE` statement generated by the data upload process. This allows inspection of how data types were mapped to SQL types. ```python ct = conn.show.create_table('phones') print(ct[0]['CreateTable']) ``` -------------------------------- ### List Numeric Methods on Ibis Column Source: https://github.com/singlestore-labs/spaces-notebooks/blob/master/notebooks/getting-started-with-dataframes/notebook.ipynb Iterates through the methods available on an Ibis numeric column object and prints those that do not start with an underscore. This helps in discovering available numeric operations. ```Python for x in dir(phones_tbl.overall): if not x.startswith('_'): print(x) ``` -------------------------------- ### Initialize Environment and OpenAI Client Source: https://github.com/singlestore-labs/spaces-notebooks/blob/master/notebooks/insure-gpt-demo/notebook.ipynb Sets up the Python environment by importing necessary libraries and configuring essential services. It sets the SingleStoreDB connection URL via an environment variable and initializes the OpenAI client with an API key, preparing for data processing and AI model interactions. ```python from fpdf import FPDF from langchain.text_splitter import CharacterTextSplitter from langchain_community.document_loaders import TextLoader from langchain_community.embeddings import OpenAIEmbeddings from openai import OpenAI import os import base64 from langchain_community.vectorstores.singlestoredb import SingleStoreDB os.environ["SINGLESTOREDB_URL"] = "' | '' } [ IN GROUP { ID '' | '' } ] [ WAIT ON RESUMED ]; SUSPEND WORKSPACE { ID '' | '' } [ IN GROUP { ID '' | '' } ] [ WAIT ON SUSPENDED ]; ``` ```python %%sql SUSPEND WORKSPACE 'workspace-1' IN GROUP '{{ wsg_name }}' ``` ```python %%sql RESUME WORKSPACE 'workspace-1' IN GROUP '{{ wsg_name }}' WAIT ON RESUMED ``` -------------------------------- ### Upload DataFrame to Singlestore Source: https://github.com/singlestore-labs/spaces-notebooks/blob/master/notebooks/getting-started-with-dataframes/notebook.ipynb Creates a new table in the Singlestore server from a pandas DataFrame. The `overwrite=True` parameter ensures that an existing table with the same name is replaced. ```python phones_tbl = conn.create_table('phones', phones_df, overwrite=True) phones_tbl.head(3) ``` -------------------------------- ### Setup Environment for FastAPI App Source: https://github.com/singlestore-labs/spaces-notebooks/blob/master/notebooks/cloud-functions-template/notebook.ipynb Configures the environment for a FastAPI application by defining the Data Model using Pydantic and setting up a thread executor for concurrent request handling. ```python from fastapi import FastAPI, HTTPException from pydantic import BaseModel from singlestoredb import connect from concurrent.futures import ThreadPoolExecutor import asyncio # Define the Type of the Data class Item(BaseModel): id: int name: str price: float # Create an executor that can execute queries on multiple threads simultaneously executor = ThreadPoolExecutor() def run_in_thread(fn, *args): loop = asyncio.get_event_loop() return loop.run_in_executor(executor, fn, *args) ``` -------------------------------- ### Count Rows Matching Filter Expression Source: https://github.com/singlestore-labs/spaces-notebooks/blob/master/notebooks/getting-started-with-dataframes/notebook.ipynb Filters the original table ('phones_tbl') using the 'has_iphone_4' boolean expression and then counts the number of rows that satisfy the condition. ```Python phones_tbl[has_iphone_4].count() ``` -------------------------------- ### Install Perspective Library Source: https://github.com/singlestore-labs/spaces-notebooks/blob/master/notebooks/network-intrusion-detection-part-3/notebook.ipynb Installs the `perspective-python` library, which is essential for creating interactive, real-time dashboards and data visualizations. ```python %pip install perspective-python --quiet ``` -------------------------------- ### Orders Count by Customer Over Time Source: https://github.com/singlestore-labs/spaces-notebooks/blob/master/notebooks/getting-started-with-singlestore/notebook.ipynb Provides a daily count of orders for each customer. It parses the 'order_date' from 'additional_details', formats it to 'YYYY-MM-DD', and groups the results by customer ID and date. ```sql SELECT customer_id, DATE_FORMAT(STR_TO_DATE(additional_details::order_date, '"%Y-%m-%d"'), '%Y-%m-%d') AS order_date, COUNT(*) AS order_count FROM orders GROUP BY customer_id, order_date ORDER BY customer_id, order_date; ```