DROP DATABASE IF EXISTS e700719f CASCADE;
CREATE DATABASE e700719f;
USE e700719f;

-- raw tables
CREATE TABLE IF NOT EXISTS raw_badges (id BIGINT, class INT, `date` STRING, name VARCHAR(100), tagbased BOOLEAN, userid BIGINT) row format delimited fields terminated by ',' tblproperties ("skip.header.line.count"="1");
CREATE TABLE IF NOT EXISTS raw_comments (id BIGINT, creationdate STRING, postid BIGINT, score INT, text VARCHAR(40000), userdisplayname VARCHAR(100), userid BIGINT) row format delimited fields terminated by ',' tblproperties ("skip.header.line.count"="1");
CREATE TABLE IF NOT EXISTS raw_posts (id BIGINT,acceptedanswerid BIGINT,answercount INT,body VARCHAR(1000),closeddate STRING,commentcount INT,communityowneddate STRING,creationdate STRING,favoritecount INT,lastactivitydate STRING,lasteditdate STRING,lasteditordisplayname VARCHAR(100),lasteditoruserid BIGINT,ownerdisplayname VARCHAR(100),owneruserid BIGINT,parentid BIGINT,posttypeid TINYINT,score INT,tags VARCHAR(200),title VARCHAR(200),viewcount INT) row format delimited fields terminated by ',' tblproperties ("skip.header.line.count"="1");
CREATE TABLE IF NOT EXISTS raw_postlinks (id BIGINT,creationdate STRING,linktypeid BIGINT,postid BIGINT,relatedpostid BIGINT) row format delimited fields terminated by ',' tblproperties ("skip.header.line.count"="1");
CREATE TABLE IF NOT EXISTS raw_users (id BIGINT,aboutme VARCHAR(3000),accountid BIGINT,creationdate INT,displayname VARCHAR(100),downvotes INT,lastaccessdate STRING,location VARCHAR(100),profileimageurl VARCHAR(500),reputation INT,upvotes INT,views INT,websiteurl VARCHAR(500)) row format delimited fields terminated by ',' tblproperties ("skip.header.line.count"="1");
CREATE TABLE IF NOT EXISTS raw_votes(id BIGINT,bountyamount INT ,creationdate STRING,postid BIGINT,userid BIGINT,votetypeid BIGINT) row format delimited fields terminated by ',' tblproperties ("skip.header.line.count"="1");

LOAD DATA LOCAL INPATH '/home/adbs/2019S/shared/hive/badges.csv' OVERWRITE INTO TABLE raw_badges;
LOAD DATA LOCAL INPATH '/home/adbs/2019S/shared/hive/comments.csv' OVERWRITE INTO TABLE raw_comments;
LOAD DATA LOCAL INPATH '/home/adbs/2019S/shared/hive/postlinks.csv' OVERWRITE INTO TABLE raw_postlinks;
LOAD DATA LOCAL INPATH '/home/adbs/2019S/shared/hive/posts.csv' OVERWRITE INTO TABLE raw_posts;
LOAD DATA LOCAL INPATH '/home/adbs/2019S/shared/hive/users.csv' OVERWRITE INTO TABLE raw_users;
LOAD DATA LOCAL INPATH '/home/adbs/2019S/shared/hive/votes.csv' OVERWRITE INTO TABLE raw_votes;

-- real tables
CREATE TABLE IF NOT EXISTS badges (id BIGINT, class INT, `date` TIMESTAMP, name VARCHAR(100), tagbased BOOLEAN, userid BIGINT) row format delimited fields terminated by ',';
CREATE TABLE IF NOT EXISTS comments (id BIGINT, creationdate TIMESTAMP, postid BIGINT, score INT, text VARCHAR(40000), userdisplayname VARCHAR(100), userid BIGINT) row format delimited fields terminated by ',';
CREATE TABLE IF NOT EXISTS postlinks (id BIGINT,creationdate TIMESTAMP,linktypeid BIGINT,postid BIGINT,relatedpostid BIGINT) row format delimited fields terminated by ',';
CREATE TABLE IF NOT EXISTS posts (id BIGINT,acceptedanswerid BIGINT,answercount INT,body VARCHAR(1000),closeddate TIMESTAMP,commentcount INT,communityowneddate TIMESTAMP,creationdate TIMESTAMP,favoritecount INT,lastactivitydate TIMESTAMP,lasteditdate TIMESTAMP,lasteditordisplayname VARCHAR(100),lasteditoruserid BIGINT,ownerdisplayname VARCHAR(100),owneruserid BIGINT,parentid BIGINT,posttypeid TINYINT,score INT,tags VARCHAR(200),title VARCHAR(200),viewcount INT) row format delimited fields terminated by ',';
CREATE TABLE IF NOT EXISTS users (id BIGINT,aboutme VARCHAR(3000),accountid BIGINT,creationdate INT,displayname VARCHAR(100),downvotes INT,lastaccessdate TIMESTAMP,location VARCHAR(100),profileimageurl VARCHAR(500),reputation INT,upvotes INT,views INT,websiteurl VARCHAR(500)) row format delimited fields terminated by ',';
CREATE TABLE IF NOT EXISTS votes(id BIGINT,bountyamount INT ,creationdate TIMESTAMP,postid BIGINT,userid BIGINT,votetypeid BIGINT) row format delimited fields terminated by ',';

INSERT OVERWRITE TABLE badges 
	SELECT id,class,date_format(regexp_replace(`date`, 'T', ' '),'yyyy-MM-dd HH:mm:ss.SSS'),name,tagbased,userid FROM raw_badges;
INSERT OVERWRITE TABLE comments 
	SELECT id,date_format(regexp_replace(creationdate, 'T', ' '),'yyyy-MM-dd HH:mm:ss.SSS'),postid,score,text,userdisplayname,userid FROM raw_comments;
INSERT OVERWRITE TABLE postlinks 
	SELECT id,date_format(regexp_replace(creationdate, 'T', ' '),'yyyy-MM-dd HH:mm:ss.SSS'),linktypeid,postid,relatedpostid FROM raw_postlinks;
INSERT OVERWRITE TABLE posts 
	SELECT id,acceptedanswerid,answercount,body,
		date_format(regexp_replace(closeddate, 'T', ' '),'yyyy-MM-dd HH:mm:ss.SSS'),commentcount,
		date_format(regexp_replace(communityowneddate, 'T', ' '),'yyyy-MM-dd HH:mm:ss.SSS'),
		date_format(regexp_replace(creationdate, 'T', ' '),'yyyy-MM-dd HH:mm:ss.SSS'),favoritecount,
		date_format(regexp_replace(lastactivitydate, 'T', ' '),'yyyy-MM-dd HH:mm:ss.SSS'),
		date_format(regexp_replace(lasteditdate, 'T', ' '),'yyyy-MM-dd HH:mm:ss.SSS'),
		lasteditordisplayname,lasteditoruserid,ownerdisplayname,owneruserid,parentid,posttypeid,score,tags,title,viewcount FROM raw_posts;
INSERT OVERWRITE TABLE users 
	SELECT id,aboutme,accountid,date_format(regexp_replace(creationdate, 'T', ' '),'yyyy-MM-dd HH:mm:ss.SSS'),
		displayname,downvotes,date_format(regexp_replace(lastaccessdate, 'T', ' '),'yyyy-MM-dd HH:mm:ss.SSS'),
		location,profileimageurl,reputation,upvotes,views,websiteurl FROM raw_users;
INSERT OVERWRITE TABLE votes 
	SELECT id,bountyamount,date_format(regexp_replace(creationdate, 'T', ' '),'yyyy-MM-dd HH:mm:ss.SSS'),postid,userid,votetypeid FROM raw_votes;
