CreateTableInstructions.constants.ts88 lines · main
1export const getPyicebergSnippet = ({
2 ref,
3 warehouse,
4 catalogUri,
5 s3Endpoint,
6 s3Region,
7 s3AccessKey,
8 s3SecretKey,
9 token,
10}: {
11 ref?: string
12 warehouse?: string
13 catalogUri?: string
14 s3Endpoint?: string
15 s3Region?: string
16 s3AccessKey?: string
17 s3SecretKey?: string
18 token?: string
19}) =>
20 `
21from pyiceberg.catalog import load_catalog
22import pyarrow as pa
23import datetime
24
25# Briven project ref
26PROJECT_REF = "${ref ?? '<your-briven-project-ref>'}"
27
28# Configuration for Iceberg REST Catalog
29WAREHOUSE = "${warehouse ?? 'your-analytics-bucket-name'}"
30TOKEN = "${token ?? '•••••••••••••'}"
31
32# Configuration for S3-Compatible Storage
33S3_ACCESS_KEY = "${s3AccessKey ?? '•••••••••••••'}"
34S3_SECRET_KEY = "${s3SecretKey ?? '•••••••••••••'}"
35S3_REGION = "${s3Region}"
36S3_ENDPOINT = f"${s3Endpoint ?? 'https://{PROJECT_REF}.supabase.co/storage/v1/s3'}"
37CATALOG_URI = f"${catalogUri ?? 'https://{PROJECT_REF}.supabase.co/storage/v1/iceberg'}"
38
39# Load the Iceberg catalog
40catalog = load_catalog(
41 "briven",
42 type="rest",
43 warehouse=WAREHOUSE,
44 uri=CATALOG_URI,
45 token=TOKEN,
46 **{
47 "py-io-impl": "pyiceberg.io.pyarrow.PyArrowFileIO",
48 "s3.endpoint": S3_ENDPOINT,
49 "s3.access-key-id": S3_ACCESS_KEY,
50 "s3.secret-access-key": S3_SECRET_KEY,
51 "s3.region": S3_REGION,
52 "s3.force-virtual-addressing": False,
53 },
54)
55
56# Create namespace if it doesn't exist
57print("Creating catalog 'default'...")
58catalog.create_namespace_if_not_exists("default")
59
60# Define schema for your Iceberg table
61schema = pa.schema([
62 pa.field("event_id", pa.int64()),
63 pa.field("event_name", pa.string()),
64 pa.field("event_timestamp", pa.timestamp("ms")),
65])
66
67# Create table (if it doesn't exist already)
68print("Creating table 'events'...")
69table = catalog.create_table_if_not_exists(("default", "events"), schema=schema)
70
71# Generate and insert sample data
72print("Preparing sample data to be inserted...")
73current_time = datetime.datetime.now()
74data = pa.table({
75 "event_id": [1, 2, 3],
76 "event_name": ["login", "logout", "purchase"],
77 "event_timestamp": [current_time, current_time, current_time],
78})
79
80# Append data to the Iceberg table
81print("Inserting data into 'events'...")
82table.append(data)
83
84print("Completed!")
85# Scan table and print data as pandas DataFrame
86df = table.scan().to_pandas()
87print(df)
88`.trim()