ncw157
    • Create new note
    • Create a note from template
      • Sharing URL Link copied
      • /edit
      • View mode
        • Edit mode
        • View mode
        • Book mode
        • Slide mode
        Edit mode View mode Book mode Slide mode
      • Customize slides
      • Note Permission
      • Read
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Write
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Engagement control Commenting, Suggest edit, Emoji Reply
    • Invite by email
      Invitee

      This note has no invitees

    • Publish Note

      Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

      Your note will be visible on your profile and discoverable by anyone.
      Your note is now live.
      This note is visible on your profile and discoverable online.
      Everyone on the web can find and read all notes of this public team.
      See published notes
      Unpublish note
      Please check the box to agree to the Community Guidelines.
      View profile
    • Commenting
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
      • Everyone
    • Suggest edit
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
    • Emoji Reply
    • Enable
    • Versions and GitHub Sync
    • Note settings
    • Note Insights New
    • Engagement control
    • Make a copy
    • Transfer ownership
    • Delete this note
    • Save as template
    • Insert from template
    • Import from
      • Dropbox
      • Google Drive
      • Gist
      • Clipboard
    • Export to
      • Dropbox
      • Google Drive
      • Gist
    • Download
      • Markdown
      • HTML
      • Raw HTML
Menu Note settings Note Insights Versions and GitHub Sync Sharing URL Create Help
Create Create new note Create a note from template
Menu
Options
Engagement control Make a copy Transfer ownership Delete this note
Import from
Dropbox Google Drive Gist Clipboard
Export to
Dropbox Google Drive Gist
Download
Markdown HTML Raw HTML
Back
Sharing URL Link copied
/edit
View mode
  • Edit mode
  • View mode
  • Book mode
  • Slide mode
Edit mode View mode Book mode Slide mode
Customize slides
Note Permission
Read
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Write
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Engagement control Commenting, Suggest edit, Emoji Reply
  • Invite by email
    Invitee

    This note has no invitees

  • Publish Note

    Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

    Your note will be visible on your profile and discoverable by anyone.
    Your note is now live.
    This note is visible on your profile and discoverable online.
    Everyone on the web can find and read all notes of this public team.
    See published notes
    Unpublish note
    Please check the box to agree to the Community Guidelines.
    View profile
    Engagement control
    Commenting
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    • Everyone
    Suggest edit
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    Emoji Reply
    Enable
    Import from Dropbox Google Drive Gist Clipboard
       Owned this note    Owned this note      
    Published Linked with GitHub
    • Any changes
      Be notified of any changes
    • Mention me
      Be notified of mention me
    • Unsubscribe
    # Data Warehouse Practical Charlie Witty & Brad Read The following practical will get you to develop a simple data warehouse, adopting the star schema, within PyCharm using Python and SQLite, two languages you are familiar with. The first part of the practical will guide you though creating dimension tables and will lead onto you creating a fact table, the core of the data warehouse. Before we get into any of the code, here is a reminder of what some of the terminology used in this practical means: - Data Warehouse <br>A term used to define a collection of data that helps analysts to make informed decisions in an organisation, often via the use of an OnLine Analytical Processing tools (OLAP). These allow us to analyse data in a multi-dimensional space. - Star Schema <br>In the Star Schema, the center of the star can have one fact table and a number of associated dimension tables. It is known as star schema as its structure resembles a star. The star schema is the simplest type of Data Warehouse schema. - Dimensions <br>Dimensions (thus the relational dimension tables) provide context to the facts. They are very important to the understandability of the data warehouse. Without dimensions, it would not be possible to understand the measures provided by the fact table because all labels and other descriptive information is sourced from the dimension table - Dimension Table <br>Dimension tables contain descriptive attributes (or fields) that are usually textual fields (or discrete numbers that behave like text); these attributes are designed to serve to be used as query constraining. - Fact Table <br>The fact table contains business facts (or measures), and foreign keys which refer to candidate keys (normally primary keys) in the dimension tables. # Scenario You are part of the UK's national school Computer Science Outreach organisation. The organisation's current information storage system is a collection of different tables that house basic information such as school details, staff details and Outreach session types. The organisation wants you to create a fact table that combines the data from all their already existing tables (dimension tables) with some invoice data (already supplied to you). **Note**: This practical is quite different to how data warehouses are created in industry. Normally the data comes from all different sources that get updated continuously and not just from premade CSV files. The aim of this practical, however, is not to simulate a real life data warehouse but to help you understand the benefits of them and their variations. --- ## Creating mock Dimension Tables in Python using SQLite Outline: - Import the SQLite library into Python. - Use SQLite commands in Python to create a database, dimension tables and a fact table. - Use SQLite commands to query the data warehouse and print the results of a query. Clone/fork this repo into you portfolio space and ensure that you have the following files: ``` csc1033-data-warehouse-practical ├── Date_Dimension.py ├── Fact_Table.py ├── Main.py ├── ParseCSV.py ├── README.md ├── School_Dimension.py ├── Session_Dimension.py ├── Staff_Dimension.py └── resources ├── Date_Data.csv ├── EduBase_Schools_July_2017_Data.csv ├── Fact_Data.csv ├── Session_Data.csv └── Staff_Data.csv ``` As you can see we have provided you with the structure for this data warehouse as well as some of the boilerplate code you will need. To increase the realism of this scenario, we have provided you with completed dimension tables that house historical data which organisation's often archive and therefore, will often already be provided. The following practical will show you how to program dimension table and will task you with designing your own fact table. # Creating Dimension Tables For our data warehouse, we will need a number of dimension tables to describe the dimensions keys, values and attributes for this Outreach scenario. For example, the school dimension will contain the details/attributes of all the schools the organisation has worked with since it started. Before we create any dimension tables, we need to make sure that we have a data source from which we are able to import the content for our tables. Luckily for you, we have provided you with multiple different `.csv` files with a range of the data for each dimension table you will need to create. For the following example we will take you through the creation of the staff dimension table. The first step is to select your data source by importing it into the the `Staff_Dimension.py` file. We do this by having a constant variable called `DATASET_FILE` and pointing it towards the appropriate `.csv` file found in the resources directory. At the same time, we have created a constant variable for the name of the table we are creating as this value will never change. ```python import ParseCSV DATASET_FILE = "resources/Staff_Data.csv" TABLE_NAME = "StaffDim" ``` You may have also noticed that we are importing a file called `ParseCSV`. This will be used later in the code in order to parse the titles and columns from the `.csv` files containing the Outreach data. The first method tidies up the data by returning a parsed list of fields given a single CSV row i.e. it removes all `'` and `\n (new lines)` from fields. The second method returns a schema list when given a CSV title row and replaces all spaces with `_` removes all `'` and`\n (new lines)` as well as making all fields lowercase. ```python def clean_row(row): return row.strip().replace("'", "").replace("\n", "").split(",") def clean_title(title): return title.strip().replace("'", "").replace("\n", "").replace(" ", "_").lower().split(",") ``` ## Create Statement Now that we have access to the data source and we have written a parser to clean up our data, we can start creating the actual dimension table. Here we want to define a new method called `create_table` in which we will pass a cursor object. As you may recall, cursor allows Python code to execute PostgreSQL commands in a database session. Cursors are created by the connection.cursor() method: they are bound to the connection for the entire lifetime and all the commands are executed in the context of the database session wrapped by the connection (see Main class for cursor and database initialisation). We now need to read in data from the previously defined `.csv`, which in this case is `Staff_Data.csv`. We achieve this by defining a variable `file` which is used to open the `.csv` and make sure that it is only read in. If the file doesn't exist, this will also cause an exception to be thrown. We then use the built-in readline function to read in the data. Notice how this is passed through our parser for tidying up the rows titles and stored in a variable called `schema`. ```python def create_table(cursor): file = open(DATASET_FILE, "r") schema = ParseCSV.clean_title(file.readline()) ``` Now that we are able to read in the relevent data from the `Staff_Data.csv` we need to create a table using SQL commands, so that the data can be stored appropriatley in a table. Firstly, create a new variable which will be called when you want to create the table and then we add in f-strings for readability. f-strings `f""" sample text """` are used as a readable way of formatting strings. We then need to use the `CREATE TABLE` command to create a new table using our constant variable. Then in the brackets we choose which columns from the `.csv` file we want to add to the table. In this case `schema[0]` is saying we want to add a formatted version of the first title in `Staff_Data.csv` which is the `staff_key`. For each of these values we will also need to define their types e.g INTEGER, TEXT, SMALL INT, etc. Finally we use the `cursor.execute` command which executes these SQL statements on the SQLite database. ```python create_table_statement = f""" CREATE TABLE {TABLE_NAME} ( {schema[0]} INTEGER PRIMARY KEY, {schema[1]} TEXT, {schema[2]} TEXT, {schema[3]} TEXT, {schema[4]} TEXT, {schema[5]} TEXT, {schema[6]} TEXT ) """ cursor.execute(create_table_statement) ``` ## Reading in Row Data Now that we have created the table and have read in the column titles, we need to populate the table with the row data. Once again we will use the readline function and check whether each row in the `.csv` is not null or an empty value **Note**: the first use of file.readline() is used to escape the column title schema. If this is true then we define a new SQL statement which inserts the data from the `.csv` into the staff table and repeats this process for each row in the file `for _ in range(len(schema))`. We then execute the insertion statement as well as the parsed data which has been read in. ```python line = file.readline() while line is not None and line != "": insert_statement = f"INSERT INTO {TABLE_NAME} (" + ','.join(schema) + ") VALUES (" + ",".join( ["?" for _ in range(len(schema))]) + ")" fields = ParseCSV.clean_row(line) cursor.execute(insert_statement, fields) line = file.readline() ``` That's it for this dimension table, luckily for us all of the other dimension tabls follow almost the exact same styling and is coded in the same way. As such the above steps/ code can be reused for the other tables. # Creating a Main In order to actually create the database, create the tables and populate them with data withn Pycharm, we need to have a main class. Firstly, we need to import sqlite3 for our database as well as the dimension tables which need to be created. ```python import sqlite3 import Date_Dimension import Fact_Table import School_Dimension import Session_Dimension import Staff_Dimension ``` Once we have our imports, we need to create a database connection and then a cursor which can be used for the given connection; in this case we are calling the database "DataWarehouse". ```python def main(): db_name = "DataWarehouse" connection = sqlite3.connect(db_name + ".db") cursor = connection.cursor() ``` Now we want to run a create_table command for each of the dimension tables we want to populate, making sure to pass through the cursor connection. This makes sure that the different dimension tables are connected to the database we have set up. We then run `connection.commit()` which sends a COMMIT statement to the sqlite database, committing the current transaction. Since by default Python does not autocommit, it is important to call this method after every transaction that modifies data for tables. The fact table is then created and committed in the same manner. Finally we close the cursor object otherwise Python will raise a ProgrammingError exception if any further operation is attempted on a closed cursor. **Note**: that the `Fact_Table.create_fact_table(cursor)` line is commented out, this is because you will implemented it in the next section. ```python Date_Dimension.create_table(cursor) School_Dimension.create_table(cursor) Session_Dimension.create_table(cursor) Staff_Dimension.create_table(cursor) # Create dimension tables connection.commit() # Fact_Table.create_fact_table(cursor) connection.commit() connection.close() if __name__ == "__main__": main() ``` # Task: Create an Invoice Fact Table Creating a fact table is the easy part as all the data and the schema is already there for you, you just need to decide what data to measure in the fact table. In our scenario, the Outreach organisation wants to have some invoice data such as a partiular session's price and rating. This data is provided to you in the `Fact_Table.csv`. You task here is to create and populate the fact table using the same methods used in the dimension table `.py` files. Remember that you need to create the fact table slightly differently by using [foreign keys](https://www.w3schools.com/sql/sql_foreignkey.asp). These foreign keys are what is going to make querying easy. From a literal standpoint, these foreign keys may make the fact table look undescriptive and not helpful but this is actually the design choice. Fact tables are designed to a low level of uniform detail (referred to as "granularity" or "grain") with the aim of keeping data storage low and making queries as simple as possible. Read, parse and insert the data from the `Fact_Table.csv` file into a database table. # Task: Queries This is where we will see the usefulness of fact tables. Perform the following quieries on the database using the fact table: - Retrieve the first name, last name and workplace of staff that ran a session with that had a rating that was above 3. `SELECT staff.first_name, staff.last_name, staff.workplace FROM StaffDim staff WHERE staff.staff_key IN (SELECT fact.staff_key FROM FactTable fact WHERE fact.rating > 3);` - Retrieve the session type and recipt code of all sessions that were priceed over £100. `SELECT fact.receipt_code, sess.theme FROM FactTable fact, SessionsDim sess WHERE fact.price > 100 AND fact.session_key = sess.session_key` - Retrieve the name of schools who have had more than 1 session. `SELECT DISTINCT establishment_name FROM SchoolDim WHERE school_key IN (SELECT school_key FROM FactTable GROUP BY school_key HAVING count(school_key) > 1)` - Retrieve the date, rating, session theme, recommended class size and the price of sessions which were less than £100 and had a rating lower than 3. `SELECT d.day, d.month, d.year, f.rating, s.theme, s.recommended_class_size, f.price FROM FactTable f, DateDim d, SessionsDim s WHERE f.price < 100 AND f.rating < 3 AND f.date_key = d.date_key AND f.session_key = s.session_key` If yoy used joins in your quries, one of the benefits you may have found is that star schema join-logic is generally simpler than the join logic required to retrieve data from a highly normalized transactional schema.<br>The star schema does also make the above queries easy using `WHERE` clauses. Imagine trying to perform these queries if, for example, the invoice data was included in the session type table. The queries would have been much more complex to write as well as taking a substantial performance hit.

    Import from clipboard

    Paste your markdown or webpage here...

    Advanced permission required

    Your current role can only read. Ask the system administrator to acquire write and comment permission.

    This team is disabled

    Sorry, this team is disabled. You can't edit this note.

    This note is locked

    Sorry, only owner can edit this note.

    Reach the limit

    Sorry, you've reached the max length this note can be.
    Please reduce the content or divide it to more notes, thank you!

    Import from Gist

    Import from Snippet

    or

    Export to Snippet

    Are you sure?

    Do you really want to delete this note?
    All users will lose their connection.

    Create a note from template

    Create a note from template

    Oops...
    This template has been removed or transferred.
    Upgrade
    All
    • All
    • Team
    No template.

    Create a template

    Upgrade

    Delete template

    Do you really want to delete this template?
    Turn this template into a regular note and keep its content, versions, and comments.

    This page need refresh

    You have an incompatible client version.
    Refresh to update.
    New version available!
    See releases notes here
    Refresh to enjoy new features.
    Your user state has changed.
    Refresh to load new user state.

    Sign in

    Forgot password

    or

    By clicking below, you agree to our terms of service.

    Sign in via Facebook Sign in via Twitter Sign in via GitHub Sign in via Dropbox Sign in with Wallet
    Wallet ( )
    Connect another wallet

    New to HackMD? Sign up

    Help

    • English
    • 中文
    • Français
    • Deutsch
    • 日本語
    • Español
    • Català
    • Ελληνικά
    • Português
    • italiano
    • Türkçe
    • Русский
    • Nederlands
    • hrvatski jezik
    • język polski
    • Українська
    • हिन्दी
    • svenska
    • Esperanto
    • dansk

    Documents

    Help & Tutorial

    How to use Book mode

    Slide Example

    API Docs

    Edit in VSCode

    Install browser extension

    Contacts

    Feedback

    Discord

    Send us email

    Resources

    Releases

    Pricing

    Blog

    Policy

    Terms

    Privacy

    Cheatsheet

    Syntax Example Reference
    # Header Header 基本排版
    - Unordered List
    • Unordered List
    1. Ordered List
    1. Ordered List
    - [ ] Todo List
    • Todo List
    > Blockquote
    Blockquote
    **Bold font** Bold font
    *Italics font* Italics font
    ~~Strikethrough~~ Strikethrough
    19^th^ 19th
    H~2~O H2O
    ++Inserted text++ Inserted text
    ==Marked text== Marked text
    [link text](https:// "title") Link
    ![image alt](https:// "title") Image
    `Code` Code 在筆記中貼入程式碼
    ```javascript
    var i = 0;
    ```
    var i = 0;
    :smile: :smile: Emoji list
    {%youtube youtube_id %} Externals
    $L^aT_eX$ LaTeX
    :::info
    This is a alert area.
    :::

    This is a alert area.

    Versions and GitHub Sync
    Get Full History Access

    • Edit version name
    • Delete

    revision author avatar     named on  

    More Less

    Note content is identical to the latest version.
    Compare
      Choose a version
      No search result
      Version not found
    Sign in to link this note to GitHub
    Learn more
    This note is not linked with GitHub
     

    Feedback

    Submission failed, please try again

    Thanks for your support.

    On a scale of 0-10, how likely is it that you would recommend HackMD to your friends, family or business associates?

    Please give us some advice and help us improve HackMD.

     

    Thanks for your feedback

    Remove version name

    Do you want to remove this version name and description?

    Transfer ownership

    Transfer to
      Warning: is a public team. If you transfer note to this team, everyone on the web can find and read this note.

        Link with GitHub

        Please authorize HackMD on GitHub
        • Please sign in to GitHub and install the HackMD app on your GitHub repo.
        • HackMD links with GitHub through a GitHub App. You can choose which repo to install our App.
        Learn more  Sign in to GitHub

        Push the note to GitHub Push to GitHub Pull a file from GitHub

          Authorize again
         

        Choose which file to push to

        Select repo
        Refresh Authorize more repos
        Select branch
        Select file
        Select branch
        Choose version(s) to push
        • Save a new version and push
        • Choose from existing versions
        Include title and tags
        Available push count

        Pull from GitHub

         
        File from GitHub
        File from HackMD

        GitHub Link Settings

        File linked

        Linked by
        File path
        Last synced branch
        Available push count

        Danger Zone

        Unlink
        You will no longer receive notification when GitHub file changes after unlink.

        Syncing

        Push failed

        Push successfully