Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --Alinea A
- CREATE KEYSPACE Videos WITH replication = {'class':'SimpleStrategy', 'replication_factor':1};
- USE Videos
- CREATE TABLE users (
- username text,
- nome text,
- email text,
- registryDate TIMESTAMP,
- PRIMARY KEY(username, nome)
- )
- CREATE TABLE videos (
- userShared text,
- nome text,
- description text,
- upload TIMESTAMP,
- tags list<text>,
- followers list<text>,
- PRIMARY KEY(userShared, nome, upload)
- )
- CREATE TABLE videos2 (
- userShared text,
- nome text,
- description text,
- upload TIMESTAMP,
- tags list<text>,
- followers list<text>,
- PRIMARY KEY(userShared, upload)
- )
- CREATE TABLE videos3 (
- nome text,
- upload TIMESTAMP,
- PRIMARY KEY(upload, nome)
- CREATE TABLE commentsByVideo (
- videoNome text,
- USER text,
- DATE TIMESTAMP,
- PRIMARY KEY(videoNome, DATE)
- ) WITH CLUSTERING ORDER BY (DATE DESC)
- CREATE TABLE commentsByUser (
- videoNome text,
- USER text,
- DATE TIMESTAMP,
- PRIMARY KEY(USER, DATE)
- ) WITH CLUSTERING ORDER BY (DATE DESC)
- CREATE TABLE events (
- videoNome text,
- USER text,
- eventType text,
- eventTime TIMESTAMP,
- videoTime INT,
- PRIMARY KEY(USER, videoNome, eventTime)
- )
- CREATE TABLE ratings (
- videoNome text,
- rating INT,
- PRIMARY KEY(videoNome)
- )
- CREATE TABLE tagsForVideos (
- tag text,
- videoNome text,
- PRIMARY KEY(tag, videoNome)
- )
- --Querries em Json Alinea B
- SELECT JSON * FROM ratings
- SELECT JSON * FROM tagsForVideos
- SELECT JSON * FROM events
- SELECT JSON * FROM commentsByUser
- SELECT JSON * FROM commentsByVideo
- SELECT JSON * FROM videos2
- SELECT JSON * FROM videos1
- SELECT JSON * FROM users
- -- Inserções Alinea B
- BEGIN BATCH
- INSERT INTO users(username, nome, email, registryDate) VALUES ('user1', 'Rui', 'r.cbd@gmail.com', dateof(now()))
- INSERT INTO users(username, nome, email, registryDate) VALUES ('user2', 'Ana', 'a.cbd@gmail.com', dateof(now()))
- INSERT INTO users(username, nome, email, registryDate) VALUES ('user3', 'Joao', 'j.cbd@gmail.com', dateof(now()))
- INSERT INTO users(username, nome, email, registryDate) VALUES ('user4', 'Maria', 'm.cbd@gmail.com', dateof(now()))
- INSERT INTO users(username, nome, email, registryDate) VALUES ('user5', 'Pedro', 'p.cbd@gmail.com', dateof(now()))
- INSERT INTO users(username, nome, email, registryDate) VALUES ('user6', 'Rita', 'ri.cbd@gmail.com', dateof(now()))
- INSERT INTO users(username, nome, email, registryDate) VALUES ('user7', 'Jose', 'js.cbd@gmail.com', dateof(now()))
- INSERT INTO users(username, nome, email, registryDate) VALUES ('user8', 'Bianca', 'b.cbd@gmail.com', dateof(now()))
- INSERT INTO users(username, nome, email, registryDate) VALUES ('user9', 'Lau', 'l.cbd@gmail.com', dateof(now()))
- INSERT INTO users(username, nome, email, registryDate) VALUES ('user10', 'Marco', 'ma.cbd@gmail.com', dateof(now()))
- APPLY BATCH;
- BEGIN BATCH
- INSERT INTO commentsByVideo(videoNome, USER, DATE) VALUES ('The Greatest Showman On Earth', 'user1', '2000-09-21 18:50' )
- INSERT INTO commentsByVideo(videoNome, USER, DATE) VALUES ('Wonder Wheel', 'user2', '2000-09-21 19:50' )
- INSERT INTO commentsByVideo(videoNome, USER, DATE) VALUES ('Birth of The Dragon', 'user3', '2000-09-21 20:50' )
- INSERT INTO commentsByVideo(videoNome, USER, DATE) VALUES ('Invisible', 'user4', '2000-09-21 17:50' )
- INSERT INTO commentsByVideo(videoNome, USER, DATE) VALUES ('Saw: Legacy', 'user5', '2000-09-21 16:50' )
- INSERT INTO commentsByVideo(videoNome, USER, DATE) VALUES ('The Greatest Showman On Earth', 'user6', '2000-09-21 15:50' )
- INSERT INTO commentsByVideo(videoNome, USER, DATE) VALUES ('Saw: Legacy', 'user10', '2000-09-21 14:50' )
- INSERT INTO commentsByVideo(videoNome, USER, DATE) VALUES ('Invisible', 'user7', '2000-09-21 13:50' )
- INSERT INTO commentsByVideo(videoNome, USER, DATE) VALUES ('Wonder Wheel', 'user8', '2000-09-21 12:50' )
- INSERT INTO commentsByVideo(videoNome, USER, DATE) VALUES ('Birth of The Dragon', 'user9', '2000-09-21 11:50' )
- INSERT INTO commentsByVideo(videoNome, USER, DATE) VALUES ('Birth of The Dragon', 'user1', '2000-09-21 12:50' )
- INSERT INTO commentsByUser(videoNome, USER, DATE) VALUES ('The Greatest Showman On Earth', 'user1', '2000-09-21 18:50' )
- INSERT INTO commentsByUser(videoNome, USER, DATE) VALUES ('Wonder Wheel', 'user2', '2000-09-21 19:50' )
- INSERT INTO commentsByUser(videoNome, USER, DATE) VALUES ('Birth of The Dragon', 'user3', '2000-09-21 20:50' )
- INSERT INTO commentsByUser(videoNome, USER, DATE) VALUES ('Invisible', 'user4', '2000-09-21 17:50' )
- INSERT INTO commentsByUser(videoNome, USER, DATE) VALUES ('Saw: Legacy', 'user5', '2000-09-21 16:50' )
- INSERT INTO commentsByUser(videoNome, USER, DATE) VALUES ('The Greatest Showman On Earth', 'user6', '2000-09-21 15:50' )
- INSERT INTO commentsByUser(videoNome, USER, DATE) VALUES ('Saw: Legacy', 'user10', '2000-09-21 14:50' )
- INSERT INTO commentsByUser(videoNome, USER, DATE) VALUES ('Invisible', 'user7', '2000-09-21 13:50' )
- INSERT INTO commentsByUser(videoNome, USER, DATE) VALUES ('Wonder Wheel', 'user8', '2000-09-21 12:50' )
- INSERT INTO commentsByUser(videoNome, USER, DATE) VALUES ('Birth of The Dragon', 'user9', '2000-09-21 11:50' )
- INSERT INTO commentsByUser(videoNome, USER, DATE) VALUES ('Birth of The Dragon', 'user1', '2000-09-21 12:50' )
- APPLY BATCH;
- BEGIN BATCH
- INSERT INTO events( videoNome, USER, eventType, eventTime, videoTime) VALUES ('The Greatest Showman On Earth', 'user1', 'play', '2000-09-21 10:50', 300)
- INSERT INTO events( videoNome, USER, eventType, eventTime, videoTime) VALUES ('Wonder Wheel', 'user2', 'stop', '2000-09-21 10:50', 200)
- INSERT INTO events( videoNome, USER, eventType, eventTime, videoTime) VALUES ('Birth of The Dragon', 'user3', 'pause', '2000-09-21 10:50', 100)
- INSERT INTO events( videoNome, USER, eventType, eventTime, videoTime) VALUES ('Invisible', 'user4', 'play', '2000-09-21 10:50', 350)
- INSERT INTO events( videoNome, USER, eventType, eventTime, videoTime) VALUES ('Saw: Legacy', 'user5', 'stop', '2000-09-21 10:50', 200)
- INSERT INTO events( videoNome, USER, eventType, eventTime, videoTime) VALUES ('The Greatest Showman On Earth', 'user6', 'pause', '2000-09-21 10:50', 300)
- INSERT INTO events( videoNome, USER, eventType, eventTime, videoTime) VALUES ('Saw: Legacy', 'user10', 'play', '2000-09-21 10:50', 300)
- INSERT INTO events( videoNome, USER, eventType, eventTime, videoTime) VALUES ('Invisible', 'user7', 'stop', '2000-09-21 10:50', 250)
- INSERT INTO events( videoNome, USER, eventType, eventTime, videoTime) VALUES ('Wonder Wheel', 'user8', 'pause', '2000-09-21 10:50', 100)
- INSERT INTO events( videoNome, USER, eventType, eventTime, videoTime) VALUES ('Birth of The Dragon', 'user9', 'play', '2000-09-21 10:50', 50)
- APPLY BATCH;
- BEGIN BATCH
- INSERT INTO tagsForVideos(tag,videoNome) VALUES ('action', 'Birth of The Dragon')
- INSERT INTO tagsForVideos(tag,videoNome) VALUES ('action', 'Invisible')
- INSERT INTO tagsForVideos(tag,videoNome) VALUES ('comedy', 'Wonder Wheel')
- INSERT INTO tagsForVideos(tag,videoNome) VALUES ('romance', 'Wonder Wheel')
- INSERT INTO tagsForVideos(tag,videoNome) VALUES ('terror', 'Saw: Legacy')
- INSERT INTO tagsForVideos(tag,videoNome) VALUES ('comedy', 'Singing')
- INSERT INTO tagsForVideos(tag,videoNome) VALUES ('fantasy', 'Willman')
- INSERT INTO tagsForVideos(tag,videoNome) VALUES ('action', 'Saw: Legacy')
- INSERT INTO tagsForVideos(tag,videoNome) VALUES ('comedy', 'Pirates walking on Land')
- APPLY BATCH;
- BEGIN BATCH
- INSERT INTO videos( userShared, nome, upload, description, tags, followers) VALUES ('user1', 'Birth of The Dragon', '2000-01-21 10:50', 'The dragon kills us all', ['action', 'drama' ], ['user1', 'user2' ])
- INSERT INTO videos( userShared, nome, upload, description, tags, followers) VALUES ('user2', 'Invisible', '2000-02-21 10:50', 'The man is invisible', ['action'], ['user3', 'user2' ])
- INSERT INTO videos( userShared, nome, upload, description, tags, followers) VALUES ('user4', 'Wonder Wheel', '2000-07-20 10:50', 'Wheel of fortune', ['romance', 'comedy' ], ['user6', 'user7' ])
- INSERT INTO videos( userShared, nome, upload, description, tags, followers) VALUES ('user2', 'Saw: Legacy', '2000-06-21 10:50', 'Another SAW', ['terror', 'drama' ], ['user1', 'user2', 'user3' ])
- INSERT INTO videos( userShared, nome, upload, description, tags, followers) VALUES ('user1', 'The Greatest Showman On Earth', '2000-09-21 10:50', 'Showman that does magic tricks', ['comedy' ], ['user5', 'user6' ])
- INSERT INTO videos( userShared, nome, upload, description, tags, followers) VALUES ('user7', 'Liberty City', '2000-10-21 10:50', 'Another thug', ['drama', 'trhiller'], ['user1', 'user2', 'user4' ])
- INSERT INTO videos( userShared, nome, upload, description, tags, followers) VALUES ('user9', 'Pirates walking on Land', '2000-07-21 10:50', 'The movie itself is a mess', ['comedy'], ['user10', 'user9' ])
- INSERT INTO videos( userShared, nome, upload, description, tags, followers) VALUES ('user7', 'Room on Fire', '2000-08-11 10:50', 'Comedy like the usual', ['comedy', 'romance' ], ['user4', 'user5' ])
- INSERT INTO videos( userShared, nome, upload, description, tags, followers) VALUES ('user3', 'The WillMan', '2000-08-19 10:50', 'The guy talks about Will', ['fantasy' ], ['user1', 'user4', 'user2' ])
- INSERT INTO videos( userShared, nome, upload, description, tags, followers) VALUES ('user8', 'Singing', '2000-08-20 10:50', 'She sings poorly', ['comedy' ], ['user5', 'user4' ])
- APPLY BATCH;
- BEGIN BATCH
- INSERT INTO videos2( userShared, nome, upload, description, tags, followers) VALUES ('user1', 'Birth of The Dragon', '2000-01-21 10:50', 'The dragon kills us all', ['action', 'drama' ], ['user1', 'user2' ])
- INSERT INTO videos2( userShared, nome, upload, description, tags, followers) VALUES ('user2', 'Invisible', '2000-02-21 10:50', 'The man is invisible', ['action'], ['user3', 'user2' ])
- INSERT INTO videos2( userShared, nome, upload, description, tags, followers) VALUES ('user4', 'Wonder Wheel', '2000-07-20 10:50', 'Wheel of fortune', ['romance', 'comedy' ], ['user6', 'user7' ])
- INSERT INTO videos2( userShared, nome, upload, description, tags, followers) VALUES ('user2', 'Saw: Legacy', '2000-06-21 10:50', 'Another SAW', ['terror', 'drama' ], ['user1', 'user2', 'user3' ])
- INSERT INTO videos2( userShared, nome, upload, description, tags, followers) VALUES ('user1', 'The Greatest Showman On Earth', '2000-09-21 10:50', 'Showman that does magic tricks', ['comedy' ], ['user5', 'user6' ])
- INSERT INTO videos2( userShared, nome, upload, description, tags, followers) VALUES ('user7', 'Liberty City', '2000-10-21 10:50', 'Another thug', ['drama', 'trhiller'], ['user1', 'user2', 'user4' ])
- INSERT INTO videos2( userShared, nome, upload, description, tags, followers) VALUES ('user9', 'Pirates walking on Land', '2000-07-21 10:50', 'The movie itself is a mess', ['comedy'], ['user10', 'user9' ])
- INSERT INTO videos2( userShared, nome, upload, description, tags, followers) VALUES ('user7', 'Room on Fire', '2000-08-11 10:50', 'Comedy like the usual', ['comedy', 'romance' ], ['user4', 'user5' ])
- INSERT INTO videos2( userShared, nome, upload, description, tags, followers) VALUES ('user3', 'The WillMan', '2000-08-19 10:50', 'The guy talks about Will', ['fantasy' ], ['user1', 'user4', 'user2' ])
- INSERT INTO videos2( userShared, nome, upload, description, tags, followers) VALUES ('user8', 'Singing', '2000-08-20 10:50', 'She sings poorly', ['comedy' ], ['user5', 'user4' ])
- APPLY BATCH;
- BEGIN BATCH
- INSERT INTO videos3( nome, upload) VALUES ('Birth of The Dragon', '2000-01-21 10:50')
- INSERT INTO videos3( nome, upload) VALUES ('Invisible', '2000-02-21 10:50')
- INSERT INTO videos3( nome, upload) VALUES ('Wonder Wheel', '2000-07-20 10:50')
- INSERT INTO videos3( nome, upload) VALUES ('Saw: Legacy', '2000-06-21 10:50')
- INSERT INTO videos3( nome, upload) VALUES ('The Greatest Showman On Earth', '2000-09-21 10:50')
- INSERT INTO videos3( nome, upload) VALUES ('Liberty City', '2000-10-21 10:50')
- INSERT INTO videos3( nome, upload) VALUES ('Pirates walking on Land', '2000-07-21 10:50')
- INSERT INTO videos3( nome, upload) VALUES ('Room on Fire', '2000-08-11 10:50')
- INSERT INTO videos3( nome, upload) VALUES ('The WillMan', '2000-08-19 10:50')
- INSERT INTO videos3( nome, upload) VALUES ('Singing', '2000-08-20 10:50')
- APPLY BATCH;
- BEGIN BATCH
- INSERT INTO ratings(videoNome, rating) VALUES ( 'Birth of The Dragon' , 3)
- INSERT INTO ratings(videoNome, rating) VALUES ( 'Birth of The Dragon' , 4)
- INSERT INTO ratings(videoNome, rating) VALUES ( 'Invisible' , 2)
- INSERT INTO ratings(videoNome, rating) VALUES ( 'Invisible' , 4)
- INSERT INTO ratings(videoNome, rating) VALUES ( 'Wonder Wheel' , 3)
- INSERT INTO ratings(videoNome, rating) VALUES ( 'Wonder Wheel' , 1)
- INSERT INTO ratings(videoNome, rating) VALUES ( 'Saw: Legacy' , 2)
- INSERT INTO ratings(videoNome, rating) VALUES ( 'Saw: Legacy' , 5)
- INSERT INTO ratings(videoNome, rating) VALUES ( 'The Greatest Showman On Earth' , 4)
- INSERT INTO ratings(videoNome, rating) VALUES ( 'The Greatest Showman On Earth' , 3)
- INSERT INTO ratings(videoNome, rating) VALUES ( 'Liberty City' , 2)
- INSERT INTO ratings(videoNome, rating) VALUES ( 'Liberty City' , 1)
- INSERT INTO ratings(videoNome, rating) VALUES ( 'Pirates walking on Land' , 2)
- INSERT INTO ratings(videoNome, rating) VALUES ( 'Pirates walking on Land' , 4)
- INSERT INTO ratings(videoNome, rating) VALUES ( 'Room on Fire' , 1)
- INSERT INTO ratings(videoNome, rating) VALUES ( 'Room on Fire' , 5)
- INSERT INTO ratings(videoNome, rating) VALUES ( 'The WillMan' , 4)
- INSERT INTO ratings(videoNome, rating) VALUES ( 'The WillMan' ,3)
- INSERT INTO ratings(videoNome, rating) VALUES ( 'Singing' ,5)
- INSERT INTO ratings(videoNome, rating) VALUES ( 'Singing' ,2)
- APPLY BATCH;
- -- Alinea C)
- --Permitir a pesquisa de todos os vídeos de determinado autor;
- SELECT * FROM videos WHERE autor = 'James Will'
- --Permitir a pesquisa de comentários por utilizador, ordenado inversamente pela data;
- SELECT * FROM commentsByUser WHERE USER = 'user1'
- --Permitir a pesquisa de comentários por vídeos, ordenado inversamente pela data;
- SELECT * FROM commentsByVideo WHERE videoNome = 'The Greatest Showman On Earth'
- --Permitir a pesquisa do rating médio de um vídeo e quantas vezes foi votado;
- SELECT videoNome, avg(rating) AS avgRate, COUNT(rating) AS COUNT FROM ratings WHERE videoNome = 'The Greatest Showman On Earth'
- --Alinea D)
- USE videos
- --1. Os últimos 3 comentários introduzidos para um vídeo;
- SELECT * FROM commentsByVideo WHERE videoNome = 'The Greatest Showman On Earth'
- ORDER BY DATE ASC LIMIT 3
- --2 Lista das tags de determinado vídeo;
- CREATE INDEX videos_name ON videos( nome );
- SELECT nome,tags FROM videos WHERE nome = 'The Greatest Showman On Earth'
- --3 Todos os vídeos com a tag Aveiro;
- CREATE INDEX tags_index ON videos( tags );
- SELECT nome, tags FROM videos WHERE tags CONTAINS 'action';
- --4 Os últimos 5 eventos de determinado vídeo realizados por um utilizador;
- SELECT USER, videoNome FROM events WHERE USER= 'user10' AND videoNome = 'Saw: Legacy' LIMIT 5
- --5 Vídeos partilhados por determinado utilizador (maria1987, por exemplo) num
- -- determinado período de tempo (Agosto de 2017, por exemplo);
- SELECT userShared, nome FROM videos2 WHERE userShared = 'user8' AND upload >= '2000-08-01 00:00:00' AND upload < '2000-09-01 00:00:00';
- --6 Os últimos 10 vídeos, ordenado inversamente pela data da partilhada;
- SELECT * FROM videos3 ORDER BY upload LIMIT 10; --Não funciona.Order by tem de restringir a partition key.
- --Mesmo usando uma tabela onde o upload time é a partition key, teria que o restringir na mesma, o que não vai de encontro à solução
- --7 Todos os seguidores (followers) de determinado vídeo;
- SELECT followers FROM videos WHERE nome = 'Invisible'
- --8 Todos os comentários (dos vídeos) que determinado utilizador está a seguir (following);
- --Esta query requere 2 passos para ser feita. Poderia existir uma tabela que regista para determinado utilizador os videos que ele segue
- --Este seria o primeiro passo. O segundo passo seria no lado do cliente, para cada resultado de um select na tabela de cima ir à tabela de comentários por vídeo
- --E arranjar esses comentários. Em baixo fica um exemplo da tabela que seria preciso construir para esta query, em conjunto com a já existente
- --commentsByVideo
- CREATE TABLE followers (
- USER text,
- videoNome text,
- PRIMARY KEY(USER, videoNome)
- )
- --9. Os 5 vídeos com maior rating;
- --Pela mesma razão que em 6, é impossível ordenar sem restringir a partition key. Aqui queremos os videos ordenados
- --Mas não sabemos quais portanto não os podemos restringir.
- --10 Uma query que retorne todos os vídeos e que mostre claramente a forma pela qual estão ordenados;
- --Mesmo caso que 9 e 6, está query é impossível
- --11 Lista com as Tags existentes e o número de vídeos catalogados com cada uma delas;
- SELECT tag, COUNT(tag) AS counTag FROM tagsForVideos GROUP BY tag
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement