首页 > 数据库 >大型数据库应用技术课堂测试05

大型数据库应用技术课堂测试05

时间:2022-10-18 22:59:26浏览次数:50  
标签:String 05 ip 数据库 traffic 课堂 type id string

题目:

Result文件数据说明:

Ip:106.39.41.166,(城市)

Date:10/Nov/2016:00:01:02 +0800,(日期)

Day:10,(天数)

Traffic: 54 ,(流量)

Type: video,(类型:视频video或文章article)

Id: 8701(视频或者文章的id)

1、 数据清洗:按照进行数据清洗,并将清洗后的数据导入hive数据库中。

两阶段数据清洗:

(1)第一阶段:把需要的信息从原始日志中提取出来

ip:    199.30.25.88

time:  10/Nov/2016:00:01:03 +0800

traffic:  62

文章: article/11325

视频: video/3235

(2)第二阶段:根据提取出来的信息做精细化操作

ip--->城市 city(IP)

date--> time:2016-11-10 00:01:03

day: 10

traffic:62

type:article/video

id:11325

(3)hive数据库表结构:

create table data(  ip string,  time string , day string, traffic bigint,

type string, id   string )

2、数据分析:在HIVE统计下列数据。

(1)统计最受欢迎的视频/文章的Top10访问次数 (video/article)

(2)按照地市统计最受欢迎的Top10课程 (ip)

(3)按照流量统计最受欢迎的Top10课程 (traffic)

步骤1:

由于不会使用MySQL格式化数据格式所以使用Java程序将数据提出清洗存入Arraylist再导入新表。

import java.sql.*;
import java.util.ArrayList;

public class test002 {
    public static void main(String[] args) {
        try {
            Class.forName("com.mysql.jdbc.Driver");
        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        }
        Connection conn=null;
        try {
            conn= DriverManager.getConnection("jdbc:mysql://localhost:3306/test03","root","zber1574");
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        String sql="select * from data";
        try {
            ArrayList<beanlist> temp=new ArrayList<beanlist>();
            PreparedStatement pt=conn.prepareStatement(sql);
            ResultSet rs=pt.executeQuery();
            while(rs.next()){
                String c=rs.getString("ip");
                String time=rs.getString("time");
                String day=rs.getString("day");
                String traffic=rs.getString("traffic");
                String type=rs.getString("type");
                String id=rs.getString("id");
                String[]a=c.split("\\.");
                String ip=""+a[0]+"."+a[1];
                beanlist temp1;
                temp1 = new beanlist(ip,time,day,traffic,type,id);
                temp.add(temp1);
            }
            add a=new add();
            for(int i=0;i<temp.size();i++){
                String sql1="INSERT INTO data2 (ip,time,day,traffic,type,id) VALUES('"+temp.get(i).getIp()+"','"+temp.get(i).getTime()+"','"+temp.get(i).getDay()+"',"+temp.get(i).getTraffic()+",'"+temp.get(i).getType()+"','"+temp.get(i).getId()+"')";
                a.insert(sql1);
                System.out.println(i);
            }

        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }
}


import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;

public class add {
    public void insert(String sql) throws SQLException {
        try {
            Class.forName("com.mysql.jdbc.Driver");
        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        }
        Connection conn=null;
        try {
            conn= DriverManager.getConnection("jdbc:mysql://localhost:3306/test03","root","zber1574");
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        PreparedStatement pt= null;
        try {
            pt = conn.prepareStatement(sql);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        pt=conn.prepareStatement(sql);
        int a=pt.executeUpdate();
        pt.close();
        conn.close();
    }
}

以上就是数据库的提取解析重构再导入

 

ps:此时从数据库导出的.csv文件可能会是带有""的

 

 会影响hive的正常读取

之后将csv文件通过xftp传入虚拟机

打开到hive

创建表

create table city(
  ip             string  comment "城市"
  , c_date        string  comment "日期"
  , c_day     string  comment "天数"
  , traffic         string  comment "流量"
  , type          string  comment "类型"
  ,Id string comment "id"
)
row format delimited fields terminated by ',';

将文件导入表

hadoop fs -put /opt/software/city.csv /user/hive/warehouse/city

 题目一表

create table type10(
id String,
total String
)ROW format delimited fields terminated by ',' STORED AS TEXTFILE;



insert into type10 select id, count(*) as total from result1 group by id order by total desc limit 10;
create table if not exists tb_ip
comment "ip"
as
select
    ip,
    type,
    id,
    count(*) as paixu from city1 group by ip,type,id
order by paixu desc
limit 10;
create table if not exists tb_traffic
comment "traffic"
as
select
    type,
    id,
    sum(traffic) as liuliang from city1
                             group by type,id
order by liuliang desc
limit 10;

最后用fineUI可视化就完成了

标签:String,05,ip,数据库,traffic,课堂,type,id,string
From: https://www.cnblogs.com/Rebz/p/16804499.html

相关文章

  • pc wx数据库密钥快速定位
    ......
  • 数据库DTS迁移工具
    迁移工具DTS操作方案​第一章从DM8迁移到DM8​1打开迁移DTS工具​1.安装了达梦客户端里面有数据迁移工具,点开,新建迁移工程:​输入工程名称,比如dm8到dm8迁移​工程创建好以后......
  • 小米运动健康及数据库分析
    数据库分析对部分数据库中的数据进行分析device_db在device表中,会储存配对的设备的信息如上图所示,连接的设备为Redmi手表2,它的设备id是513932244,mac地址44:27:F3:......
  • plsql连接oracle数据库常见的报错
    1、ORA-00001:违反唯一约束条件的解决办法(sql重复执行)2、ORA-01427问题的分析和解决(sql语句有错误)3、Oracle报ORA-00942:表或视图不存在的解决方法(用sql重新查找表是否存......
  • 星起航跨境——亚马逊云计算为各大领域提供创新技术突破数据库瓶颈
    在云计算尚未问世的很长时间里,几乎所有的开发人员都只会使用传统的商务数据库。但是,由于互联网技术的飞速发展,用户数量不断增加,业务也呈现出了复杂性的趋势变化,而云计算与传......
  • AI智能视频融合平台EasyCVR如何通过数据库批量修改tcp/udp播放协议?
    EasyCVR视频融合云平台基于云边端一体化架构,兼容性高、拓展性强,可支持多类型设备、多协议方式接入,包括国标GB/T28181、RTMP、RTSP/Onvif协议,以及厂家的私有协议,如:海康Ehome......
  • mysql数据库整理之修改数据
    1.对数据进行更新操作数据在原有的基础上修改格式:update表名set列1=值1,列2=值2where条件where条件:数据中的唯一性例如:--修改智能手机,价格上调到15......
  • 每日习字|2020-05-25
    不堪盈手赠,还寝梦佳期海上生明月,天涯共此时。故人西辞黄鹤楼,烟花三月下扬州。孤帆远影碧空尽,唯见长江天际流。月落乌啼霜满天,江枫渔火对愁眠。姑苏城外寒山寺,......
  • 560数据库连接池_c3p0_基本使用and 561数据库连接池_c3p0_配置演示
    数据库连接池_c3p0_基本使用(1)步骤导入两个jar包c3po-o.9.5.2.jar   mchange-commons-java-o.2.12.jar还需要导入数据库的驱动jar包mysql-connector-java-5.1.47......
  • PostgreSQL 数据库开发规范
    背景PostgreSQL的功能非常强大,但是要把PostgreSQL用好,开发人员是非常关键的。下面将针对PostgreSQL数据库原理与特性,输出一份开发规范,希望可以减少大家在使用PostgreSQL......