Path: blob/main/docs/_sources/api.rst.txt
469 views
.. module:: singlestoredb1.. _api:2API Reference3=============4.. _api.functions:5Connections6-----------7The :func:`connect` function is the primary entry point for the SingleStore8package. It connects to a SingleStore database using either9`DB-API <https://peps.python.org/pep-0249/>`_ compliant parameters,10or a connection string in the form of a URL.11The :func:`vector_db` function gives you an interface for working with12an API better for working with vector data.13The :func:`create_engine` function is used with the SQLAlchemy package14to create an SQLAlchemy engine for SingleStoreDB connections. This is15primarily for use in environments where the connection parameters are16stored in environment variables so that you can create SingleStoreDB17connections without specifying any parameters in the code itself.18.. autosummary::19:toctree: generated/20connect21vector_db22create_engine23Connection24..........25Connection objects are created by the :func:`singlestoredb.connect` function. They are26used to create :class:`Cursor` objects for querying the database.27.. currentmodule:: singlestoredb.connection28.. autosummary::29:toctree: generated/30Connection31Connection.autocommit32Connection.close33Connection.commit34Connection.rollback35Connection.cursor36Connection.is_connected37Connection.enable_data_api38Connection.disable_data_api39Connection.vector_db40The :attr:`Connection.show` attribute of the connection objects allow you to access various41information about the server. The available operations are shown below.42.. currentmodule:: singlestoredb.connection43.. autosummary::44:toctree: generated/45ShowAccessor.aggregates46ShowAccessor.columns47ShowAccessor.create_aggregate48ShowAccessor.create_function49ShowAccessor.create_pipeline50ShowAccessor.create_table51ShowAccessor.create_view52ShowAccessor.databases53ShowAccessor.database_status54ShowAccessor.errors55ShowAccessor.functions56ShowAccessor.global_status57ShowAccessor.indexes58ShowAccessor.partitions59ShowAccessor.pipelines60ShowAccessor.plan61ShowAccessor.plancache62ShowAccessor.procedures63ShowAccessor.processlist64ShowAccessor.reproduction65ShowAccessor.schemas66ShowAccessor.session_status67ShowAccessor.status68ShowAccessor.table_status69ShowAccessor.tables70ShowAccessor.warnings71ShowResult72^^^^^^^^^^73The results of the above methods and attributes are in the form of a74:class:`ShowResult` object. This object is primarily used to display75information to the screen or web browser, but columns from the output76can also be accessed using dictionary-like key access syntax or77attributes.78.. currentmodule:: singlestoredb.connection79.. autosummary::80:toctree: generated/81ShowResult82Cursor83......84Cursors are used to query the database and download results. They are85created using the :meth:`Connection.cursor` method.86.. currentmodule:: singlestoredb.connection87.. autosummary::88:toctree: generated/89Cursor90Cursor.callproc91Cursor.close92Cursor.execute93Cursor.executemany94Cursor.fetchone95Cursor.fetchmany96Cursor.fetchall97Cursor.nextset98Cursor.setinputsizes99Cursor.setoutputsize100Cursor.scroll101Cursor.next102Cursor.is_connected103Uploading from streaming sources104^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^105While the standard way of loading a local file using ``LOAD DATA LOCAL INFILE``106is well-known, it is possible to load data from programmatic sources107as well. This means that you can generate data with a Python function108and load that data into the database without writing it to a file109first. Below are two methods of doing this. Both of them use the110``local_infile=true`` parameter on the connection and the ``infile_stream=``111option on the ``cursor.execute`` method.112Uploading data using a generator function113:::::::::::::::::::::::::::::::::::::::::114.. sourcecode:: python115conn = s2.connect('s2-host.com/my_db?local_infile=true')116cur = conn.cursor()117def upload_csv():118yield '1,2,3\n'119yield '4,5,6\n'120yield '7,8,9\n'121# Note that the data returned does not have to122# correspond to a single row. Any length of123# data can be returned.124yield '10,11,12\n13,14,15\n'125cur.execute('CREATE TABLE generator (a INT, b INT, c INT)')126cur.execute(127'''128LOAD DATA LOCAL INFILE ':stream:' INTO TABLE generator129FIELDS TERMINATED BY ',' ENCLOSED BY '"'130''',131infile_stream=upload_csv(),132)133Uploading data using a queue and threads134::::::::::::::::::::::::::::::::::::::::135.. sourcecode:: python136from queue import Queue137from threading import Thread138conn = s2.connect('s2-host.com/my_db?local_infile=true')139cur = conn.cursor()140data_feeder = Queue()141cur.execute('CREATE TABLE queue (a INT, b INT, c INT)')142t = Thread(143target=cur.execute,144args=(145'''146LOAD DATA LOCAL INFILE ':stream:' INTO TABLE queue147FIELDS TERMINATED BY ',' ENCLOSED BY '"'148''',149),150kwargs=dict(infile_stream=data_feeder),151)152t.start()153data_feeder.put('1,2,3\n')154data_feeder.put('4,5,6\n')155data_feeder.put('7,8,9\n')156# Note that the data sent does not have to157# correspond to a single row. Any length of158# data can be sent.159data_feeder.put('10,11,12\n13,14,15\n')160# Send an empty string to end the data loader.161data_feeder.put('')162t.join()163Utilities164---------165.. currentmodule:: singlestoredb.auth166.. autosummary::167:toctree: generated/168get_jwt169Management API170--------------171The management objects allow you to create, destroy, and interact with172workspaces in the SingleStoreDB Cloud.173The :func:`manage_workspaces` function will return a :class:`WorkspaceManager`174object that can be used to interact with the Management API.175.. currentmodule:: singlestoredb176.. autosummary::177:toctree: generated/178manage_workspaces179WorkspaceManager180................181WorkspaceManager objects are returned by the :func:`manage_workspaces` function.182They allow you to retrieve information about workspaces in your account, or183create new ones.184.. currentmodule:: singlestoredb.management.workspace185.. autosummary::186:toctree: generated/187WorkspaceManager188WorkspaceManager.organization189WorkspaceManager.workspace_groups190WorkspaceManager.starter_workspaces191WorkspaceManager.regions192WorkspaceManager.shared_tier_regions193WorkspaceManager.create_workspace_group194WorkspaceManager.create_workspace195WorkspaceManager.create_starter_workspace196WorkspaceManager.get_workspace_group197WorkspaceManager.get_workspace198WorkspaceManager.get_starter_workspace199WorkspaceGroup200..............201WorkspaceGroup objects are retrieved from :meth:`WorkspaceManager.get_workspace_group`202or by retrieving an element from :attr:`WorkspaceManager.workspace_groups`.203.. autosummary::204:toctree: generated/205WorkspaceGroup206WorkspaceGroup.workspaces207WorkspaceGroup.stage208WorkspaceGroup.create_workspace209WorkspaceGroup.refresh210WorkspaceGroup.update211WorkspaceGroup.terminate212Workspace213.........214Workspaces are created within WorkspaceGroups. They can be created using either215:meth:`WorkspaceGroup.create_workspace` or retrieved from216:attr:`WorkspaceManager.workspaces`.217.. autosummary::218:toctree: generated/219Workspace220Workspace.connect221Workspace.refresh222Workspace.update223Workspace.terminate224Region225......226Region objects are accessed from the :attr:`WorkspaceManager.regions` attribute.227.. currentmodule:: singlestoredb.management.region228.. autosummary::229:toctree: generated/230Region231Organization232............233Organization objects are retrieved from :attr:`WorkspaceManager.organization`.234They provide access to organization-level resources and operations.235.. currentmodule:: singlestoredb.management.organization236.. autosummary::237:toctree: generated/238Organization239Organization.get_secret240Organization.jobs241Organization.inference_apis242Secret243......244Secret objects are retrieved from :meth:`Organization.get_secret`.245They represent organization-wide secrets that can be used in various operations.246.. autosummary::247:toctree: generated/248Secret249Jobs Management250...............251The jobs management functionality allows you to schedule and manage notebook executions.252JobsManager253^^^^^^^^^^^254JobsManager objects are accessed through :attr:`Organization.jobs` and provide255methods for creating, monitoring, and managing scheduled notebook jobs.256.. currentmodule:: singlestoredb.management.job257.. autosummary::258:toctree: generated/259JobsManager260JobsManager.schedule261JobsManager.run262JobsManager.wait263JobsManager.get264JobsManager.get_executions265JobsManager.get_parameters266JobsManager.delete267JobsManager.modes268JobsManager.runtimes269Job270^^^271Job objects represent scheduled notebook executions and are returned by272:meth:`JobsManager.schedule`, :meth:`JobsManager.run`, and :meth:`JobsManager.get`.273.. autosummary::274:toctree: generated/275Job276Job.wait277Job.get_executions278Job.get_parameters279Job.delete280Supporting Classes281^^^^^^^^^^^^^^^^^^282The following classes are used as parameters and return values in the jobs API.283.. autosummary::284:toctree: generated/285Mode286TargetType287Status288Parameter289Runtime290JobMetadata291ExecutionMetadata292Execution293ExecutionsData294ExecutionConfig295Schedule296TargetConfig297Stage Files298...........299To interact with files in your Stage, use the300:attr:`WorkspaceGroup.stage` attribute.301It will return a :class:`Stage` object which defines the following302methods and attributes.303.. currentmodule:: singlestoredb.management.workspace304.. autosummary::305:toctree: generated/306Stage307Stage.open308Stage.download_file309Stage.download_folder310Stage.upload_file311Stage.upload_folder312Stage.info313Stage.listdir314Stage.exists315Stage.is_dir316Stage.is_file317Stage.mkdir318Stage.rename319Stage.remove320Stage.removedirs321Stage.rmdir322Personal, Shared, and Model Files323.................................324To manage personal files, shared files, and model files in an organization,325you use the :func:`singlestoredb.manage_files` function. This will return a326:class:`FilesManager` instance to access each location. You then use the327returned :class:`FileSpace` to manage the files in each location. These328classes are described below.329.. currentmodule:: singlestoredb330.. autosummary::331:toctree: generated/332manage_files333The :class:`FilesManager` gives you access to each of the personal,334shared, and model file locations.335.. currentmodule:: singlestoredb.management.files336.. autosummary::337:toctree: generated/338FilesManager339FilesManager.personal_space340FilesManager.shared_space341FilesManager.models_space342The :class:`FileSpace` contains the methods for interacting with the343personal, shared, or model files.344.. currentmodule:: singlestoredb.management.files345.. autosummary::346:toctree: generated/347FileSpace348FileSpace.open349FileSpace.download_file350FileSpace.download_folder351FileSpace.upload_file352FileSpace.upload_folder353FileSpace.info354FileSpace.listdir355FileSpace.exists356FileSpace.is_dir357FileSpace.is_file358FileSpace.mkdir359FileSpace.rename360FileSpace.remove361FileSpace.removedirs362FileSpace.rmdir363FilesObject364...........365:class:`FilesObject`s are returned by the :meth:`StageObject.upload_file`366:meth:`FilesObject.upload_folder`, :meth:`FilesObject.mkdir`,367:meth:`FilesObject.rename`, and :meth:`FilesObject.info` methods.368.. currentmodule:: singlestoredb.management.workspace369.. autosummary::370:toctree: generated/371FilesObject372FilesObject.open373FilesObject.download374FilesObject.exists375FilesObject.is_dir376FilesObject.is_file377FilesObject.abspath378FilesObject.basename379FilesObject.dirname380FilesObject.getmtime381FilesObject.getctime382FilesObject.rename383FilesObject.remove384FilesObject.removedirs385FilesObject.rmdir386Notebook Tools387--------------388The SDK includes a `notebook` sub-module for tools that are for use in389the `SingleStore Managed Service Portal Notebooks <https://portal.singlestore.com>`_390environment. The following objects in `sinlgestoredb.notebook` are391singletons that automatically track the organization, workspace group, workspace,392stage, and secrets that are selected in the portal.393These objects act just like the corresponding objects discussed in previous394of this documentation (including attributes and methods), but they proxy all395calls to the currently selected portal services.396.. currentmodule:: singlestoredb.notebook397.. autosummary::398:toctree: generated/399organization400secrets401workspace_group402stage403workspace404Server Tools405------------406It is possible to start a SingleStoreDB server from the Python SDK in a407couple of ways: Docker or Cloud Free Tier. Both of these methods are408described below.409Docker410......411If you have Docker installed on your machine, you can use the Docker interface412included with the Python SDK to start a SingleStoreDB server in Docker. This413allows you to open interactive shells, SQL Studio, and also use the `%sql`414magic commands from Jupysql with a SingleStoreDB server running in a container.415An example of starting SingleStoreDB in Docker is shown below.416.. sourcecode:: python417from singlestoredb.server import docker418s2db = docker.start()419with s2db.connect() as conn:420with conn.cursor() as cur:421cur.execute('SHOW DATABASES')422for line in cur:423print(line)424s2db.stop()425It is possible to use the server instance as a context manager as well.426This will automatically shut down the container after exiting the ``with``427block.428.. sourcecode:: python429from singlestoredb.server import docker430with docker.start() as s2db:431with s2db.connect() as conn:432with conn.cursor() as cur:433cur.execute('SHOW DATABASES')434for line in cur:435print(line)436If you do not explicitly shut down the container, it will get shut437down when the Python process exits.438.. currentmodule:: singlestoredb.server.docker439.. autosummary::440:toctree: generated/441start442SingleStoreDB.logs443SingleStoreDB.connect444SingleStoreDB.connect_kai445SingleStoreDB.connection_url446SingleStoreDB.http_connection_url447SingleStoreDB.kai_url448SingleStoreDB.studio_url449SingleStoreDB.open_studio450SingleStoreDB.open_shell451SingleStoreDB.open_mongosh452SingleStoreDB.stop453Cloud Free Tier454...............455The Cloud Free Tier interface works much in the same way as the Docker456interface, although it doesn't support as many features. To start a457server in the Cloud Free Tier, you use the following:458.. sourcecode:: python459from singlestoredb.server import free_tier460s2db = free_tier.start()461with s2db.connect() as conn:462with conn.cursor() as cur:463cur.execute('SHOW DATABASES')464for line in cur:465print(line)466s2db.stop()467Just as with the Docker interface, you can also use a Python468context manager to automatically shut down your connection.469.. currentmodule:: singlestoredb.server.free_tier470.. autosummary::471:toctree: generated/472start473SingleStoreDB.connect474SingleStoreDB.connection_url475SingleStoreDB.http_connection_url476SingleStoreDB.kai_url477SingleStoreDB.open_shell478SingleStoreDB.open_mongosh479SingleStoreDB.stop480Configuration481-------------482The following functions are used to get and set package configuration settings.483Execute the :func:`describe_option` function with no parameters to484see the documentation for all options.485.. currentmodule:: singlestoredb486.. autosummary::487:toctree: generated/488get_option489set_option490describe_option491In addition to the function above, you can access options through the492``singlestoredb.options`` object. This gives you attribute-like access to the option493values.494.. ipython:: python495import singlestoredb as s2496s2.describe_option('local_infile')497s2.options.local_infile498s2.options.local_infile = True499s2.describe_option('local_infile')500.. ipython:: python501:suppress:502s2.options.local_infile = False503504505