The hiveddl.sql script has performed the following steps to refine the data:

  • Converted the raw Twitter data into a tabular format.
  • Used the dictionary file to score the sentiment of each Tweet by the number of positive words compared to the number of negative words, and then assigned a positive, negative, or neutral sentiment value to each Tweet.
  • Created a new table that includes the sentiment value for each Tweet.

http://hortonworks.com/hadoop-tutorial/how-to-refine-and-visualize-sentiment-data/

ADD JAR json-serde-1.1.6-SNAPSHOT-jar-with-dependencies.jar;

--create the tweets_raw table containing the records as received from Twitter

CREATE EXTERNAL TABLE tweets_raw (
id BIGINT,
created_at STRING,
source STRING,
favorited BOOLEAN,
retweet_count INT,
retweeted_status STRUCT<
text:STRING,
user:STRUCT<screen_name:STRING,name:STRING>>,
entities STRUCT<
urls:ARRAY<STRUCT<expanded_url:STRING>>,
user_mentions:ARRAY<STRUCT<screen_name:STRING,name:STRING>>,
hashtags:ARRAY<STRUCT<text:STRING>>>,
text STRING,
user STRUCT<
screen_name:STRING,
name:STRING,
friends_count:INT,
followers_count:INT,
statuses_count:INT,
verified:BOOLEAN,
utc_offset:STRING, -- was INT but nulls are strings
time_zone:STRING>,
in_reply_to_screen_name STRING,
year int,
month int,
day int,
hour int
)
ROW FORMAT SERDE 'org.openx.data.jsonserde.JsonSerDe'
LOCATION '/user/hue/upload/upload/data/tweets_raw'
; -- create sentiment dictionary
CREATE EXTERNAL TABLE dictionary (
type string,
length int,
word string,
pos string,
stemmed string,
polarity string
)
ROW FORMAT DELIMITED FIELDS TERMINATED BY '\t'
STORED AS TEXTFILE
LOCATION '/user/hue/upload/upload/data/dictionary'; CREATE EXTERNAL TABLE time_zone_map (
time_zone string,
country string,
notes string
)
ROW FORMAT DELIMITED FIELDS TERMINATED BY '\t'
STORED AS TEXTFILE
LOCATION '/user/hue/upload/upload/data/time_zone_map'; -- Clean up tweets
CREATE VIEW tweets_simple AS
SELECT
id,
cast ( from_unixtime( unix_timestamp(concat( '2013 ', substring(created_at,5,15)), 'yyyy MMM dd hh:mm:ss')) as timestamp) ts,
text,
user.time_zone
FROM tweets_raw
; CREATE VIEW tweets_clean AS
SELECT
id,
ts,
text,
m.country
FROM tweets_simple t LEFT OUTER JOIN time_zone_map m ON t.time_zone = m.time_zone; -- Compute sentiment
create view l1 as select id, words from tweets_raw lateral view explode(sentences(lower(text))) dummy as words;
create view l2 as select id, word from l1 lateral view explode( words ) dummy as word ; -- was: select * from l2 left outer join dict d on l2.word = d.word where polarity = 'negative' limit 10; create view l3 as select
id,
l2.word,
case d.polarity
when 'negative' then -1
when 'positive' then 1
else 0 end as polarity
from l2 left outer join dictionary d on l2.word = d.word; create table tweets_sentiment stored as orc as select
id,
case
when sum( polarity ) > 0 then 'positive'
when sum( polarity ) < 0 then 'negative'
else 'neutral' end as sentiment
from l3 group by id; -- put everything back together and re-number sentiment
CREATE TABLE tweetsbi
STORED AS ORC
AS
SELECT
t.*,
case s.sentiment
when 'positive' then 2
when 'neutral' then 1
when 'negative' then 0
end as sentiment
FROM tweets_clean t LEFT OUTER JOIN tweets_sentiment s on t.id = s.id; -- for Tableau or Excel
-- UDAF sentiscore = sum(sentiment)*50 / count(sentiment) -- context n-gram made readable
CREATE TABLE twitter_3grams
STORED AS RCFilese
AS
SELECT year, month, day, hour, snippet
FROM
( SELECT
year,
month,
day,
hour,
context_ngrams(sentences(lower(text)), array("iron","man","3",null,null,null), 10) ngs
FROM tweets group by year,month,day, hour
) base
LATERAL VIEW
explode( ngs ) ngsTab AS snippet -- ngsTab is random alias => must be there even though not used
;

Hortonworks 用于做 Sentimental Analysis的Hiveddl.sql 文件的更多相关文章

  1. vis用于做3D图表的js插件

    vis.js用于做3D图表:(浏览网站需要FQ)实例:http://visjs.org/graph3d_examples.html代码下载:https://github.com/almende/vis

  2. [开源硬件DIY] 自制一款精致炫酷的蓝牙土壤温湿度传感器,用于做盆栽呵护类产品(API开放,开发者可自行DIY微信小程序\安卓IOS应用)

    目录 前言: 1. 成品展示 2. 原理图解析 3. pcb设计 4. 嵌入式对外提供接口 4.1 蓝牙广播 4.2 蓝牙服务和属性 4.3 数据包格式 4.4 数据通信模型 重要 . 前言: 本期给 ...

  3. 批量执行SQL文件

    原文:批量执行SQL文件 摘要:很多时候我们在做系统升级时需要将大量的.sql文件挨个执行,十分不方便.而且考虑到执行顺序和客服的操作方便性,能不能找到一种简单的方法来批量执行这些sql文件呢? 主要 ...

  4. sh 脚本执行sql文件传参数

    一.前言 今天做数据删除,用的命令行输入参数,并且调用执行的sql文件,我采用了sed命令,进行替换. sh脚本如下 #! /bin/sh echo "Please enter the ba ...

  5. 使用Python批量修改数据库执行Sql文件

    由于上篇文章中批量修改了文件,有的时候数据库也需要批量修改一下,之前的做法是使用宝塔的phpMyAdmin导出一个已经修改好了的sql文件,然后依次去其他数据库里导入,效率不说极低,也算低了,且都是些 ...

  6. 编程方式实现MySQL批量导入sql文件

    有时候需要在本地导入一些stage环境的数据到本地mysql,面对1000+的sql文件(包含表结构和数据,放在同一个文件夹下),使用navicat一个一个导入sql文件显然有点太慢了,于是考虑使用s ...

  7. SQLCMD备忘录:执行文件夹所有Sql文件

    在做性能测试的时候最希望的一件事情是数据自动导入. 一般做法就是写很多SQL文件,通过Bat自动执行所有Sql文件. Bat代码: @ECHO OFF SET SQLCMD="C:\Prog ...

  8. uct框架数据库sql文件导入错误之 sql_mode

    uct框架在导入sql文件时可能会出现一种错误 ERROR 1101 (42000): BLOB/TEXT column 'brief' can't have a default value 这是由于 ...

  9. sql文件批量导入mysql数据库

    有一百多个sql文件肿么破?一行一行地导入数据库肯定是极其愚蠢的做法,但是我差点就这么做了... 网上首先找到的方法是:写一个xxx.sql文件,里边每一行都是source *.sql ...,之后再 ...

随机推荐

  1. asp.net 内部重定向

    1. /* * 2. * Context.RewritePath() * 使用给定路径重写 URL.(内部重写) * 内部请求重写 */ public static void TestTwo() { ...

  2. Android——ExpandableListView事件拦截

    1.满足条件 如果使用ExpandableListView,需要子item响应一个事件,比如重新启动一个新的activity,需要满足下面的条件: (1).修改Adapter返回值 覆写BaseExp ...

  3. 获取UILabel上最后一个字符串的位置。获取文字长度和高度,自动换行

    //行的高度. -(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPat ...

  4. eclipse中默认的提示键

    Alt+/:代码提示 Ctrl+/:注释/取消注释 Ctrl+D:删除光标所在行 Ctrl+K:将光标停留在变量上,按Ctrl+K键可以查找到下一个同样的变量 Shift+Ctrl+K:和Ctrl+K ...

  5. (原)Eclipse的java中文件读写

    1 在<uses-sdk…/>下面添加permission <uses-sdk android:minSdkVersion="16" android:target ...

  6. EasyUI TextBox的keypress

    关于EasyUI TextBox的事件好像不多,像keypress,keydown在textbox的事件里都没有,所以要用这些事件要采取一些特殊的方法,今天用到了这些就记录一下,有两种方法, 第一种: ...

  7. HDU 1172 猜数字(DFS)

    猜数字 Time Limit:10000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Submit Status ...

  8. performance_schema 变量

    performance_schema 这个变量来控制performance_schema库的禁用和启用 1.performance_schema的值说明 performance_schema=1 表示 ...

  9. Eclipse中设置条件断点

    1.在要添加断点的变量那一行前双击,添加断点: 2.在该断点处点击鼠标右键,在弹出的选项卡中选择“断点属性”Breakpoint Properties; 3.在断点属性选项卡中勾选Enabled复选框 ...

  10. MVC 模型绑定

    在WebForm,获取提交表单的值一般都是Request.Form["Title"]这样的方式.在MVC中,提供了模型绑定机制.让后台获取表单或Url中的参数变得更加简单. 一.基 ...