林子雨编著《大数据基础编程、实验和案例教程》教材第8章的代码

大数据学习路线图

林子雨编著《大数据基础编程、实验和案例教程》(教材官网)教材中的代码,在纸质教材中的印刷效果不是很好,可能会影响读者对代码的理解,为了方便读者正确理解代码或者直接拷贝代码用于上机实验,这里提供全书配套的所有代码。
查看教材所有章节的代码

第8章 数据仓库Hive的安装和使用

教材第157页

(温馨提示:代码框上方的复制代码按钮,也就是“两张A4纸图标”,用鼠标点击复制代码按钮,就可以把代码框中的代码复制到粘贴板,粘贴到其他地方。但是,有的浏览器可能不支持该功能)

  1. sudo tar -zxvf ./apache-hive-1.2.1-bin.tar.gz -C /usr/local # 解压到/usr/local中
  2. cd /usr/local/
  3. sudo mv apache-hive-1.2.1-bin hive # 将文件夹名改为hive
  4. sudo chown -R hadoop:hadoop hive # 修改文件权限
Shell 命令

教材第158页

  1. vim ~/.bashrc
Shell 命令
export HIVE_HOME=/usr/local/hive
export PATH=$PATH:$HIVE_HOME/bin
  1. source ~/.bashrc
Shell 命令
  1. cd /usr/local/hive/conf
  2. sudo mv hive-default.xml.template hive-default.xml
Shell 命令
  1. cd /usr/local/hive/conf
  2. vim hive-site.xml
Shell 命令
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<?xml-stylesheet type="text/xsl" href="configuration.xsl"?>
<configuration>
  <property>
    <name>javax.jdo.option.ConnectionURL</name>
    <value>jdbc:mysql://localhost:3306/hive?createDatabaseIfNotExist=true</value>
    <description>JDBC connect string for a JDBC metastore</description>
  </property>
  <property>
    <name>javax.jdo.option.ConnectionDriverName</name>
    <value>com.mysql.jdbc.Driver</value>
    <description>Driver class name for a JDBC metastore</description>
  </property>
  <property>
    <name>javax.jdo.option.ConnectionUserName</name>
    <value>hive</value>
    <description>username to use against metastore database</description>
  </property>
  <property>
    <name>javax.jdo.option.ConnectionPassword</name>
    <value>hive</value>
    <description>password to use against metastore database</description>
  </property>
</configuration>

教材第159页

  1. cd ~
  2. tar -zxvf mysql-connector-java-5.1.40.tar.gz #解压
  3. #下面将mysql-connector-java-5.1.40-bin.jar拷贝到/usr/local/hive/lib目录下
  4. cp mysql-connector-java-5.1.40/mysql-connector-java-5.1.40-bin.jar /usr/local/hive/lib
Shell 命令

教材第160页

  1. service mysql start #启动MySQL服务
  2. mysql -u root -p #登录MySQL数据库
Shell 命令
  1. mysql> create database hive;
mysql
  1. mysql> grant all on *.* to hive@localhost identified by 'hive';
  2. mysql> flush privileges;
mysql
  1. cd /usr/local/hadoop
  2. ./sbin/start-dfs.sh
Shell 命令
  1. cd /usr/local/hive
  2. ./bin/hive
Shell 命令
  1. hive
Shell 命令

教材第161页

  1. schematool -dbType mysql -initSchema
Shell 命令

教材第162页

  1. hive> create database hive;
hive
  1. hive> create database if not exists hive;
hive
  1. hive> use hive;
  2. hive>create table if not exists usr(id bigint,name string,age int);
hive
  1. hive>create table if not exists hive.usr(id bigint,name string,age int)
  2. >location ‘/usr/local/hive/warehouse/hive/usr’;
hive
  1. hive>create external table if not exists hive.usr(id bigint,name string,age int)
  2. >row format delimited fields terminated by ','
  3. >location ‘/usr/local/data’;
hive

教材第163页

  1. hive>create table hive.usr(id bigint,name string,age int) partition by(sex boolean);
hive
  1. hive> use hive;
  2. hive> create table if not exists usr1 like usr;
hive
  1. hive>create view little_usr as select id,age from usr;
hive
  1. hive> drop database hive;
hive
  1. hive>drop database if exists hive;
hive
  1. hive> drop database if exists hive cascade;
hive
  1. hive> drop table if exists usr;
hive
  1. hive> drop view if exists little_usr;
hive

教材第164页

  1. hive> alter database hive set dbproperties(‘edited-by’=’lily’);
hive
  1. hive> alter table usr rename to user;
hive
  1. hive> alter table usr add if not exists partition(age=10);
  2. hive> alter table usr add if not exists partition(age=20);
hive
  1. hive> alter table usr drop if exists partition(age=10);
hive
  1. hive>alter table usr change name username string after age;
hive
  1. hive>alter table usr add columns(sex boolean);
hive
  1. hive>alter table usr replace columns(newid bigint,newname string,newage int);
hive
  1. hive> alter table usr set tblproperties(‘notes’=’the columns in usr may be null except id’);
hive

教材第165页

  1. hive> alter view little_usr set tblproperties(‘create_at’=’refer to timestamp’);
hive
  1. hive> show databases;
hive
  1. hive>show databases like h.*’;
hive
  1. hive> use hive;
  2. hive> show tables;
hive
  1. hive> show tables in hive like u.*’;
hive
  1. hive> describe database hive;
hive
  1. hive>describe database extended hive;
hive
  1. hive> describe hive.usr;
  2. hive> describe hive.little_usr;
hive

教材第166页

  1. hive> describe extended hive.usr;
  2. hive> describe extended hive.little_usr;
hive
  1. hive> describe extended hive.usr.id;
hive
  1. hive> load data local inpath ‘/usr/local/data overwrite into table usr;
hive
  1. hive> load data local inpath ‘/usr/local/data into table usr;
hive
  1. hive> load data inpath hdfs://master_server/usr/local/data’ overwrite into table usr;
hive
  1. hive> insert overwrite table usr1
  2. > select * from usr where age=10;
hive
  1. hive> insert into table usr1
  2. > select * from usr where age=10;
hive

教材第167页

  1. cd /usr/local/hadoop
  2. mkdir input
Shell 命令
  1. cd /usr/local/hadoop/input
  2. echo "hello world" > file1.txt
  3. echo "hello hadoop" > file2.txt
Shell 命令
  1. hive
Shell 命令
  1. hive> create table docs(line string);
  2. hive> load data inpath 'input' overwrite into table docs;
  3. hive>create table word_count as
  4. >select word, count(1) as count from
  5. >(select explode(split(line,' '))as word from docs) w
  6. >group by word
  7. >order by word;
hive