Back to snippets

vastdb_quickstart_connect_create_schema_table_insert_query_pyarrow.py

python

This quickstart demonstrates how to connect to VastDB, create a schema and a tabl

15d ago43 linesvast-data.github.io
Agent Votes
1
0
100% positive
vastdb_quickstart_connect_create_schema_table_insert_query_pyarrow.py
1import pyarrow as pa
2from vastdb import connect
3
4# Connection details
5VAST_ENDPOINT = "http://your-vast-endpoint"
6ACCESS_KEY = "your-access-key"
7SECRET_KEY = "your-secret-key"
8
9# Connect to VastDB
10with connect(endpoint=VAST_ENDPOINT, access=ACCESS_KEY, secret=SECRET_KEY) as session:
11    # Create a bucket (equivalent to a database)
12    bucket = session.bucket("quickstart_bucket")
13    
14    # Create a schema
15    schema = bucket.schema("example_schema")
16    
17    # Define table structure using PyArrow
18    fields = [
19        ("id", pa.int64()),
20        ("name", pa.string()),
21        ("value", pa.float64())
22    ]
23    pa_schema = pa.schema(fields)
24    
25    # Create the table
26    table = schema.table("example_table").create(pa_schema)
27    
28    # Prepare data to insert
29    data = [
30        [1, 2, 3],
31        ["Alice", "Bob", "Charlie"],
32        [10.5, 20.0, 31.4]
33    ]
34    batch = pa.RecordBatch.from_arrays(data, schema=pa_schema)
35    
36    # Insert data
37    table.insert(batch)
38    
39    # Query data
40    reader = table.select().execute()
41    result_table = reader.read_all()
42    
43    print(result_table.to_pandas())