|
| 1 | +# User-defined types (UDTs) in Amazon Keyspaces |
| 2 | + |
| 3 | +Amazon Keyspaces (for Apache Cassandra) allows the use of user-defined types (UDTs) to optimize data organization and enhance data modeling capabilities. |
| 4 | + |
| 5 | +A user-defined type (UDT) is a grouping of fields and data types that you can use to define a single column in Amazon Keyspaces. |
| 6 | +Valid data types for UDTs are all supported Cassandra data types, including collections and other UDTs that you've already created in the same keyspace. |
| 7 | + |
| 8 | +For more information about supported Keyspaces data types, see [Cassandra data type support](https://docs.aws.amazon.com/keyspaces/latest/devguide/cassandra-apis.html#cassandra-data-type) . |
| 9 | + |
| 10 | + |
| 11 | +# Amazon Keyspaces Real Estate Schema Example |
| 12 | + |
| 13 | +This example demonstrates a comprehensive Real Estate data model using Amazon Keyspaces with User Defined Types (UDTs). The schema showcases how to structure complex real estate data including property details, market analytics, and location-based queries. |
| 14 | + |
| 15 | + |
| 16 | +## Schema Overview |
| 17 | +The real estate schema consists of three main tables and seven UDTs that model comprehensive property information: |
| 18 | + |
| 19 | +### User Defined Types (UDTs) |
| 20 | + |
| 21 | +- **address_details**: Complete address information including coordinates and neighborhood data |
| 22 | +- **property_specifications**: Physical property details (size, rooms, construction details) |
| 23 | +- **property_amenities**: Features and amenities (pool, security, smart home features) |
| 24 | +- **financial_details**: Pricing, taxes, HOA fees, and financing information |
| 25 | +- **location_quality**: School districts, walkability scores, and area ratings |
| 26 | +- **market_intelligence**: Market trends, days on market, and pricing analytics |
| 27 | +- **listing_details**: Agent information, listing status, and marketing materials |
| 28 | + |
| 29 | +### Tables |
| 30 | + |
| 31 | +1. **properties**: Main property table with complete property information |
| 32 | +2. **properties_by_location**: Optimized for geographic and price range queries |
| 33 | +3. **market_analytics**: Time-series market data for trend analysis |
| 34 | + |
| 35 | +## Sample Data |
| 36 | + |
| 37 | +The schema includes sample data for the Seattle/Bellevue area: |
| 38 | + |
| 39 | +- **Luxury Properties**: High-end homes and condos ($1M+) |
| 40 | +- **Mid-Range Properties**: Family homes and investment properties ($500K-$1M) |
| 41 | +- **Market Analytics**: Historical market data with trends and statistics |
| 42 | +- **Location Data**: Properties organized by ZIP code and property type |
| 43 | + |
| 44 | +## Quick Start |
| 45 | + |
| 46 | +### Prerequisites |
| 47 | + |
| 48 | +- Amazon Keyspaces access or local Cassandra installation |
| 49 | +- cqlsh or cqlsh-expansion for Amazon Keyspaces |
| 50 | + |
| 51 | +### Setup |
| 52 | + |
| 53 | +1. **Execute the setup script:** |
| 54 | + ```bash |
| 55 | + cd real_eastate_schema_sample |
| 56 | + chmod +x execute_real_estate_schema.sh |
| 57 | + ./execute_real_estate_schema.sh |
| 58 | + ``` |
| 59 | + |
| 60 | +2. **Manual setup (alternative):** |
| 61 | + ```bash |
| 62 | + # Create keyspace |
| 63 | + cqlsh -e "CREATE KEYSPACE IF NOT EXISTS real_estate WITH REPLICATION = {'class': 'SingleRegionStrategy'};" |
| 64 | + |
| 65 | + # Execute files in order |
| 66 | + cqlsh -f 01_real_estate_udts.cql |
| 67 | + cqlsh -f 02_properties_table.cql |
| 68 | + cqlsh -f 03_sample_luxury_properties.cql |
| 69 | + cqlsh -f 04_sample_midrange_properties.cql |
| 70 | + cqlsh -f 05_sample_properties_by_location.cql |
| 71 | + cqlsh -f 06_sample_market_analytics.cql |
| 72 | + ``` |
| 73 | + |
| 74 | + |
| 75 | +## Sample Queries |
| 76 | + |
| 77 | +The schema supports various query patterns: |
| 78 | + |
| 79 | +### Location-Based Queries |
| 80 | +```cql |
| 81 | +-- Properties by ZIP code and type |
| 82 | +SELECT property_id, address.street_name, financial.listing_price |
| 83 | +FROM properties_by_location |
| 84 | +WHERE zip_code = '98101' |
| 85 | +AND property_type = 'condo'; |
| 86 | +``` |
| 87 | + |
| 88 | +### Market Analytics |
| 89 | +```cql |
| 90 | +-- Market trends over time |
| 91 | +SELECT area_code, date, avg_price, median_price, |
| 92 | + market_stats.market_temperature |
| 93 | +FROM market_analytics |
| 94 | +WHERE area_code = '98101' |
| 95 | +AND property_type = 'condo'; |
| 96 | +``` |
| 97 | + |
| 98 | +## Schema Features |
| 99 | + |
| 100 | +### Complex Data Modeling |
| 101 | +- **Nested UDTs**: Rich data structures for comprehensive property information |
| 102 | +- **Collections**: Lists and maps for amenities, school ratings, and features |
| 103 | +- **Flexible Schema**: Easy to extend with additional property attributes |
| 104 | + |
| 105 | +### Query Optimization |
| 106 | +- **Partition Keys**: Efficient data distribution by location and property type |
| 107 | +- **Clustering Keys**: Ordered data for range queries and time-series analysis |
| 108 | + |
| 109 | +### Real Estate Use Cases |
| 110 | +- **Property Listings**: Complete MLS-style property information |
| 111 | +- **Market Analysis**: Historical trends and comparative market analysis |
| 112 | +- **Location Intelligence**: School districts, walkability, and neighborhood data |
| 113 | +- **Investment Analysis**: Financial metrics and market performance |
| 114 | + |
| 115 | +## File Structure |
| 116 | + |
| 117 | +``` |
| 118 | +real_eastate_schema_sample/ |
| 119 | +├── 01_real_estate_udts.cql # UDT definitions |
| 120 | +├── 02_properties_table.cql # Table schemas |
| 121 | +├── 03_sample_luxury_properties.cql # High-end property samples |
| 122 | +├── 04_sample_midrange_properties.cql # Mid-range property samples |
| 123 | +├── 05_sample_properties_by_location.cql # Location-based data |
| 124 | +├── 06_sample_market_analytics.cql # Market trend data |
| 125 | +├── 07_sample_queries.cql # Example queries |
| 126 | +└── execute_real_estate_schema.sh # Setup script |
| 127 | +``` |
| 128 | + |
| 129 | +## Data Model Benefits |
| 130 | + |
| 131 | +### Structured Data |
| 132 | +- **Type Safety**: UDTs provide schema validation |
| 133 | +- **Data Integrity**: Consistent structure across all properties |
| 134 | +- **Rich Metadata**: Comprehensive property and market information |
| 135 | + |
| 136 | +### Flexibility |
| 137 | +- **Extensible**: Easy to add new UDT fields |
| 138 | +- **Backward Compatible**: Schema evolution without breaking changes |
| 139 | +- **Multi-Use**: Supports various real estate applications |
| 140 | + |
| 141 | +--- |
| 142 | +## Amazon Keyspaces Considerations |
| 143 | +When using with Amazon Keyspaces: |
| 144 | +- Use point-in-time recovery for data protection |
| 145 | +- Consider on-demand billing for variable workloads |
| 146 | +- Implement proper IAM policies for data access |
| 147 | + |
| 148 | +### Next Steps |
| 149 | + |
| 150 | +1. **Extend the Schema**: Add more UDTs for additional property types |
| 151 | +2. **Implement Applications**: Build real estate applications using this schema |
| 152 | +3. **Monitor Performance**: Use CloudWatch metrics to optimize queries |
0 commit comments