HiveQl语句应用实例:WordCount

    xiaoxiao2022-07-05  169

    具体步骤如下:

    1. 在linux本地创建数据源文件:

    touch hive.txt vi hive.txt

    并随意给上一些内容

    2.将hive.txt数据源文件上传到hdfs的input文件夹中

    hdfs dfs -put hive.txt /input //上传文件 hdfs dfs -ls /input //查看

    3.进入hive shell命令

    hive //并新建一张数据源表t2 create table t1 (line string);

    4.装载数据

    将我们上传到hdfs的hive.txt文件写入到数据源表t2中:

    load data inpath '/input/hive.txt' overwrite into table t2; 将内容写入数据源表

    5.编写HiveQL语句实现wordcount算法,并建表hiveTest保存计算结果:

    create table hiveTest as select word, count(1) as count from (select explode (split (line, ' ')) as word from t2) w group by word order by word;

    完成!

    6.查看wordcount计算结果:

    select * from hiveTest;

    最新回复(0)