1. 啟動metastore服務
<code> ./hive --service metastore &/<code>
2. 建表
創建一個行表,用於存儲foobar.txt文件中的每行句子。
<code>create table tbl_line(line string) row format delimited fields terminated by '\\n';/<code>
3. 加載數據
將文件數據加載到hive表中。
<code>echo "Hadoop Common\\nHadoop Distributed File System\\nHadoop YARN\\nHadoop MapReduce " > /tmp/foobar.txt/<code>
<code>hive> load data local inpath '/tmp/foobar.txt' into table tbl_line;/<code>
加載的數據會放到Hadoop中/data/hive/warehouse/test.db目錄下,/data/hive/warehouse是hive-site.xml配置的hive.metastore.warehouse.dir值, test是數據庫名稱, tbl_line是表名。
4. HQL
根據MapReduce方式我們需要將每行句子拆分成獨立的單詞,然後對單詞彙總。
split(字符串,分割符) 函數:用於分割字符串, 返回一個數組explode(數組)函數:將數組中的每個元素展開成列<code>hive> select split("hello world", " ") from tbl_line limit 1;OK["hello","world"]hive> select * from tbl_line;OKHadoop CommonHadoop Distributed File SystemHadoop YARNHadoop MapReduce# 將每行句子分割成每個單詞數組hive> select split(line, " ") from tbl_line;OK["Hadoop","Common"]["Hadoop","Distributed","File","System"]["Hadoop","YARN"]["Hadoop","MapReduce",""]hive> select explode(split(line, " ")) from tbl_line;OKHadoopCommonHadoopDistributedFileSystemHadoopYARNHadoopMapReduce/<code>
<code># 創建一個單詞表hive> create table tbl_word(word string);# 將每一行句子拆分成每個單詞插入到表中hive> insert into table tbl_word select explode(split(line, " ")) as word from tbl_line;hive> select * from tbl_word;OKHadoopCommonHadoopDistributedFileSystemHadoopYARNHadoopMapReducehive> select word, count(*) as count from tbl_word group by word order by count desc;/<code>