当前位置: 首页 > news >正文

【hive】Hive对数据库,对表的操作(一)

 知识点看ppt,以下进行个人瞎编的模拟实践:

要求:创建一个数据库名为 weather 的数据库。

      此时你为了展示你的专业程度,不选择使用 create database weather ,而是为了充分向老板展示专业,选择 if not exists 避免重复建库,用 comment 关键字添加注释说明,用 with dbproperties 指定数据库的属性配置,本来还想用 location 指定数据库在 hdfs 的存储位置,因为默认位置在 /user/hive/warehouse/dbname.db,想了想算了,于是写下:

create database if not exists weather comment "this is tianqi" with dbproperties ('creator' = 'zhangsan','data'='2025-04-17');

要求:向老板展示你的成果。

 于是你输入 describe database extended 用于查看数据库信息的命令。

describe database extended weather;

 要求:检查当前会话是否使用 weather 数据库。

 当你 use weather 准备开始对数据表工作时,面对这无趣的要求,知道这是老板想考验你,你知道 show databases 是查看所有的数据库的你,于是输入 select current_database() 来查看当前正在使用的数据库。

select current_database();

要求:随便创建一个 temp 表

create table temp(id string);

此时你已经想删库跑路了,你知道 drop database weather 只能针对空白数据库,于是你加上关键字  CASCADE 就可以删除带有表的数据库了

drop database if exists weather CASCADE;

删完后老板决定重视你,找你升职加薪,布置新任务了,于是你又把 weather 数据库搞回来。

 要求:创建 temperature 表,相应的字段有 id,year,temperature

 你知道要导入的数据是以逗号分割,于是你果断再后面加上 row format delimited fields terminated by ',';

create table if not exists temperature(id string,year string,temperature int) row format delimited fields terminated by ',';

 要求:备份一份表

你知道  like temperature  可以复制表结构,虽然表里面没有数据,但你还是选择更专业的 as select * from temperature  同时复制结构和数据。甚至不忘在新的表设置分隔符 row format delimited fields terminated by ','

create table temperature_back row format delimited fields terminated by ',' as select * from temperature;

要求:将对应的数据集,station.log 和 temperature.log 导入到表中

 你知道有 本地路径 和 hdfs 路径两种方式通过 hive 的 load data 导入,于是你 temperature.log 使用hdfs路径, station.log 使用本地路径

hadoop fs -mkdir /hive-data
hadoop fs -put ~/hadoop-2.9.2/study-hive-data/* /hive-data
hive
hive> load data inpath '/hive-data/temperature.log' into table temperature;
hive
hive> create table if not exists station(id string,latitude string,longitude string,state string) row format delimited fields terminated by ',';
hive> load data local inpath '/home/hadoop/hadoop-2.9.2/study-hive-data/station.log' overwrite into table station;

最后,你还想说,你还会数据导出和备份与恢复:

数据导出:

使用 INSERT OVERWRITE [LOCAL] DIRECTORY 'filepath' 语句,

把数据从Hive表中导出到本地文件系统或HDFS

备份与恢复:

EXPORT TABLE table_name to 'filepath'

使用 EXPORT 命令,对表中的数据进行备份,表的结构也会一并导出;

相关文章:

  • 第六节:React Hooks进阶篇-自定义Hook设计
  • 大模型时代下全场景数据消费平台的智能BI—Quick BI深度解析
  • 【数字图像处理】图像增强
  • King3399(ubuntu文件系统)GDB/GDBServer调试配置
  • 《Cangjie Magic实战手记:用Agent DSL与MCP协议重构智能物流调度系统》——一次从技术困惑到行业落地的探索之旅
  • 当 AI 有了 “万能插头” 和 “通用语言”:MCP 与 A2A 如何重构智能体生态
  • 【JAVA】在idea新加artifact时,点击Build-Build Artifacts时,新加的artifact不能选中
  • Java NIO Java 虚拟线程(微线程)与 Go 协程的运行原理不同 为何Go 能在低配机器上承接10万 Websocket 协议连接
  • 吊顶上的灯线怎么预留?是提前到位还是后期随意拉拽?
  • AI 驱动下的后端开发架构革命:从智能协同体系
  • golang处理时间的包time一次性全面了解
  • 岚图L3智能架构发布,9大首发新技术引领电动车变革
  • git更新的bug
  • C/C++语言常见问题-智能指针、多态原理
  • ES|QL,知道吗,专为搜索而生 —— 推出评分和语义搜索
  • Elasticsearch的Java客户端库QueryBuilders查询方法大全
  • 【Amazon 工具】在MacOS本地安装 AWS CLI、kubectl、eksctl工具
  • 【Windows上配置Git环境】
  • 关于 AI驱动的智慧家居、智慧城市、智慧交通、智慧医疗和智慧生活 的详细解析,涵盖其定义、核心技术、应用场景、典型案例及未来趋势
  • AI与物联网的深度融合:开启智能生活新时代
  • 探月工程鹊桥二号中继星取得阶段性进展
  • 欧洲观察室|欧盟对华战略或在中欧建交50年时“低开高走”
  • 北美票房|华纳又赢了,《死神来了6》开画远超预期
  • 上海百年龙华码头开启新航线,弥补浦江游览南段空缺
  • 上海位居全球40城科技传播能力第六名
  • 当“小铁人”遇上青浦,看00后如何玩转长三角铁三