HiveSQl使用

show databases;


use date;


show tables;

drop table networkqualityinfo;

--   创建networkqualityinfo数据表
create external table networkqualityinfo
(
    id                 INT,
    ping               STRING,
    ave_downloadSpeed  STRING,
    max_downloadSpeed  STRING,
    ave_uploadSpeed    STRING,
    max_uploadSpeed    STRING,
    rssi               STRING,
    gps_lat            STRING,
    gps_lon            STRING,
    location_type      STRING,
    imei               STRING,
    server_url         STRING,
    ant_version        STRING,
    detail             STRING,
    time_client_test   STRING,
    time_server_insert STRING,
    networkType        STRING,
    operator_name      STRING,
    wifi_bss_id        STRING,
    cell_id            STRING,
    province           STRING,
    city               STRING,
    mobile_type        STRING,
    street             STRING,
    location_detail    STRING,
    upload_traffle     STRING,
    download_traffic   STRING
)
    PARTITIONED BY (yyyy STRING,mm STRING,dd STRING) ROW FORMAT DELIMITED FIELDS TERMINATED BY '\t';

select *
from networkqualityinfo;


show databases;


select *
from app_traffic;


select *
from network;

show databases ;
show tables ;
--                                                                 练习题
create database viuser;

use viuser;

drop table  video_ori;
drop table  video_user_ori;


create database video;
use video;
show tables ;

create table video_ori(
videoId string,
uploader string,
age string,
category string,
length string,
views string,
rate string,
ratings string,
comments string,
relatedId string)
row format delimited
fields terminated by ":"
stored as textfile;


create table video_user_ori(
uploader string,
videos string,
friends string)
row format delimited
fields terminated by ","
stored as textfile;

create table video_orc(
videoId string,
uploader string,
age string,
category string,
length string,
views string,
rate string,
ratings string,
comments string,
relatedId string)
row format delimited
fields terminated by ":"
stored as ORC;

create table video_user_orc(
uploader string,
videos string,
friends string)
row format delimited
fields terminated by ","
stored as ORC;

show tables ;

-- 加载数据
load data local inpath '/opt/bao/part-r-00000' OVERWRITE  into table video_ori;

select *
from video.video_ori;

load data local inpath '/opt/bao/user.txt' into table video_user_ori;

select *
from video.video_user_ori;

//从原始表查出数据  添加到 ori 里面
insert into table video_orc select * from video_ori;

select *
from video.video_orc;

insert into table video_user_orc select * from video_user_ori;

select *
from video.video_user_orc;

-- 3.数据的分析阶段  从视频表中统计出视频评分为5分的视频信息,把查询结果保存到/export/rate.txt
--  #!  bin/bash
-- hive -e "select * from video.video_orc where rate=5 " > /opt/bao/rate.txt

-- 从视频表中统计出评论数大于100条的视频信息,把查询结果保存到   /export/comments.txt
--  hive -e "select * from video.video_orc where comments >100 " > /opt/bao/comments.txt


-- 创建外部表
show databases ;

use video;

show tables ;

 create external  table rate(
    videoId string,
    uploader string,
    age string,
    category string,
    length string,
    views string,
    rate string,
    ratings string,
    comments string,
    relatedId string)
row format delimited
fields terminated by "\t"
stored as textfile;

-- 创建comments外部表的语句:
   create external  table comments(
    videoId string,
    uploader string,
    age string,
    category string,
    length string,
    views string,
    rate string,
    ratings string,
    comments string,
    relatedId string)
row format delimited
fields terminated by "\t"
stored as textfile;


-- 数据加载语句
load data local inpath '/opt/bao/rate.txt' into table rate;
load data local inpath '/opt/bao/comments.txt' into table comments;


--创建  HBase 映射    rate
create table video.hbase_rate(
  videoId string,
    uploader string,
    age string,
    category string,
    length string,
    views string,
    rate string,
    ratings string,
    comments string,
    relatedId string)
stored by 'org.apache.hadoop.hive.hbase.HBaseStorageHandler'
with serdeproperties("hbase.columns.mapping" = "cf:uploader,cf:age,cf:category,cf:length,cf:views,cf:rate,cf:ratings,cf:comments,cf:relatedId")
tblproperties("hbase.table.name" = "hbase_rate");


--创建  HBase 映射    comments

create table video.hbase_comments(
  videoId string,
    uploader string,
    age string,
    category string,
    length string,
    views string,
    rate string,
    ratings string,
    comments string,
    relatedId string)
stored by 'org.apache.hadoop.hive.hbase.HBaseStorageHandler'
with serdeproperties("hbase.columns.mapping" = "cf:uploader,cf:age,cf:category,cf:length,cf:views,cf:rate,cf:ratings,cf:comments,cf:relatedId")
tblproperties("hbase.table.name" = "hbase_comments");

select *
from comments;

select *
from hbase_rate;

drop table hbase_rate;

drop database  viuser;


select *
from rate;

--   插入数据
insert into table hbase_rate select * from rate;


insert into table hbase_comments select * from comments;

select *
from hbase_comments;

-- 创建video_ori视频表:
create table video_ori
(
    videoId   string,
    uploader  string,
    age       int,
    category  array<string>,
    length    int,
    views     int,
    rate      float,
    ratings   int,
    comments  int,
    relatedId array<string>
)
    row format delimited
        fields terminated by ":"
        collection items terminated by ","
    stored as textfile;

-- scan 'hbase_rate', { => [2gEDTaLUqEg, 3jT4cGkld8s]}


select *
from video_ori;

select *
from video_user_ori;


-- 创建video_user_ori用户表:
create table video_user_ori
(
    uploader string,
    videos   int,
    friends  int
)
    row format delimited
        fields terminated by ","
    stored as textfile;


-- 请写出ORC格式的建表语句:
-- 创建video_orc表
create table video_orc
(
    videoId   string,
    uploader  string,
    age       int,
    category  array<string>,
    length    int,
    views     int,
    rate      float,
    ratings   int,
    comments  int,
    relatedId array<string>
)
    row format delimited
        fields terminated by ":"
        collection items terminated by ","
    stored as ORC;


-- 创建 video_user_orc 表:
create table video_user_orc
(
    uploader string,
    videos   int,
    friends  int
)
    row format delimited
        fields terminated by ","
    stored as ORC;

--                                                                   练习题 导入 数据

-- 请写出导入语句 :   video_ori:

--  #!  bin/bash
-- hive -e"
use viuser;

LOAD DATA LOCAL INPATH '/videori/video.txt'
    OVERWRITE INTO TABLE video_ori;

--    "


-- 请写出导入语句 :   video_user_ori:

--  #!  bin/bash
-- hive -e"
use viuser;
LOAD DATA LOCAL INPATH '/videori/user.txt'
    OVERWRITE INTO TABLE video_user_ori;
--    "


--   从原始表查询数据并插入对应的ORC表中             请写出插入语句:

insert overwrite table course.video_user_orc
select *
from course.video_user_ori;


insert overwrite table course.video_orc
select *
from course.video_ori;

-- 3.1从视频表中统计出视频评分为5分的视频信息,把查询结果保存到/export/rate.txt

--   第一种方式      脚本儿

--  #!  bin/bash
use viuser;
-- hive -e  "
-- "select * from video_ori  where rate =5 " >  /export/rate.txt

-- "


--  第二种方式     代码`
insert overwrite local directory "/export/rate.txt"
select *
from video_ori
where rate = 5;


-- 3.2从视频表中统计出评论数大于100条的视频信息,把查询结果保存到    /export/comments.txt

--   第一种方式      脚本儿
--  #!  bin/bash
use viuser;
-- hive -e  "
-- "select * from video_ori  where comments >100 " >  /export/comments.txt
-- "

--  第二种方式     代码`
insert overwrite local directory "/export/comments.txt"
select *
from video_ori
where comments > 100;

--                                                                     4.1创建hive对应的数据库外部表
-- 请写出创建rate外部表的语句:

--  create EXTERNAL table tableName(字段名称 字段类型,字段名称 字段类型)

create EXTERNAL table rate
(
    videoId  string,
    uploader string,
    age      int,
    category array<string>,
    length   int,
    views    int,
    rate     float,
    ratings  int,
    comments int
);

-- 请写出创建 comments 外部表的语句:

create EXTERNAL table comments
(
    videoId  string,
    uploader string,
    age      int,
    category array<string>,
    length   int,
    views    int,
    rate     float,
    ratings  int,
    comments int
);

-- 加载第3步的结果数据到外部表中

load data local inpath '/export/rate.txt' overwrite into table rate;

load data local inpath '/export/comments.txt' overwrite into table comments;

-- 创建hive管理表与HBase进行映射

-- Hive中的    rate    ,   comments   两个表分别对应hbase中的      hbase_rate   ,    hbase_comments   两个表


-- 创建hbase_rate表并进行映射:
create table course.hbase_rate( videoId  string,
    uploader string,
    age      int,
    category array<string>,
    length   int,
    views    int,
    rate     float,
    ratings  int,
    comments int)
stored by 'org.apache.hadoop.hive.hbase.HBaseStorageHandler'
with serdeproperties("hbase.columns.mapping" = "cf:name,cf:score")
tblproperties("hbase.table.name" = "rate");

-- 通过insert  overwrite select  插入数据
insert overwrite table course.hbase_rate select * from course.rate;

-- 请写出通过insert overwrite select,插入hbase_rate表的语句
-- 创建hbase_comments表并进行映射:

create table course.hbase_comments( videoId  string,
    uploader string,
    age      int,
    category array<string>,
    length   int,
    views    int,
    rate     float,
    ratings  int,
    comments int)
stored by 'org.apache.hadoop.hive.hbase.HBaseStorageHandler'
with serdeproperties("hbase.columns.mapping" = "cf:name,cf:score")
tblproperties("hbase.table.name" = "comments");


-- 请写出通过insert overwrite select,插入hbase_comments表的语句
-- 通过insert  overwrite select  插入数据

insert overwrite table course.hbase_rate select * from course.comments;

-- scan   'hbase_comments'


--
-- 5.通过hbaseapi进行查询操作
-- 5.1请使用hbaseapi    对hbase_rate表,按照通过  startRowKey=1   和    endRowKey=100   进行扫描查询出结果。
-- 5.2请使用hbaseapi对hbase_comments表,只查询comments列的值。

show databases;

use date;

show tables;


-- drop table networkqualityinfo;

select *
from networkqualityinfo;


-- 创建app_traffic数据表

create external table app_traffic
(
    id               INT,
    package_name     STRING,
    app_name         STRING,
    uid              STRING,
    network_type     STRING,
    mobile_type      STRING,
    cell_id          STRING,
    wifi_bssid       STRING,
    start_time       STRING,
    end_time         STRING,
    upload_traffic   STRING,
    download_traffic STRING,
    date             STRING,
    time_index       STRING,
    imei             STRING,
    sdk_version      STRING,
    user_lat         STRING,
    user_lon         STRING,
    location_type    STRING
) PARTITIONED BY (DS STRING,DT STRING) ROW FORMAT DELIMITED FIELDS TERMINATED BY '\t';

show tables;

-- 创建cell_strength数据表

create external table cell_strength
(
    id                 INT,
    network_id         STRING,
    network_type       STRING,
    gsm_strength       STRING,
    cdma_dbm           STRING,
    evdo_dbm           STRING,
    gsm_bit_errorrate  STRING,
    cdma_ecio          STRING,
    evdo_ecio          STRING,
    user_lat           STRING,
    user_lon           STRING,
    user_location_info STRING,
    bs_lat             STRING,
    bs_lon             STRING,
    time_index         STRING,
    imei               STRING
) PARTITIONED BY (DS STRING,DT STRING) ROW FORMAT DELIMITED FIELDS TERMINATED BY '\t';

-- 创建data_connection数据表

create external table data_connection
(
    id                  INT,
    imei                STRING,
    network_type        STRING,
    wifi_bssid          STRING,
    wifi_state          STRING,
    wifi_rssi           STRING,
    mobile_state        STRING,
    mobile_network_type STRING,
    network_id          STRING,
    gsm_strength        STRING,
    cdma_dbm            STRING,
    evdo_dbm            STRING,
    internal_ip         STRING,
    web_url             STRING,
    ping_value          STRING,
    user_lat            STRING,
    user_lon            STRING,
    user_location_info  STRING,
    bs_lat              STRING,
    bs_lon              STRING,
    time_index_client   STRING,
    version             STRING,
    time_server_insert  STRING
) PARTITIONED BY (DS STRING,DT STRING) ROW FORMAT DELIMITED FIELDS TERMINATED BY '\t';


-- 创建device数据表
create external table device
(
    id           INT,
    imei         STRING,
    company      STRING,
    model        STRING,
    os           STRING,
    os_version   STRING,
    sdk_version  STRING,
    cpu          STRING,
    total_memory STRING,
    free_memory  STRING,
    display      STRING,
    time_first   STRING,
    time_last    STRING
) PARTITIONED BY (DS STRING,DT STRING) ROW FORMAT DELIMITED FIELDS TERMINATED BY '\t';


-- 创建network数据表

create external table network
(
    network_id   INT,
    network_name STRING
) ROW FORMAT DELIMITED FIELDS TERMINATED BY '\t';

show tables;


-- 加载数据表app_traffic数据

LOAD DATA INPATH '/HistoryDatas/app_traffic_data.sql' OVERWRITE INTO TABLE app_traffic PARTITION (DS = 'local',DT = '2017-2018');

select *
from app_traffic;


-- 加载数据表cell_strength数据
LOAD DATA INPATH '/HistoryDatas/cell_strength_data.sql' OVERWRITE INTO TABLE cell_strength PARTITION (DS = 'local',DT = '2017-2018');
select *
from cell_strength;

-- 加载数据表data_connection数据
LOAD DATA INPATH '/HistoryDatas/dataconnection.sql' OVERWRITE INTO TABLE data_connection PARTITION (DS = 'local',DT = '2017-2018');

select *
from data_connection;


-- 加载数据表device数据
LOAD DATA INPATH '/HistoryDatas/device_db.sql' OVERWRITE INTO TABLE device PARTITION (DS = 'local',DT = '2017-2018');

select *
from device;

-- 加载数据表network数据
LOAD DATA INPATH '/HistoryDatas/networkid_name.sql' INTO TABLE network;


-- select  * from telecom;


show tables;

-- CREATE TABLE `tb_user` (
--   `id` int(9) NOT NULL AUTO_INCREMENT,
--   `uname` varchar(20) NOT NULL,
--   `upassword` varchar(20) NOT NULL,
--   `role` varchar(20) NOT NULL,
--   `NWOperator` varchar(20) DEFAULT NULL,
--   `region` varchar(20) NOT NULL,
--   PRIMARY KEY (`id`),
--   UNIQUE KEY `uname` (`uname`),
--   UNIQUE KEY `uname_2` (`uname`)
-- ) ENGINE=MyISAM AUTO_INCREMENT=9 DEFAULT CHARSET=utf8;


 

发布了177 篇原创文章 · 获赞 288 · 访问量 25万+

猜你喜欢

转载自blog.csdn.net/bbvjx1314/article/details/103852108