This is a project to deal with big data by using appropriate indexes in the database & dynamic queries.
It is a console project implementing functions in OTT(over-the-top) services.
It uses large amount of dummy data created instead of real data used in current OTT services.๋์ฉ๋ ๋ฐ์ดํฐ ์ฒ๋ฆฌ๋ฅผ ์ํ DB ๋ฐ Transaction์ ์ค๊ณ ๋ฐ ๊ตฌํํ ํ๋ก์ ํธ์ ๋๋ค.
์ฝ์ ํ๋ก๊ทธ๋จ์ด๋ฉฐ, ๊ธฐ๋ณธ์ ์ธ OTT ์๋น์ค๋ฅผ ์ํ ๊ธฐ๋ฅ๋ค์ ํฌํจํ๊ณ ์์ต๋๋ค.
์๋น์ค ๊ธฐ๋ฅ ๊ตฌํ์ ์ํด ๋์ฉ๋ ๋๋ฏธ ๋ฐ์ดํฐ๋ฅผ ๋ง๋ค์ด ์ฌ์ฉํ์์ต๋๋ค.
- To deal with big data (ex. over 1000000 data) we need to think of a way to effectively process them in the database
- This could be done by using appropriate indexs in relational databases
- Login/ Register (๋ก๊ทธ์ธ/ํ์๊ฐ์ )
- Buy tickets (์ด์ฉ๊ถ ๊ฒฐ์ )
- Multiple profiles per user (ํ ์ฌ์ฉ์์๊ฒ ์ฌ๋ฌ๊ฐ์ ํ๋กํ ํ ๋น)
- Show all contents to user (๋ชจ๋ ์ปจํ ์ธ ๋ณด์ฌ์ฃผ๊ธฐ)
- Show contents by genre (์ฅ๋ฅด๋ณ ์ปจํ ์ธ ๋ณด์ฌ์ฃผ๊ธฐ)
- Search contents / See user search history (์ปจํ ์ธ ๊ฒ์ ๋ฐ ์ฌ์ฉ์ ๊ฒ์๊ธฐ๋ก ๋ณด๊ธฐ)
- View content specific information (์ปจํ ์ธ ์์ธ ์ ๋ณด ๋ณด๊ธฐ)
- Rate content (์ปจํ ์ธ ๋ณ์ ์ฃผ๊ธฐ)
- Add content to my favorites (๋ด ์ฐ ๋ชฉ๋ก์ ์ปจํ ์ธ ์ถ๊ฐ)
To begin using this template, choose one of the following options to get started:
- Fork, Clone, or Download on GitHub
- Open the project through Intellij IDEA
- Run "Main.java" to start using the program
To set up the database with pre-defined SQL statements, please refer to the file below:
- Java Version 11
- MySQL Version 8.0.23
- MySQLWorkBench Version 8.0.20
โโโ dao
โ โโโ ContentCommentDao.java
โ โโโ ContentDao.java
โ โโโ MyContentDao.java
โ โโโ OrdersDao.java
โ โโโ RatingDao.java
โ โโโ SearchHistoryDao.java
โ โโโ TicketDao.java
โ โโโ UserDao.java
โ โโโ UserProfileDao.java
โโโ entity
โ โโโ ContentComment.java
โ โโโ Content.java
โ โโโ LoggedInUser.java
โ โโโ MyContent.java
โ โโโ Orders.java
โ โโโ Rating.java
โ โโโ SearchHistory.java
โ โโโ Ticket.java
โ โโโ User.java
โ โโโ UserProfile.java
โโโ enums
โ โโโ ContentGenre.java
โ โโโ ContentType.java
โ โโโ RateStatus.java
โ โโโ TicketType.java
โ โโโ UserStatus.java
โโโ Main.java
- Database is connected by using jdbc
- To use this code and connect to your local database, change the "USERNAME" & "PASSWORD"
- You may need to change the URL if your schema name is not "WatchIt"
Connection conn = null;
final String USERNAME = "ENTER_YOUR_MYSQL_USERNAME";
final String PASSWORD = "ENTER_YOUR_MYSQL_PASSWORD";
final String URL = "jdbc:mysql://localhost:3306/WatchIt?characterEncoding=latin1&useConfigs=maxPerformance";
try{
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection(URL,USERNAME,PASSWORD);
}catch (ClassNotFoundException e){
e.printStackTrace();
System.out.println("Class not found!!");
}catch (SQLException e){
e.printStackTrace();
System.out.println("Connection failed!!");
}
1. Create a method making dynamic SQL statements
โก๏ธ Use PreparedStatement to create dynamic queries & use set methods to insert data for each property
public static void insertComments(ContentComment contentComment, Connection conn) throws SQLException {
String sqlStmt = "insert into content_comment values(?,?,?,?);";
PreparedStatement pStmt = null;
try{
pStmt = conn.prepareStatement(sqlStmt);
pStmt.setInt(1,contentComment.getId());
pStmt.setString(2,contentComment.getComment());
pStmt.setInt(3,contentComment.getUserProfileId());
pStmt.setInt(4,contentComment.getContentId());
pStmt.executeUpdate();
}catch (SQLException e){
e.printStackTrace();
}finally {
pStmt.close();
}
}
2. Create method for making Entities
3. Call the method made in (1) and provide entity made in (2) & connection instance parameters
private static void makeRandomContents() throws SQLException {
for(int i =1;i<=5;i++){
ContentDao contentDao = new ContentDao();
Content content = new Content();
content.setId(i);
content.setContentType(getContentType(getRandomIndex(0,4)));
content.setContentGenre(getContentGenre(getRandomIndex(0,12)));
content.setTitle(getRandomString(5,10));
content.setYear(getRandomIndex(1980,2022));
content.setDescription(getRandomString(30,300));
content.setPoster("https://watchIt.com/"+getRandomString(5,20)+".jpg");
content.setVideo("https://watchIt.com/"+getRandomString(5,20)+".mp4");
content.setTotalRateScore(getRandomDouble(0.0,5.0));
content.setAgeLimit(getRandomIndex(0,20));
contentDao.insertContent(content);
}
}
Result
โก๏ธ 100000 data created in database
- Function: Showing all contents by type
- Index needed in Content table
- SQL statement for creating index:
CREATE INDEX idx_type_content ON WatchIt.content(type);
- Function: Showing contents in user's my content list that the user didn't rate
- Index needed in MyContent table
- SQL statement for creating index:
CREATE INDEX idx_profile_id_rating_status ON WatchIt.my_content(user_profile_id,rating_status);
- Function: Change rate status of a certain content in my content list
- Index needed in MyContent table
- SQL statement for creating index:
CREATE INDEX dx_profile_content ON WatchIt.my_content(user_profile_id,content_id);
- Function:
- Update user ticket payment & status when "INACTIVE" user logged in
- ์ฌ์ฉ์ ๋ก๊ทธ์ธ ์ ์ฌ์ฉ์๊ฐ ๋นํ์ฑํ ๋ ์ฌ์ฉ์์ ์ด์ฉ๊ถ ๊ตฌ๋งค ๋ฐ status ์ ๋ฐ์ดํธ
- SQL statements in transaction:
-
S1-1 :
SELECT * FROM user WHERE username = ? and password = ? ;
-
S1-1 Explanation:
- Check if user with username & password exists
- ํ์๊ฐ์ ๋ ์ฌ์ฉ์ ์ค, ์ฌ์ฉ์ ์ ๋ ฅ์ผ๋ก ๋ค์ด์จ username & password๋ฅผ ๊ฐ๋ ์ฌ์ฉ์๊ฐ ์๋์ง ํ์ธ ํ ์๋ค๋ฉด ํด๋น ์ฌ์ฉ์ ์กฐํ
-
S1-2:
SELECT * FROM ticket; ( ์ ์ SQL )
-
S1-2 Explanation:
- If user status is INACTIVE, redirect to payment page & show all ticket types
- ์ฌ์ฉ์๊ฐ ๋นํ์ฑํ ๋ ์ฌ์ฉ์์ธ ๊ฒฝ์ฐ ์ด์ฉ์ ๊ตฌ๋งค ํ์ด์ง๋ก ์ด๋ํ์ฌ ์ด์ฉ๊ถ์ ๋ชจ๋ ๋ณด์ฌ์ค
-
S1-3:
SELECT MAX( id ) as total FROM orders;
-
S1-3 Explanation:
- If user bought a new ticket, get max id from order table
- ์ฌ์ฉ์๊ฐ ์ด์ฉ๊ถ์ ๊ตฌ๋งคํ๋ฉด orders ํ ์ด๋ธ์ ๋ ์ฝ๋๋ฅผ ํ๋ ์์ฑํด์ผ ํ๋๋ฐ ์ด๋ id๊ฐ ์ค์ ์ ์ํด ํ์ฌ orders ํ ์ด๋ธ์ ๋ ์ฝ๋ ์ค id ์นผ๋ผ์ ์ต๋๊ฐ์ ์กฐํํ์ฌ ์ด๋ณด๋ค 1๋งํผ ํฐ ์๋ฅผ ์ ๋ ์ฝ๋์ id ๊ฐ์ผ๋ก ์ค์ ํ๋ค.
-
S1-4:
INSERT INTO orders values(?,?,?,?,?);
-
S1-4 Explanation:
- If user bought a new ticket, add a record in order table
- ์ฌ์ฉ์๊ฐ ์ ์ด์ฉ๊ถ์ ๊ตฌ๋งคํ๋ค๋ฉด order ํ ์ด๋ธ์ ๋ ์ฝ๋ ํ๋ ์์ฑ
-
S1-5:
UPDATE user SET status = ? WHERE id = ?;
-
S1-5 Explanation:
- Update user status to ACTIVE
- ์ฌ์ฉ์๊ฐ์ ์ ์ด์ฉ์ ๊ตฌ๋งค๊ฐ ์ฑ๊ณต์ ์ด๋ฉฐ ์ฌ์ฉ์์ ์ํ๋ฅผ ๋นํ์ฑํ์์ ํ์ฑํ๋ก ๋ณ๊ฒฝํ๋ค. ์ด๋ ๋์ ๋งค๊ฐ๋ณ์ id์ ๊ฐ์ S1-1์์ ๋ฐ์ ๊ฒฐ๊ณผ ์ ์ ๋ฐ์ดํฐ ๊ฐ์ ์ผ๋ถ์ด๋ค.
-