Analyze Donald Trump Tweets with Couchbase and N1QL

AWS Serverless Lambda Scheduled Events to Store Tweets in Couchbase explained how to store tweets in Couchbase using AWS Serverless Lambda. Now, this Lambda Function has been running for a few days and has collected 269 tweets from @realDonaldTrump. This blog , inspired by SQL on Twitter: Analysis Made Easy Using N1QL, will show how these tweets can be analyzed using N1QL.

trump-tweets
N1QL is a SQL-like query language from Couchbase that operates on JSON documents. N1QL and SQL Differences provide differences between N1QL and SQL. Let’s use N1QL to reveal some interesting information from @realDonaldTrump‘s tweets.

Many thanks to Sitaram from N1QL team to help hack the queries.

How Many Tweets

First query is to find out how many tweets are available in the database. The query is pretty simple:

Query:

As you notice, the syntax is very similar to SQL. SELECT, COUNT and FROM clauses are what you are already familiar with from SQL syntax. tweet_count is an alias defined for the returned result. twitter is the bucket where all the JSON documents are stored.

Results:

The result is a JSON document as well.

Tweet Sample JSON Document

In order to write queries on a JSON document, you need to know the structure of the document. The next query will give you that.

Query:

The new clause introduced here is LIMIT. This allows to restrict the number of objects that are returned in a result set of SELECT.

Results:

Top 5 Tweeting Days

After the basic queries are out of the way, let’s look at some interesting data now.

What are the top 5 days on which @realDonaldTrump tweeted and the tweet count?

Query:

Usual GROUP BY and ORDER BY SQL clauses perform the same function.

N1QL Functions apply a function to values. The createdAt field is returned a number as a String. TO_NUM function converts the String to a number. MILLIS_TO_STR function converts the String to a date. Finally, SUBSTR function extracts the relevant part of the date.

Results:

Jan 17th, 2017 is the most tweeted day. Now, this result is of course restricted to the data from the JSON documents stored in the database.

Does anybody have a more comprehensive database of @realDonaldTrump tweets?

Tweet Frequency

OK, our database shows that that maximum number of tweets in a day were 13. How do I find out how many days @realDonaldTrump tweeted a certain number of times?

Query:

This is easily achieved using N1QL nested queries.

Results:

In 47 days, there is only one day with a single tweet. A sum total of tweet_count shows that there is no single day without a tweet :)

Most Common Hour In a Day To Tweet

@realDonaldTrump is known to tweet at 3am. Let’s take a look what are the most common hours for him to tweet.

Query:

Results:

Now seems like the controversial tweets come at 3am. But 39 tweets are coming at 1pm ET, likely right after lunch and while having a dessert 😉

Common Day of The Week to Tweet

Let’s find out what are the most common day of the week to tweet.

Query:

DATE_PART_STR is a new function returns date part of the date. Further day_of_week attribute is used to get day of the week.

Results:

Seems like Tuesday is the most common day to tweet. Then comes Sunday and Wednesday at the same level. The performance tends to fizzle out closer to the weekend.

Here is a nice chart that shows the same trend:

realdonaldtrump-tweets-per-day

#22417 should allow to report the weekday part in English.

Top 5 Mentions in Tweets

Query:

userMentionEntities is a nested array in the JSON document. UNNEST conceptually performs a join of the nested array with its parent object. Each resulting joined object becomes an input to the query.

Results:

Needless to say, he mentions his own name the most in tweets! And his two favorite TV stations Fox News and CNN.

Top 5 Tweets with RTs

Lambda Function wakes up every 3 hours and fetches the latest tweets. So the database is a snapshot of tweets and associated information such as RTs and Favorites. So depending upon when the tweet was archived, the RTs and Favorites may not be an accurate representation. But given this information, let’s take a look at the tweets with most RTs.

Query:

Pretty straight forward query.

Results:

Original vs RTs

How many of tweets were written vs retweeted?

Query:

Results:

Most of the tweets are original with only a few RTs.

Most Common Words in Tweet

Query:

This query uses SPLIT function that

Results:

Frequency of words “media”, “fake” and “America” in tweets

Query:

LOWER function is used to compare words independent of the case.

Result:

Lambda function will continue to store tweets in the database.

Try these queries yourself?

N1QL References

  • N1QL Interactive Tutorial
  • N1QL Cheatsheet
  • N1QL Language Reference
  • Run Your First N1QL Query

Source: https://blog.couchbase.com/2017/january/analyze-donald-trump-tweets-couchbase-n1ql

Be Sociable, Share!
  • Tweet

28 thoughts on “Analyze Donald Trump Tweets with Couchbase and N1QL

  1. It is the information I am looking for. I’m learning more about this. Hope you bring more things related to it. Thank you very much!

  2. hey friends robox robux is now with new best ever resources added like get free robux free robux generator etc.

  3. Thank for your writting! It is easy to understand and detailed. I have read many other articles on the same subject, but your article has persuaded me!
    usps tracking

  4. Hi there to every one, since I am genuinely keen of
    reading this website’s post to be updated daily. It carries
    nice data.
    happy birthday wishes for a friend

  5. This is a great post. I like this topic. This site has lots of advantage. I found many interesting things from this site. It helps me in many ways. Thanks for posting this again.

  6. I easily understand this. This is very useful for me which I am looking for. Thanks for sharing.

  7. Hi admin, thanks for taking time to share this page. It gives lots of information to me and I learned a lot from your blog.

  8. Great and Informative Blog. Your style of writing is very unique. Thanks for Posting.

    IELTS coaching in Chennai

  9. Very informative post. Looking for this information for a long time. Thanks for Sharing.

    Tableau Training in Chennai

  10. Great Post. It shows your deep understanding of the topic. Thanks for Posting.

    Node JS Training in Chennai

  11. Great Posting…
    Keep doing it…
    Thanks

    Digital Marketing Certification Course in Chennai – Eminent Digital Academy

  12. This article was great, I read a lot of articles with this topic, but I found your article to have the most impressive and detailed content, I hope you will have more interesting tracks. more to bring to the reader, thank you for sharing.
    candy crush soda

  13. It is as yet critical, nonetheless, to ensure you know about every one of the subjects since you don’t know which ones will show up on your exam

  14. The article you have shared here very awesome. I really like and appreciated your work. I read deeply your article, the points you have mentioned in this article are useful

  15. Thanks for sharing such a informative article!!
    this all information are really helpful for java freshers.

  16. Un grande piatto quando passi la tua mano, ho seguito la tua ricetta ed i risultati hanno superato le mie aspettative

  17. I love this grate article!!
    My all Queries are solve by reading this blog!!
    Please share article like this ,its helpful for many peoples.
    concepts are explain very nicely.
    keep sharing

Leave a Reply

Your email address will not be published. Required fields are marked *