Big Data Analytics 简明教程

Big Data Analytics - Data Collection

数据收集在“大数据”周期中发挥着最重要的作用。Internet 几乎提供了无限的数据源,可用于各种主题。该领域的重要性取决于业务类型,但传统行业可以获取外部数据的多元化来源,并将这些数据与其交易数据相结合。

例如,让我们假设要构建一个推荐餐厅的系统。第一步是收集数据,在本例中,从不同网站收集餐厅评论,并将它们存储在数据库中。由于我们对原始文本感兴趣,并且会将其用于分析,因此在何处存储用于开发模型的数据并不是那么重要。这听起来可能与大数据主要技术相矛盾,但为了实现大数据应用程序,我们只需要让它实时运行即可。

Twitter Mini Project

定义问题后,接下来的阶段是收集数据。以下小项目的想法是对收集自网络的数据进行处理,并对其进行结构化,以便用于机器学习模型。我们将使用 R 编程语言从 Twitter Rest API 收集一些推文。

首先创建一个 Twitter 帐户,然后按照 twitteRvignette 中的说明创建一个 Twitter 开发者帐户。以下是这些说明的摘要——

  1. 转至 https://twitter.com/apps/new 并登录。

  2. 填写基本信息后,转到“设置”选项卡,然后选择“读取、编写和访问直接消息”。

  3. 执行此操作后,请务必单击保存按钮

  4. 在“详细信息”选项卡中,记下您的客户机密钥和客户机密

  5. 在 R 会话中,您将使用 API 密钥和 API 密文值

  6. 最后运行以下脚本。这将从其在 GitHub 上的存储库中安装 twitteR 包。

install.packages(c("devtools", "rjson", "bit64", "httr"))

# Make sure to restart your R session at this point
library(devtools)
install_github("geoffjentry/twitteR")

我们感兴趣的是,在其中包括字符串“big mac”的数据,并找出有关此字符串的突出主题。为此,第一步是从 Twitter 收集数据。以下是我们的 R 脚本,用于从 Twitter 收集所需数据。此代码也位于 bda/part1/collect_data/collect_data_twitter.R 文件中。

rm(list = ls(all = TRUE)); gc() # Clears the global environment
library(twitteR)
Sys.setlocale(category = "LC_ALL", locale = "C")

### Replace the xxx’s with the values you got from the previous instructions

# consumer_key = "xxxxxxxxxxxxxxxxxxxx"
# consumer_secret = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"

# access_token = "xxxxxxxxxx-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"

# access_token_secret= "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"

# Connect to twitter rest API
setup_twitter_oauth(consumer_key, consumer_secret, access_token, access_token_secret)

# Get tweets related to big mac
tweets <- searchTwitter(’big mac’, n = 200, lang = ’en’)
df <- twListToDF(tweets)

# Take a look at the data
head(df)

# Check which device is most used
sources <- sapply(tweets, function(x) x$getStatusSource())
sources <- gsub("</a>", "", sources)
sources <- strsplit(sources, ">")
sources <- sapply(sources, function(x) ifelse(length(x) > 1, x[2], x[1]))
source_table = table(sources)
source_table = source_table[source_table > 1]
freq = source_table[order(source_table, decreasing = T)]
as.data.frame(freq)

#                       Frequency
# Twitter for iPhone       71
# Twitter for Android      29
# Twitter Web Client       25
# recognia                 20