You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Once we have cloned our repository, the next thing we have to do is create our database that will hold our house sales data. There are two ways we can create our database. One way is creating IBM Db2 Warehouse on Cloud. This database will be hosted on the cloud. However, if you perfer to have your database on premise or locally, we can also use the Db2 Docker Image.
31
+
32
+
Choose which type of database you would like and follow the corresponding instructions:
33
+
34
+
1.[Create IBM Db2 Warehouse on Cloud](#2a-create-ibm-db2-warehouse-on-cloud)
35
+
2.[Create IBM Db2 Database Locally Using Docker Image](#2b-create-an-ibm-db2-on-premise-database)
36
+
37
+
#### 2a. Create IBM Db2 Warehouse on Cloud
29
38
30
39
Create the Db2 Warehouse on Cloud service and make sure to note the credentials using the following link:
31
40
32
41
*[**IBM Db2 Warehouse on Cloud**](https://cloud.ibm.com/catalog/services/db2-warehouse)
33
42
34
-
### 3. Create schema and tables
43
+
#### 2b. Create an IBM Db2 On Premise Database
44
+
45
+
Instead of creating the Db2 Warehouse on Cloud service, we can also have our database instantiated locally by using the free IBM Db2 Docker Image.
46
+
47
+
Prerequisite:
48
+
49
+
* A [Docker](https://www.docker.com) account
50
+
*[Docker Desktop](https://www.docker.com/products/docker-desktop) installed on your machine
51
+
* Logging into your Docker account on Docker Desktop
52
+
53
+
Steps to get your db2 running locally:
54
+
55
+
* Create a folder name `db2`
56
+
* Open a terminal window and make sure your current directory is the same as where your `db2` is located
Once this is done, it will create a db2 docker container with the follow customizations:
68
+
69
+
* IP Address/Domain: `localhost`
70
+
* Port: `50000`
71
+
* Database name: `homesalesdb`
72
+
* Username: `db2inst1`
73
+
* Password: `hackathon`
74
+
75
+
76
+
### 3. Create Schema and Tables
77
+
Now that we have created our databases, we need to import the data from the csv file into our database. We will be creating a schema called `DB2WML`. The two tables we will create are `HOME_SALES` and `HOME_ADDRESS`. `HOME_SALES` will store the data we retrieve from our csv file. `HOME_ADDRESS` is going to be the addresses associated with each home.
78
+
79
+
Depending on which type you have (Cloud or On-Premise), the steps will be a little different. Please follow the corresponding steps:
80
+
81
+
1.[Create Schema and Tables for IBM Db2 Warehouse on Cloud](#3a-create-schema-and-tables-for-ibm-db2-warehouse-on-cloud)
82
+
2.[Create Schema and Tables for IBM Db2 Docker Image](#3b-create-schema-and-tables-for-ibm-db2-docker-image)
83
+
84
+
85
+
#### 3a. Create Schema and Tables for IBM Db2 Warehouse on Cloud
35
86
36
87
In the Db2 warehouse resource page, click on `Manage` and go to DB2 console by clicking the button `Open Console`. In the console do the following to load your data.
37
88
38
89
* Click `Load` from the hamburger menu.
39
90
* Click `Browse files` or you can drag files, select the [data/home-sales-training-data.csv](data/home-sales-training-data.csv) and click `Next`
40
91
* Choose existing schema or create a new one named `DB2WML` by clicking `+ New Schema`
41
92
* Create a new table named `HOME_SALES` by clicking `+ New Table` on the schema you created and click `Next`
42
-
* Make sure the column names and data types displayed are correct, then cick`Next`
93
+
* Make sure the column names and data types displayed are correct, then click`Next`
43
94
* Click `Begin Load` to load the data
44
95
45
-
Once this is done it will create a table `HOME_SALES` under schema `DB2WML` which will be used by the Node.js application.
96
+
We also need to create a table for `HOME_ADDRESS`, which will store the addresses of each house data. We won't be able to use the same instructions we used for `HOME_SALES` since we have no data to load.
97
+
98
+
* Click `Run SQL` from the hamburger menu.
99
+
* Click `Blank`, which will open a blank sql editor
100
+
* Run the command
101
+
102
+
```bash
103
+
CREATE TABLE DB2WML.HOME_ADDRESS (ADDRESS1 VARCHAR(50), ADDRESS2 VARCHAR(50), CITY VARCHAR(50), STATE VARCHAR(5), ZIPCODE INTEGER, COUNTRY VARCHAR(50), HOME_ID INTEGER)
104
+
```
105
+
106
+
Once this is done it will create a table `HOME_SALES` and `HOME_ADDRESS ` under schema `DB2WML` which will be used by the Node.js application.
107
+
108
+
109
+
#### 3b. Create Schema and Tables for IBM Db2 Docker Image
110
+
111
+
Exit out of the container shell by CONTROL-C. Load the sample data into the onprem Db2 database:
Run the container and enter into the container shell:
118
+
119
+
```bash
120
+
docker exec -ti mydb2 bash -c "su - db2inst1"
121
+
```
122
+
123
+
Steps To Create Schema and Tables:
124
+
125
+
126
+
* Connect to the database `homesalesdb` NOTE: This command may not work for sometime, since the container takes some time to create the database. If this command doesn work, please wait a couple minutes and then try again.
127
+
128
+
```bash
129
+
db2 connect to homesalesdb
130
+
```
131
+
132
+
* Create Schema `DB2WML`
133
+
134
+
```bash
135
+
db2 'CREATE SCHEMA DB2WML'
136
+
```
137
+
138
+
* Create Table `HOME_SALES` and `HOME_ADDRESS` within Schema `DB2WML`
varstr2="UPDATE "+process.env.DB_SCHEMA+".HOME_ADDRESS SET ADDRESS1='"+request.body.addressInfo.address1+"',ADDRESS2='"+request.body.addressInfo.address2+"',CITY='"+request.body.addressInfo.city+"',STATE='"+request.body.addressInfo.state+"',COUNTRY='"+request.body.addressInfo.country+"' WHERE HOME_ID="+request.body.id+";";
140
+
varstr2="UPDATE "+process.env.DB_SCHEMA+".HOME_ADDRESS SET ADDRESS1='"+request.body.addressInfo.address1+"',ADDRESS2='"+request.body.addressInfo.address2+"',CITY='"+request.body.addressInfo.city+"',STATE='"+request.body.addressInfo.state+"',COUNTRY='"+request.body.addressInfo.country+"',ZIPCODE="+request.body.addressInfo.zipcode+" WHERE HOME_ID="+request.body.id+";";
141
+
142
+
varstr4="INSERT INTO "+process.env.DB_SCHEMA+".HOME_ADDRESS (ADDRESS1, ADDRESS2, CITY, STATE,ZIPCODE, COUNTRY,HOME_ID) VALUES ('"+request.body.addressInfo.address1+"', '"+request.body.addressInfo.address2+"', '"+request.body.addressInfo.city+"', '"+request.body.addressInfo.state+"', "+request.body.addressInfo.zipcode+", '"+request.body.addressInfo.country+"', "+request.body.id+");";
143
+
144
+
138
145
139
146
varstr="UPDATE "+process.env.DB_SCHEMA+".HOME_SALES SET LOTAREA="+request.body.data.lotArea+", YEARBUILT="+request.body.data.yearBuilt+", BLDGTYPE='"+request.body.data.bldgType+"',HOUSESTYLE='"+request.body.data.houseStyle+"',OVERALLCOND="+request.body.data.overallCond+",ROOFSTYLE='"+request.body.data.roofStyle+"',EXTERCOND='"+request.body.data.exterCond+"',FOUNDATION='"+request.body.data.foundation+"',BSMTCOND='"+request.body.data.bsmtCond+"',HEATING='"+request.body.data.heating+"',HEATINGQC='"+request.body.data.heatingQC+"',CENTRALAIR='"+request.body.data.centralAir+"',ELECTRICAL='"+request.body.data.electrical+"',FULLBATH="+request.body.data.fullBath+",HALFBATH="+request.body.data.halfBath+",BEDROOMABVGR="+request.body.data.bedroomAbvGr+",KITCHENABVGR="+request.body.data.kitchenAbvGr+",KITCHENQUAL='"+request.body.data.kitchenQual+"',TOTRMSABVGRD="+request.body.data.tempotRmsAbvGrd+",FIREPLACES="+request.body.data.fireplaces+",FIREPLACEQU='"+request.body.data.fireplaceQu+"',GARAGETYPE='"+request.body.data.garageType+"',GARAGEFINISH='"+request.body.data.garageFinish+"',GARAGECARS="+request.body.data.garageCars+",GARAGECOND='"+request.body.data.garageCond+"',POOLAREA="+request.body.data.poolArea+",POOLQC='"+request.body.data.poolQC+"',FENCE='"+request.body.data.fence+"',MOSOLD="+request.body.data.moSold+",YRSOLD="+request.body.data.yrSold+",SALEPRICE="+request.body.data.salePrice+" WHERE ID="+request.body.id+";";
140
147
148
+
varstr3="SELECT * FROM "+process.env.DB_SCHEMA+".HOME_ADDRESS WHERE HOME_ID="+request.body.id+";";
0 commit comments