参考:https://www.cnblogs.com/jonban/p/10779938.html

Hive 启动:hive
退出:hive>quit;
show databases;

use  analysis;
show tables;
desc tab_name; --查看表的结构及表的路径
show partitions fact_measured_cft_hive ;展示表分区 fact_measured_cft_hive

show create table fact_five_data_cft; --查看创建表的过程

启动pyspark 

pyspark2 --master local[]   本地运行
==========
HADOOP_CONF_DIR 指hadoop配置文件所在目录
HADOOP_CONF_DIR=/opt/cloudera/parcels/CDH/lib/hadoop/etc/hadoop pyspark2 --master yarn --deploy-mode client 在hadoop yarn上运行pyspark

1、 创建表 create-table.sql

create table if not exists db_hive.tb_user
(
id int,
username string comment '用户名',
age int comment '年龄',
address string comment '地址'
)
comment '用户表'
row format delimited fields terminated by ','
stored as textfile
location '/user/hive/warehouse/db_hive.db/db_user'

2、执行创建表

hive -f 'create-table.sql'

3、加载数据到 tb_user 表中

数据文件  /root/files/tb_user.txt

1001,Logan,16,shenzhen
1002,Herry potter,12,Magic school
1003,孙悟空,500,花果山

Hive交互式命令行执行命令  load data local inpath '/root/files/tb_user.txt' into table db_hive.tb_user;

如下所示:

hive (db_hive)> load data local inpath '/root/files/tb_user.txt' into table db_hive.tb_user;

如果要覆盖旧数据,可以加 overwrite,如下所示

hive (db_hive)> load data local inpath '/root/files/tb_user.txt' overwrite into table db_hive.tb_user;

4、查询数据

hive -e "select id,username from db_hive.tb_user"

5、根据已有表创建只有部分字段的子表

create table if not exists db_hive.tb_user_sub
as
select id,username from db_hive.tb_user;

6、 like 创建表

create table if not exists db_hive.tb_user_like like db_hive.tb_user;

插入数据

insert into table db_hive.tb_user_like select * from db_hive.tb_user;

7、重命名表

alter table tb_user_like rename to tb_user_rename ;

8、 创建外部表,删除时只删除元数据,不会删除表数据

create external table if not exists db_hive.tb_ext(id string);

9、创建分区表

create table if not exists db_hive.tb_logs(
ip string,
text string,
log_time string
)
partitioned by (month string)
row format delimited fields terminated by "\t";

数据文件 /root/files/tb_logs.txt

192.168.32.100  login   20190429072650
192.168.32.100 order 20190429072730
192.168.32.101 browse 20190429072812

载入数据

load data local inpath '/root/files/tb_logs.txt' into table db_hive.tb_logs partition (month = '201904')

查询数据

select ip,text,log_time from tb_logs where month = '201904';

10、手工创建分区数据及修复分区表

创建分区目录

hdfs dfs -mkdir -p /user/hive/warehouse/db_hive.db/tb_logs/month=201905

上传数据文件到分区目录下

hdfs dfs -put /root/files/tb_logs.txt /user/hive/warehouse/db_hive.db/tb_logs/month=201905

此时执行查询

select count(distinct ip) from db_hive.tb_logs where month = '201905';

查询结果为0。

【原因】:数据并未添加到分区中,查看配置的MySQL元数据信息

mysql> use hive_metastore;
mysql> select * from PARTITIONS;

示例配置的Hive元数据存放为MySQL数据库中的 hive_metastore 数据库

查询分区表 PARTITIONS 中的数据,发现只有一条记录,如下所示:

+---------+-------------+------------------+--------------+-------+--------+
| PART_ID | CREATE_TIME | LAST_ACCESS_TIME | PART_NAME | SD_ID | TBL_ID |
+---------+-------------+------------------+--------------+-------+--------+
| 1 | 1556494255 | 0 | month=201904 | 29 | 28 |
+---------+-------------+------------------+--------------+-------+--------+

【修复方法一】直接执行修复命令

msck repair table tb_logs

此时分区表中的数据如下:

+---------+-------------+------------------+--------------+-------+--------+
| PART_ID | CREATE_TIME | LAST_ACCESS_TIME | PART_NAME | SD_ID | TBL_ID |
+---------+-------------+------------------+--------------+-------+--------+
| 1 | 1556494255 | 0 | month=201904 | 29 | 28 |
| 2 | 1556495227 | 0 | month=201905 | 30 | 28 |
+---------+-------------+------------------+--------------+-------+--------+

执行查询命令

select count(distinct ip) from db_hive.tb_logs where month = '201905';

返回结果为2,数据已正常加入分区。

【修复方法二】 使用增加分区命令

操作步骤:创建新分区目录并上传数据文件,命令如下:

hive (db_hive)> dfs -mkdir -p /user/hive/warehouse/db_hive.db/tb_logs/month=201906;
hive (db_hive)> dfs -put /root/files/tb_logs.txt /user/hive/warehouse/db_hive.db/tb_logs/month=201906;

执行增加分区命令

alter table tb_logs add partition(month = '201906');

查询数据,测试结果正常。

此时元数据分区表中数据如下:

+---------+-------------+------------------+--------------+-------+--------+
| PART_ID | CREATE_TIME | LAST_ACCESS_TIME | PART_NAME | SD_ID | TBL_ID |
+---------+-------------+------------------+--------------+-------+--------+
| 1 | 1556494255 | 0 | month=201904 | 29 | 28 |
| 2 | 1556495227 | 0 | month=201905 | 30 | 28 |
| 3 | 1556495635 | 0 | month=201906 | 31 | 28 |
+---------+-------------+------------------+--------------+-------+--------+

查看表分区命令

show partitions db_hive.tb_logs;

11、 导出表数据

export table db_hive.tb_logs to '/user/hive/warehouse/export/db_hive/tb_logs';

12、 导入表数据

创建表

create table tb_logs_like like tb_logs;

导入数据

import table tb_logs_like from '/user/hive/warehouse/export/db_hive/tb_logs';

13、导出数据到本地文件

insert overwrite local directory '/root/files/hive_out'
row format delimited fields terminated by '\t' collection items terminated by '\n'
select * from db_hive.tb_logs;

Hive 常用命令和语句

#!/usr/bin/python
# -*- coding: utf- -*-
from sqlalchemy.engine import create_engine
from sqlalchemy import text
import pandas as pd
import datetime starttime = datetime.datetime.now() sql = """
select *
from fact_five_data_cft
where source = 'classics' and wfid='' and yyyy=''
limit
"""
engine = create_engine('presto://node1:8085/hive/cnyb')
df = pd.read_sql(text(sql),engine)
print(df)
endtime = datetime.datetime.now()
runtime = endtime - starttime
print "presto run time -->"+str(runtime)

hive 常用操作的更多相关文章

  1. Hive常用操作之数据导入导出

    一.Hive数据导入导出 1.hive数据导出 很多时候,我们在hive中执行select语句,希望将最终的结果保存到本地文件或者保存到hdfs系统中或者保存到一个新的表中,hive提供了方便的关键词 ...

  2. hive常用操作

    相关显示参数设置 显示参数设置 set hive.cli.print.header=true; // 打印列名 set hive.cli.print.row.to.vertical=true; // ...

  3. 入门大数据---Hive常用DML操作

    Hive 常用DML操作 一.加载文件数据到表 1.1 语法 LOAD DATA [LOCAL] INPATH 'filepath' [OVERWRITE] INTO TABLE tablename ...

  4. Hive 常用命令和语句

    示例数据库为 db_hive 1. 创建表 create-table.sql create table if not exists db_hive.tb_user ( id int, username ...

  5. Hive 时间操作

    Hive 时间转换 UNIX时间戳概念:因为UNIX时间戳只是一个秒数,一个UNIX时间戳在不同时区看来,时间是不同的.如UNIX时间戳0,在0时区看来是1970-01-01 00:00:00,在东八 ...

  6. 【三】用Markdown写blog的常用操作

    本系列有五篇:分别是 [一]Ubuntu14.04+Jekyll+Github Pages搭建静态博客:主要是安装方面 [二]jekyll 的使用 :主要是jekyll的配置 [三]Markdown+ ...

  7. php模拟数据库常用操作效果

    test.php <?php header("Content-type:text/html;charset='utf8'"); error_reporting(E_ALL); ...

  8. Mac OS X常用操作入门指南

    前两天入手一个Macbook air,在装软件过程中摸索了一些基本操作,现就常用操作进行总结, 1关于触控板: 按下(不区分左右)            =鼠标左键 control+按下        ...

  9. mysql常用操作语句

    mysql常用操作语句 1.mysql -u root -p   2.mysql -h localhost -u root -p database_name 2.列出数据库: 1.show datab ...

随机推荐

  1. LeetCode 50. Pow(x, n) 12

    50. Pow(x, n) 题目描述 实现 pow(x, n),即计算 x 的 n 次幂函数. 每日一算法2019/5/15Day 12LeetCode50. Pow(x, n) 示例 1: 输入: ...

  2. LeetCode 404. 左叶子之和(Sum of Left Leaves)

    404. 左叶子之和 404. Sum of Left Leaves LeetCode404. Sum of Left Leaves 题目描述 计算给定二叉树的所有左叶子之和. 示例: 3 / \ 9 ...

  3. Redis搭建Windows平台

    安装程序下载 从官网下载安装程序. https://redis.io/download https://github.com/MicrosoftArchive/redis/releases 新地址:h ...

  4. go 数据渲染到html页面 02

    渲染到浏览器页面 //把数据渲染到浏览器 package main import ( "fmt" "text/template" "net/http& ...

  5. 『Python基础』第7节:基本运算符

    一. 基本运算符 运算按种类可以分为: 算数运算.比较运算.逻辑运算.赋值运算.成员运算.身份运算.位运算. 今天我们只学习算数运算.比较运算.逻辑运算.赋值运算.成员运算 1.1 算数运算 以下假设 ...

  6. xml文件中引用网址报红色如何解决

    用了ideal的宝宝们一定遇到过配置文件网址报红的错误吧 其实解决很简单,就是网不好导致它没法补全,我们手动给他补全就好啦 复制报红的网址 点击File==>settings==>lang ...

  7. SQL SERVER 中如何获取日期(一个月的最后一日、一年的第一日等等)

    https://blog.csdn.net/deepwishly/article/details/9101307 这是计算一个月第一天的SQL 脚本:   SELECT DATEADD(mm, DAT ...

  8. Oracle触发器编译错误及解决方案

    错误 TRIGGER **** 编译错误 错误:PLS-00103: 出现符号 "END"在需要下列之一时:        ( begin case declare exit    ...

  9. python之爬取小说

    继上一篇爬取小说一念之间的第一章,这里将进一步展示如何爬取整篇小说 # -*- coding: utf- -*- import urllib.request import bs4 import re ...

  10. css 居中 父子元素

    居中:是子元素相对于在父元素里面居中.父子宽度都固定. A:水平居中: ①给子元素设置一个宽度后.在给其水平方向的margin设置auto,子元素会在父元素水平方向的剩余空间,左右两边平均分配,也就左 ...