Hadoop常用命令及范例

  hadoop中的zookeeper,hdfs,以及hive,hbase都是hadoop的组件,要学会熟练掌握相关的命令及其使用规则,下面就是一些常用命令及对hbase和hive的操作语句,同时也列出了一些范例。

  start-dfs.sh

  NameNode 进程启动:hadoop-daemon.sh start namenode

  DataNode 进程启动:hadoop-daemon.sh start datanode

  HA 高可用环境中需要启动的进程:

  zookeeper:

  zkServer.sh start 启动

  zkServer.sh stop 停止

  zkServer.sh status 查看状态 leader follwer

  journalnode 集群命令

  hadoop-daemon.sh start journalnode 启动

  hadoop-daemon.sh stop journalnode 停止

  ZKFC

  启动 zkfc 进程: hadoop-daemon.sh start zkfc

  停止 zkfc 进程: hadoop-daemon.sh stop zkfc

  1. shell命令 管 理和 HDFS 的文件管 理。

  (1)启动 Zookeeper

  zkServer.sh start

  (2)启动 HDFS 的命令

  start-dfs.sh

  (3)启动 Yarn 的命令

  start-yarn.sh

  (4)显示 HDFS 中/data/test 目录信息

  hadoop fs -mkdir /data/test

  hadoop fs -lsr /data/test

  (5)将本地文件/tmp/log.txt 文件上传到/data/test 目录中

  hadoop fs -put /tmp/log.txt /data/test

  (6)设置文件/data/test/log.txt 的副本数为 3

  hadoop fs -setrep -w 3 /data/test/log.txt

  (7)显示/data/test/log.txt 文件内容

  hadoop fs -cat /data/test/log.txt

  (8)将/data/test/log.txt 文件移动到集群/user/hadoop 目录下

  hadoop fs -mkdir /user/hadoop

  hadoop fs -mv /data/test/log.txt /user/hadoop

  (9)将/data/test/log.txt 文件下载到/home/hadoop 目录下

  hadoop fs -copyToLocal /data/test/log.txt /home/hadoop/

  (10)关闭 HDFS 命令

  stop-dfs.sh

  (11)停止 Zookeeper

  zkServer.sh stop

  2.将学生数据存储到 Hive 数据仓库中,信息包括(学号,姓名,性别,年龄,联系方式,

  邮箱),创建表语法如下:

  启动 hive 前切记要先启动 mysql 数据库

  create table student(sno string ,name string ,sex string ,age int ,phone string, email string) row

  format delimited fields terminated by ',' ;(1)将本地数据“/tmp/student.dat”加载到 student 表,写出操作语句

  load data local inpath '/tmp/student.dat' overwrite into table student;

  (2)写 HQL 语句查询全部学生信息

  select * from student;

  (3)写 HQL 语句查询各个年龄及对应学生数量

  (4) select age,count(*) from student group by age;

  (5)写 HQL 语句查询全部学生的姓名和性别

  select name,sex from student;

  (6)写 HQL 语句查询年龄为 18 的学生姓名和联系方式

  select name,phone from student where age=18;

  (7)写 HQL 语句查看 student 表结构

  describe student;

  (8)写 HQL 语句删除 student 表

  drop table student;

  (9)导出 生 地/home/hadoop/out 目录 ,写出 语

  from student insert overwrite local directory '/home/hadoop/out' select *;

  3.员工表 employee 包含两个列族 basic 和 info,使用 shell 命令完成以下操作。

  (1)启动 HBase 集群

  注意:在启动 hbase 集群前,需要将 zookeeper,hdfs,yarn 及历史服务器启动

  zkServer.sh start

  start-dfs.sh

  start-yarn.sh

  mr-jobhistory-daemon.sh start historyserver

  启动 hbase 集群

  start-hbase.sh

  hbase shell

  (2)在 HBase 中创建 employee 表

  create 'employee','basic','info'

  (3)一个员工行键为“19006”,basic 列族有 sex 列,值为“男”,添加该值

  put 'employee','19006','basic:sex','nan'

  (4)获取行键为“19002”的员工的性别信息

  get 'employee','19006','basic:sex'

  (5)获取行键为“19002”的员工的 info 列族所有数据

  get 'employee','19006','info'

  (6)查询所有员工的数据

  scan 'employee'

  (7)统计 employee 表的行数

  count 'employee'

  (8)关闭 HBase 集群

  stop-hbase.sh

  ********

  (9)单独启动和关闭 hadoop 服务(10) 启动名称节点

  (11) hadoop-daemon.sh start namenode

  (12) 启动数据节点

  (13)hadoop-daemons.sh start datanode slave

  (14)开启 resourcemanager

  (15)yarn-daemon.sh start resourcemanager

  (16)停止一个数据节点

  (17)hadoop-daemon.sh stop datanode

  (18)重新开启

  (19)hadoop-daemon.sh start datanode

  HBase基本语法及示例: (1)建数据表,包含两个列族,第一个列族保留版本为 2,第二个列族保留版本为 1;

  create 'stu',{NAME=>'info',VERSIONS=>2},{NAME=>'grade'}

  (2) put 添加 , 一 一 识 message;

  put 'stu','zhangsan','info:message','16jiruangongyiban'

  put 'stu','zhangsan','grade:c++','90'

  (3)使用 get 和 scan 查询数据。

  get 'stu' ,'zhangsan', 'info'

  get 'stu', 'zhangsan',{COLUMNS=>'info:message',VERSIONS=>2}

  scan 'stu'

  Hbase 语法总结:

  注意,在向 hbase 的表中添加数据的时候只能一列一列的添加

  添加数据:

  put '行键' ,'列族名:列名','数值'

  获取数据:

  get '表名','行键'

  get '表名','行键','列族名'

  get '表名','行键','列族名:列名'

  get '表名','行键',{COLUMNS=>'列族名:列名',VERSIONS=>版本}

  扫描数据:

  scan '表名'

  scan '表名' {COLUMNS=>'列族名'}

  scan '表名' {COLUMNS=>'列族名:列名'}

  scan '表名' {COLUMNS=>'列族名:列名',VERSIONS=>版本}

  修改数据表:

  alter '表名',{NAME=>'已经存在的列族名',VERSIONS=>版本}

  清除数据表:

  truncate '表名'

  disable '表名' drop '表名'

  Hive 示例: 使用 DDL 完成数据定义

  按照下面学生信息创建学生表和标准身高体重表并加载数据,分别创建内部表、外部表、分区表和桶表,其中分区依据是专业名称,桶表分为 3 个桶。学生信息实例:

  学号 姓名 性别 爱好 年龄 学习成绩 通讯地址

  实验内容 140010101 张三 男 足球,篮球,长跑等 19 C 程序:90,Java:85,DB:88 河南,郑州,大

  学 路 60 号,450001

  内部表:

  create table stu(sno int,sname string,sex string,hobby array,age int,grade

  map,address struct)row format

  delimited fields terminated by ' ' collection items terminated by ',' map keys terminated by':';

  加载数据:无锡妇科医院哪家好 http://mobile.xasgyy.net/

  load data local inpath 'kk.txt' overwrite into table stu;

  分区表:

  create table stupartition(sno int,sname string,sex string,hobby array,age int,grade

  map,address struct)partitioned

  by (major string)row format delimited fields terminated by ' ' collection items terminated by ','

  map keys terminated by':';

  加载数据:

  load data local inpath 'kk.txt' overwrite into table stupartition partition(major='jsj');

  桶表:

  create table stubucket(sno int,sname string,sex string,hobby array,age int,grade

  map,address struct)clustered by

  (age) into 3 buckets row format delimited fields terminated by ' ' collection items terminated by

  ',' map keys terminated by':';

  设置使用分桶属性:set hive.enforce.bucketing=true;

  加载数据:

  load data local inpath 'kk.txt' overwrite into table stubucket;

  查看数据存储位置可以看到桶表中的数据

  hadoop fs -ls /user/hive/warehouse

  外部表:

  create external table stuexternal(sno int,sname string,sex string,hobby array,age

  int,grade map,address

  struct)row format delimited fields

  terminated by ' ' collection items terminated by ',' map keys terminated by':' location

  '/user/hive/external/stuexternal';

  加载数据:

  load data local inpath 'kk.txt' overwrite into table stuexternal;

  标准身高体重实例: 年龄 身高范围 体重范围

  19 160-170 60-65

  身高体重内部表:

  create table shengao(age int,highth struct,weighth struct)row

  format delimited fields terminated by ' ' collection items terminated by '-';'

  加载数据:

  load data local inpath 'shengao.txt' overwrite into table shengao;

  3.使用 DML 完成数据查询

  查询指定学生的第一爱好和通讯邮编;查询指定学生姓名、性别和身高范围;查询有对应身高范围和体重范围的学生信息;查询软件工程专业学生总人数;查询各专业学生的

  平均年龄。

  查询指定学生的第一爱好和通讯邮编;

  select hobby[0],address.postid from stu where sname='aa';

  查询指定学生姓名、性别和身高范围;

  select sex,highth from stu left outer join shengao on stu.age=shengao.age where stu.sname='aa';

  查询有对应身高范围和体重范围的学生信息;

  select * from stu left outer join shengao on stu.age=shengao.age where highth.up=170 and

  highth.down=160 and weighth.max=65 and weighth.min=60;

  查询软件工程专业学生总人数;

  select count(*) from stupartition where major='rjgc';

  查询各专业学生的平均年龄。

  select avg(age) major from stupartition group by(major);

原文地址:https://www.cnblogs.com/djw12333/p/11124672.html