Crossing the Finish Line

DSE Version: 6.7

Intro

Exercises

Slides

No write up.

Exercise: Finish Line!

In this exercise, you will:

  • Be awesome

  • View videos in your KillrVideo application

  • Complete your journey of becoming an Apache Cassandratm App Dev Hero!

Dah dah dah dah dah daaaaaaaaah! You made it! The last module! You've created a database, connected to it with the driver, did some lightweight transaction and batch magic, and got some video data into your database. Wouldn't it be epic to actually see the videos show up on the homepage? Or better yet...play them?

Steps

1) Write a ton more code. Just kidding. All you need to do is implement the getLatestVideos() and getVideo() methods inside VideoAccess.java. getLatestVideos() pulls the most recently added videos from the latest_videos table. getVideo() retrieves a specific video from the videos table given a videoid. You could implement these methods in a variety of ways:

  • You could use execute() in its raw form
  • You could use SimpleStatement.newInstance()
  • You could use QueryBuilder
  • You can optionally turn your queries into PreparedStatements

All of these you have already done up to this point. So, doing so would be extra practice. So in the interest of time, we implemented these methods for you. Copy the code below and paste it over their existing counterparts (getVideo() and getLatestVideos()) in the killrvideo/src/main/java/killrvideo/dataLayer/VideoAccess.java file:

  public static Video getVideo(DseSession session, UUID videoId) throws Exception {
    LOGGER.debug("------Start get video------");
        
    SimpleStatement getVideo = SimpleStatement.newInstance(
      "SELECT * FROM VIDEOS WHERE videoid = ?", videoId);

    Row row = session.execute(getVideo).one();
    
    Video video = new Video(
      row.getUuid("videoid"),
      row.getUuid("userid"),
      row.getString("name"),
      row.getString("description"),
      row.getString("location"),
      row.getInt("location_type"),
      row.getString("preview_image_location"),
      row.getSet("tags", String.class),
      row.getInstant("added_date")); 

    return video;     
  }

  public static List getLatestVideos(DseSession session) throws Exception {
    LOGGER.debug("------Start get latest videos------");
  
    SimpleStatement getLatestVideos = SimpleStatement.newInstance(
      "SELECT * FROM killrvideo.latest_videos WHERE bucket = 0 LIMIT 5");

    ResultSet results = session.execute(getLatestVideos);
    
    List ret = new ArrayList();
    
    for(Row row : results) {
      LatestVideos latestVideo = new LatestVideos(
        row.getUuid("userid"),
        row.getUuid("videoid"),
        row.getString("name"),
        row.getString("preview_image_location"),
        row.getInstant("added_date")
      );
      ret.add(latestVideo);
    }
    
    return ret;
  }
If you didn't get the entire VideoAccess.java file complete from the previous exercises, click here.
ARE YOU SUUUUUUUUUUUURRRRRREEEEE YOU NEED THE SOLUTION??? If so, click here. Don't feel intimidated.
package killrvideo.dataLayer;

import java.io.File;
import java.time.Instant;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

import com.datastax.oss.driver.api.core.cql.BatchStatement;
import com.datastax.oss.driver.api.core.cql.BoundStatement;
import com.datastax.oss.driver.api.core.cql.DefaultBatchType;
import com.datastax.oss.driver.api.core.cql.PreparedStatement;
import com.datastax.oss.driver.api.core.cql.ResultSet;
import com.datastax.oss.driver.api.core.cql.Row;
import com.datastax.oss.driver.api.core.cql.SimpleStatement;
import com.datastax.oss.driver.api.core.cql.SimpleStatementBuilder;
import com.datastax.dse.driver.api.core.DseSession;
import com.datastax.oss.driver.api.core.DefaultConsistencyLevel;
import com.datastax.oss.driver.api.querybuilder.QueryBuilder;
import com.datastax.oss.driver.api.querybuilder.insert.RegularInsert;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import killrvideo.entity.LatestVideos;
import killrvideo.entity.Video;

public class VideoAccess {
  private static final Logger LOGGER = LoggerFactory.getLogger(UserAccess.class);
  
  static PreparedStatement insertVideoStatementPrepared;
  static PreparedStatement insertLatestVideoStatementPrepared;

  public static void addNewVideo(DseSession session, Video video) throws Exception {
    LOGGER.debug("------Start add new video------");

    Instant now = Instant.now();

    BoundStatement insertVideoStatementBound = insertVideoStatementPrepared.bind()
      .setUuid("videoid", video.getVideoid())
      .setUuid("userid", video.getUserid()) 
      .setString("name", video.getName())
      .setString("description", video.getDescription())
      .setString("location", video.getLocation())
      .setInt("location_type", 0)
      .setString("preview_image_location", video.getPreviewImageLocation())
      .setInstant("added_date", Instant.now())
      .setSet("tags", video.getTags(), String.class);

    BoundStatement insertLatestVideoStatementBound = insertLatestVideoStatementPrepared.bind()
      .setInt("bucket", 0)
      .setInstant("added_date", now)
      .setUuid("videoid", video.getVideoid())
      .setString("name", video.getName())
      .setString("preview_image_location", video.getPreviewImageLocation())
      .setUuid("userid", video.getUserid());
      
    //----------------------------------------------------------------
    // Build a BatchStatement:
    //----------------------------------------------------------------
    BatchStatement batch = BatchStatement.newInstance(DefaultBatchType.LOGGED,
      insertVideoStatementBound, insertLatestVideoStatementBound);

    //----------------------------------------------------------------
    // Execute the statement here:
    //----------------------------------------------------------------
    session.execute(batch);
  }
  
  public static Video getVideo(DseSession session, UUID videoId) throws Exception {
    LOGGER.debug("------Start get video------");
        
    SimpleStatement getVideo = SimpleStatement.newInstance(
      "SELECT * FROM VIDEOS WHERE videoid = ?", videoId);

    Row row = session.execute(getVideo).one();
    
    Video video = new Video(
      row.getUuid("videoid"),
      row.getUuid("userid"),
      row.getString("name"),
      row.getString("description"),
      row.getString("location"),
      row.getInt("location_type"),
      row.getString("preview_image_location"),
      row.getSet("tags", String.class),
      row.getInstant("added_date")); 

    return video;     
  }

  public static List getLatestVideos(DseSession session) throws Exception {
    LOGGER.debug("------Start get latest videos------");
  
    SimpleStatement getLatestVideos = SimpleStatement.newInstance(
      "SELECT * FROM killrvideo.latest_videos WHERE bucket = 0 LIMIT 5");

    ResultSet results = session.execute(getLatestVideos);
    
    List ret = new ArrayList();
    
    for(Row row : results) {
      LatestVideos latestVideo = new LatestVideos(
        row.getUuid("userid"),
        row.getUuid("videoid"),
        row.getString("name"),
        row.getString("preview_image_location"),
        row.getInstant("added_date")
      );
      ret.add(latestVideo);
    }
    
    return ret;
  } 
  
  public static void init(DseSession session) {
    // Insert into videos (from a previous exercise)
    SimpleStatementBuilder insertVideoBuilder = 
      QueryBuilder.insertInto("killrvideo", "videos")
        .value("videoId", QueryBuilder.bindMarker())
        .value("userId", QueryBuilder.bindMarker())
        .value("name", QueryBuilder.bindMarker())
        .value("description", QueryBuilder.bindMarker())
        .value("location", QueryBuilder.bindMarker())
        .value("location_type", QueryBuilder.bindMarker())
        .value("preview_image_location", QueryBuilder.bindMarker())
        .value("tags", QueryBuilder.bindMarker())
        .value("added_date", QueryBuilder.bindMarker())
        .builder();
        
    // Insert into latest_videos (following the same pattern)
    SimpleStatementBuilder insertLatestVideoBuilder = 
      QueryBuilder.insertInto("killrvideo", "latest_videos")
        .value("bucket", QueryBuilder.bindMarker())
        .value("added_date", QueryBuilder.bindMarker())
        .value("videoid", QueryBuilder.bindMarker())
        .value("name", QueryBuilder.bindMarker())
        .value("preview_image_location", QueryBuilder.bindMarker())
        .value("userid", QueryBuilder.bindMarker())
        .builder();    
                       
    SimpleStatement insertVideoStatement = insertVideoBuilder.build();
    SimpleStatement insertLatestVideoStatement = insertLatestVideoBuilder.build();
        
    insertVideoStatementPrepared = session.prepare(insertVideoStatement);
    insertLatestVideoStatementPrepared = session.prepare(insertLatestVideoStatement);
  }
}

2) Add two more videos. Below are two more epic cat video URLs. Use your KillrVideo application UI to add these. If you don't remember how to add a video in the UI, follow steps 1 through 4 located here. After you add each video, click on the KillrVideo icon in the upper-left corner to view your progress.

https://www.youtube.com/watch?v=ucQe-3XLEDE
https://www.youtube.com/watch?v=XbNFrfZADA8

3) Bask in the glory! Bounce your killrvideo application and visit the homepage. Notice the latest videos show up! (You may have to do a hard browser refresh (CMND+SHIFT+R for Chrome/Mac users and CTRL+F5 for Chrome/Windows users)).

Click on one, and see how its details load. (FYI If you used other YouTube videos than suggested, know that some YouTube videos have copyright restrictions which prevent them from playing.)

You can even play the video from this page! Cool eh?

You did it! You survived Jamie and Steve for an entire day! But what's better is that you have become an Apache Cassandratm app-dev hero! Congratulations!

END OF EXERCISE

No FAQs.
No resources.
Comments are closed.