CreateTableInstructions.constants.ts88 lines · main
| 1 | export 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 | ` |
| 21 | from pyiceberg.catalog import load_catalog |
| 22 | import pyarrow as pa |
| 23 | import datetime |
| 24 | |
| 25 | # Briven project ref |
| 26 | PROJECT_REF = "${ref ?? '<your-briven-project-ref>'}" |
| 27 | |
| 28 | # Configuration for Iceberg REST Catalog |
| 29 | WAREHOUSE = "${warehouse ?? 'your-analytics-bucket-name'}" |
| 30 | TOKEN = "${token ?? '•••••••••••••'}" |
| 31 | |
| 32 | # Configuration for S3-Compatible Storage |
| 33 | S3_ACCESS_KEY = "${s3AccessKey ?? '•••••••••••••'}" |
| 34 | S3_SECRET_KEY = "${s3SecretKey ?? '•••••••••••••'}" |
| 35 | S3_REGION = "${s3Region}" |
| 36 | S3_ENDPOINT = f"${s3Endpoint ?? 'https://{PROJECT_REF}.supabase.co/storage/v1/s3'}" |
| 37 | CATALOG_URI = f"${catalogUri ?? 'https://{PROJECT_REF}.supabase.co/storage/v1/iceberg'}" |
| 38 | |
| 39 | # Load the Iceberg catalog |
| 40 | catalog = 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 |
| 57 | print("Creating catalog 'default'...") |
| 58 | catalog.create_namespace_if_not_exists("default") |
| 59 | |
| 60 | # Define schema for your Iceberg table |
| 61 | schema = 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) |
| 68 | print("Creating table 'events'...") |
| 69 | table = catalog.create_table_if_not_exists(("default", "events"), schema=schema) |
| 70 | |
| 71 | # Generate and insert sample data |
| 72 | print("Preparing sample data to be inserted...") |
| 73 | current_time = datetime.datetime.now() |
| 74 | data = 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 |
| 81 | print("Inserting data into 'events'...") |
| 82 | table.append(data) |
| 83 | |
| 84 | print("Completed!") |
| 85 | # Scan table and print data as pandas DataFrame |
| 86 | df = table.scan().to_pandas() |
| 87 | print(df) |
| 88 | `.trim() |