Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
singlestore-labs
GitHub Repository: singlestore-labs/singlestoredb-python
Path: blob/main/docs/_sources/api.rst.txt
469 views
1
.. module:: singlestoredb
2
.. _api:
3
API Reference
4
=============
5
.. _api.functions:
6
Connections
7
-----------
8
The :func:`connect` function is the primary entry point for the SingleStore
9
package. It connects to a SingleStore database using either
10
`DB-API <https://peps.python.org/pep-0249/>`_ compliant parameters,
11
or a connection string in the form of a URL.
12
The :func:`vector_db` function gives you an interface for working with
13
an API better for working with vector data.
14
The :func:`create_engine` function is used with the SQLAlchemy package
15
to create an SQLAlchemy engine for SingleStoreDB connections. This is
16
primarily for use in environments where the connection parameters are
17
stored in environment variables so that you can create SingleStoreDB
18
connections without specifying any parameters in the code itself.
19
.. autosummary::
20
:toctree: generated/
21
connect
22
vector_db
23
create_engine
24
Connection
25
..........
26
Connection objects are created by the :func:`singlestoredb.connect` function. They are
27
used to create :class:`Cursor` objects for querying the database.
28
.. currentmodule:: singlestoredb.connection
29
.. autosummary::
30
:toctree: generated/
31
Connection
32
Connection.autocommit
33
Connection.close
34
Connection.commit
35
Connection.rollback
36
Connection.cursor
37
Connection.is_connected
38
Connection.enable_data_api
39
Connection.disable_data_api
40
Connection.vector_db
41
The :attr:`Connection.show` attribute of the connection objects allow you to access various
42
information about the server. The available operations are shown below.
43
.. currentmodule:: singlestoredb.connection
44
.. autosummary::
45
:toctree: generated/
46
ShowAccessor.aggregates
47
ShowAccessor.columns
48
ShowAccessor.create_aggregate
49
ShowAccessor.create_function
50
ShowAccessor.create_pipeline
51
ShowAccessor.create_table
52
ShowAccessor.create_view
53
ShowAccessor.databases
54
ShowAccessor.database_status
55
ShowAccessor.errors
56
ShowAccessor.functions
57
ShowAccessor.global_status
58
ShowAccessor.indexes
59
ShowAccessor.partitions
60
ShowAccessor.pipelines
61
ShowAccessor.plan
62
ShowAccessor.plancache
63
ShowAccessor.procedures
64
ShowAccessor.processlist
65
ShowAccessor.reproduction
66
ShowAccessor.schemas
67
ShowAccessor.session_status
68
ShowAccessor.status
69
ShowAccessor.table_status
70
ShowAccessor.tables
71
ShowAccessor.warnings
72
ShowResult
73
^^^^^^^^^^
74
The results of the above methods and attributes are in the form of a
75
:class:`ShowResult` object. This object is primarily used to display
76
information to the screen or web browser, but columns from the output
77
can also be accessed using dictionary-like key access syntax or
78
attributes.
79
.. currentmodule:: singlestoredb.connection
80
.. autosummary::
81
:toctree: generated/
82
ShowResult
83
Cursor
84
......
85
Cursors are used to query the database and download results. They are
86
created using the :meth:`Connection.cursor` method.
87
.. currentmodule:: singlestoredb.connection
88
.. autosummary::
89
:toctree: generated/
90
Cursor
91
Cursor.callproc
92
Cursor.close
93
Cursor.execute
94
Cursor.executemany
95
Cursor.fetchone
96
Cursor.fetchmany
97
Cursor.fetchall
98
Cursor.nextset
99
Cursor.setinputsizes
100
Cursor.setoutputsize
101
Cursor.scroll
102
Cursor.next
103
Cursor.is_connected
104
Uploading from streaming sources
105
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
106
While the standard way of loading a local file using ``LOAD DATA LOCAL INFILE``
107
is well-known, it is possible to load data from programmatic sources
108
as well. This means that you can generate data with a Python function
109
and load that data into the database without writing it to a file
110
first. Below are two methods of doing this. Both of them use the
111
``local_infile=true`` parameter on the connection and the ``infile_stream=``
112
option on the ``cursor.execute`` method.
113
Uploading data using a generator function
114
:::::::::::::::::::::::::::::::::::::::::
115
.. sourcecode:: python
116
conn = s2.connect('s2-host.com/my_db?local_infile=true')
117
cur = conn.cursor()
118
def upload_csv():
119
yield '1,2,3\n'
120
yield '4,5,6\n'
121
yield '7,8,9\n'
122
# Note that the data returned does not have to
123
# correspond to a single row. Any length of
124
# data can be returned.
125
yield '10,11,12\n13,14,15\n'
126
cur.execute('CREATE TABLE generator (a INT, b INT, c INT)')
127
cur.execute(
128
'''
129
LOAD DATA LOCAL INFILE ':stream:' INTO TABLE generator
130
FIELDS TERMINATED BY ',' ENCLOSED BY '"'
131
''',
132
infile_stream=upload_csv(),
133
)
134
Uploading data using a queue and threads
135
::::::::::::::::::::::::::::::::::::::::
136
.. sourcecode:: python
137
from queue import Queue
138
from threading import Thread
139
conn = s2.connect('s2-host.com/my_db?local_infile=true')
140
cur = conn.cursor()
141
data_feeder = Queue()
142
cur.execute('CREATE TABLE queue (a INT, b INT, c INT)')
143
t = Thread(
144
target=cur.execute,
145
args=(
146
'''
147
LOAD DATA LOCAL INFILE ':stream:' INTO TABLE queue
148
FIELDS TERMINATED BY ',' ENCLOSED BY '"'
149
''',
150
),
151
kwargs=dict(infile_stream=data_feeder),
152
)
153
t.start()
154
data_feeder.put('1,2,3\n')
155
data_feeder.put('4,5,6\n')
156
data_feeder.put('7,8,9\n')
157
# Note that the data sent does not have to
158
# correspond to a single row. Any length of
159
# data can be sent.
160
data_feeder.put('10,11,12\n13,14,15\n')
161
# Send an empty string to end the data loader.
162
data_feeder.put('')
163
t.join()
164
Utilities
165
---------
166
.. currentmodule:: singlestoredb.auth
167
.. autosummary::
168
:toctree: generated/
169
get_jwt
170
Management API
171
--------------
172
The management objects allow you to create, destroy, and interact with
173
workspaces in the SingleStoreDB Cloud.
174
The :func:`manage_workspaces` function will return a :class:`WorkspaceManager`
175
object that can be used to interact with the Management API.
176
.. currentmodule:: singlestoredb
177
.. autosummary::
178
:toctree: generated/
179
manage_workspaces
180
WorkspaceManager
181
................
182
WorkspaceManager objects are returned by the :func:`manage_workspaces` function.
183
They allow you to retrieve information about workspaces in your account, or
184
create new ones.
185
.. currentmodule:: singlestoredb.management.workspace
186
.. autosummary::
187
:toctree: generated/
188
WorkspaceManager
189
WorkspaceManager.organization
190
WorkspaceManager.workspace_groups
191
WorkspaceManager.starter_workspaces
192
WorkspaceManager.regions
193
WorkspaceManager.shared_tier_regions
194
WorkspaceManager.create_workspace_group
195
WorkspaceManager.create_workspace
196
WorkspaceManager.create_starter_workspace
197
WorkspaceManager.get_workspace_group
198
WorkspaceManager.get_workspace
199
WorkspaceManager.get_starter_workspace
200
WorkspaceGroup
201
..............
202
WorkspaceGroup objects are retrieved from :meth:`WorkspaceManager.get_workspace_group`
203
or by retrieving an element from :attr:`WorkspaceManager.workspace_groups`.
204
.. autosummary::
205
:toctree: generated/
206
WorkspaceGroup
207
WorkspaceGroup.workspaces
208
WorkspaceGroup.stage
209
WorkspaceGroup.create_workspace
210
WorkspaceGroup.refresh
211
WorkspaceGroup.update
212
WorkspaceGroup.terminate
213
Workspace
214
.........
215
Workspaces are created within WorkspaceGroups. They can be created using either
216
:meth:`WorkspaceGroup.create_workspace` or retrieved from
217
:attr:`WorkspaceManager.workspaces`.
218
.. autosummary::
219
:toctree: generated/
220
Workspace
221
Workspace.connect
222
Workspace.refresh
223
Workspace.update
224
Workspace.terminate
225
Region
226
......
227
Region objects are accessed from the :attr:`WorkspaceManager.regions` attribute.
228
.. currentmodule:: singlestoredb.management.region
229
.. autosummary::
230
:toctree: generated/
231
Region
232
Organization
233
............
234
Organization objects are retrieved from :attr:`WorkspaceManager.organization`.
235
They provide access to organization-level resources and operations.
236
.. currentmodule:: singlestoredb.management.organization
237
.. autosummary::
238
:toctree: generated/
239
Organization
240
Organization.get_secret
241
Organization.jobs
242
Organization.inference_apis
243
Secret
244
......
245
Secret objects are retrieved from :meth:`Organization.get_secret`.
246
They represent organization-wide secrets that can be used in various operations.
247
.. autosummary::
248
:toctree: generated/
249
Secret
250
Jobs Management
251
...............
252
The jobs management functionality allows you to schedule and manage notebook executions.
253
JobsManager
254
^^^^^^^^^^^
255
JobsManager objects are accessed through :attr:`Organization.jobs` and provide
256
methods for creating, monitoring, and managing scheduled notebook jobs.
257
.. currentmodule:: singlestoredb.management.job
258
.. autosummary::
259
:toctree: generated/
260
JobsManager
261
JobsManager.schedule
262
JobsManager.run
263
JobsManager.wait
264
JobsManager.get
265
JobsManager.get_executions
266
JobsManager.get_parameters
267
JobsManager.delete
268
JobsManager.modes
269
JobsManager.runtimes
270
Job
271
^^^
272
Job objects represent scheduled notebook executions and are returned by
273
:meth:`JobsManager.schedule`, :meth:`JobsManager.run`, and :meth:`JobsManager.get`.
274
.. autosummary::
275
:toctree: generated/
276
Job
277
Job.wait
278
Job.get_executions
279
Job.get_parameters
280
Job.delete
281
Supporting Classes
282
^^^^^^^^^^^^^^^^^^
283
The following classes are used as parameters and return values in the jobs API.
284
.. autosummary::
285
:toctree: generated/
286
Mode
287
TargetType
288
Status
289
Parameter
290
Runtime
291
JobMetadata
292
ExecutionMetadata
293
Execution
294
ExecutionsData
295
ExecutionConfig
296
Schedule
297
TargetConfig
298
Stage Files
299
...........
300
To interact with files in your Stage, use the
301
:attr:`WorkspaceGroup.stage` attribute.
302
It will return a :class:`Stage` object which defines the following
303
methods and attributes.
304
.. currentmodule:: singlestoredb.management.workspace
305
.. autosummary::
306
:toctree: generated/
307
Stage
308
Stage.open
309
Stage.download_file
310
Stage.download_folder
311
Stage.upload_file
312
Stage.upload_folder
313
Stage.info
314
Stage.listdir
315
Stage.exists
316
Stage.is_dir
317
Stage.is_file
318
Stage.mkdir
319
Stage.rename
320
Stage.remove
321
Stage.removedirs
322
Stage.rmdir
323
Personal, Shared, and Model Files
324
.................................
325
To manage personal files, shared files, and model files in an organization,
326
you use the :func:`singlestoredb.manage_files` function. This will return a
327
:class:`FilesManager` instance to access each location. You then use the
328
returned :class:`FileSpace` to manage the files in each location. These
329
classes are described below.
330
.. currentmodule:: singlestoredb
331
.. autosummary::
332
:toctree: generated/
333
manage_files
334
The :class:`FilesManager` gives you access to each of the personal,
335
shared, and model file locations.
336
.. currentmodule:: singlestoredb.management.files
337
.. autosummary::
338
:toctree: generated/
339
FilesManager
340
FilesManager.personal_space
341
FilesManager.shared_space
342
FilesManager.models_space
343
The :class:`FileSpace` contains the methods for interacting with the
344
personal, shared, or model files.
345
.. currentmodule:: singlestoredb.management.files
346
.. autosummary::
347
:toctree: generated/
348
FileSpace
349
FileSpace.open
350
FileSpace.download_file
351
FileSpace.download_folder
352
FileSpace.upload_file
353
FileSpace.upload_folder
354
FileSpace.info
355
FileSpace.listdir
356
FileSpace.exists
357
FileSpace.is_dir
358
FileSpace.is_file
359
FileSpace.mkdir
360
FileSpace.rename
361
FileSpace.remove
362
FileSpace.removedirs
363
FileSpace.rmdir
364
FilesObject
365
...........
366
:class:`FilesObject`s are returned by the :meth:`StageObject.upload_file`
367
:meth:`FilesObject.upload_folder`, :meth:`FilesObject.mkdir`,
368
:meth:`FilesObject.rename`, and :meth:`FilesObject.info` methods.
369
.. currentmodule:: singlestoredb.management.workspace
370
.. autosummary::
371
:toctree: generated/
372
FilesObject
373
FilesObject.open
374
FilesObject.download
375
FilesObject.exists
376
FilesObject.is_dir
377
FilesObject.is_file
378
FilesObject.abspath
379
FilesObject.basename
380
FilesObject.dirname
381
FilesObject.getmtime
382
FilesObject.getctime
383
FilesObject.rename
384
FilesObject.remove
385
FilesObject.removedirs
386
FilesObject.rmdir
387
Notebook Tools
388
--------------
389
The SDK includes a `notebook` sub-module for tools that are for use in
390
the `SingleStore Managed Service Portal Notebooks <https://portal.singlestore.com>`_
391
environment. The following objects in `sinlgestoredb.notebook` are
392
singletons that automatically track the organization, workspace group, workspace,
393
stage, and secrets that are selected in the portal.
394
These objects act just like the corresponding objects discussed in previous
395
of this documentation (including attributes and methods), but they proxy all
396
calls to the currently selected portal services.
397
.. currentmodule:: singlestoredb.notebook
398
.. autosummary::
399
:toctree: generated/
400
organization
401
secrets
402
workspace_group
403
stage
404
workspace
405
Server Tools
406
------------
407
It is possible to start a SingleStoreDB server from the Python SDK in a
408
couple of ways: Docker or Cloud Free Tier. Both of these methods are
409
described below.
410
Docker
411
......
412
If you have Docker installed on your machine, you can use the Docker interface
413
included with the Python SDK to start a SingleStoreDB server in Docker. This
414
allows you to open interactive shells, SQL Studio, and also use the `%sql`
415
magic commands from Jupysql with a SingleStoreDB server running in a container.
416
An example of starting SingleStoreDB in Docker is shown below.
417
.. sourcecode:: python
418
from singlestoredb.server import docker
419
s2db = docker.start()
420
with s2db.connect() as conn:
421
with conn.cursor() as cur:
422
cur.execute('SHOW DATABASES')
423
for line in cur:
424
print(line)
425
s2db.stop()
426
It is possible to use the server instance as a context manager as well.
427
This will automatically shut down the container after exiting the ``with``
428
block.
429
.. sourcecode:: python
430
from singlestoredb.server import docker
431
with docker.start() as s2db:
432
with s2db.connect() as conn:
433
with conn.cursor() as cur:
434
cur.execute('SHOW DATABASES')
435
for line in cur:
436
print(line)
437
If you do not explicitly shut down the container, it will get shut
438
down when the Python process exits.
439
.. currentmodule:: singlestoredb.server.docker
440
.. autosummary::
441
:toctree: generated/
442
start
443
SingleStoreDB.logs
444
SingleStoreDB.connect
445
SingleStoreDB.connect_kai
446
SingleStoreDB.connection_url
447
SingleStoreDB.http_connection_url
448
SingleStoreDB.kai_url
449
SingleStoreDB.studio_url
450
SingleStoreDB.open_studio
451
SingleStoreDB.open_shell
452
SingleStoreDB.open_mongosh
453
SingleStoreDB.stop
454
Cloud Free Tier
455
...............
456
The Cloud Free Tier interface works much in the same way as the Docker
457
interface, although it doesn't support as many features. To start a
458
server in the Cloud Free Tier, you use the following:
459
.. sourcecode:: python
460
from singlestoredb.server import free_tier
461
s2db = free_tier.start()
462
with s2db.connect() as conn:
463
with conn.cursor() as cur:
464
cur.execute('SHOW DATABASES')
465
for line in cur:
466
print(line)
467
s2db.stop()
468
Just as with the Docker interface, you can also use a Python
469
context manager to automatically shut down your connection.
470
.. currentmodule:: singlestoredb.server.free_tier
471
.. autosummary::
472
:toctree: generated/
473
start
474
SingleStoreDB.connect
475
SingleStoreDB.connection_url
476
SingleStoreDB.http_connection_url
477
SingleStoreDB.kai_url
478
SingleStoreDB.open_shell
479
SingleStoreDB.open_mongosh
480
SingleStoreDB.stop
481
Configuration
482
-------------
483
The following functions are used to get and set package configuration settings.
484
Execute the :func:`describe_option` function with no parameters to
485
see the documentation for all options.
486
.. currentmodule:: singlestoredb
487
.. autosummary::
488
:toctree: generated/
489
get_option
490
set_option
491
describe_option
492
In addition to the function above, you can access options through the
493
``singlestoredb.options`` object. This gives you attribute-like access to the option
494
values.
495
.. ipython:: python
496
import singlestoredb as s2
497
s2.describe_option('local_infile')
498
s2.options.local_infile
499
s2.options.local_infile = True
500
s2.describe_option('local_infile')
501
.. ipython:: python
502
:suppress:
503
s2.options.local_infile = False
504
505