从hdfs导入数据到hive表

在文件已经导入(存入)hdfs之后,需要建表进行映射才可以show tables。

现在假设文件已导入该hdfs目录: /apps/hive/warehouse/db_name.db/tb_name (这里也可能是其他文件,如csv,txt等,如:/username/test/test.txt)

  • 方式一:建立的是外部分区表

1. 先按照hdfs中文件的字段,建立外部分区表:

create external table if not exists db_name.tb_name_2(id string, name string) partitioned by (day string) row format delimited fields terminated by '1' STORED AS textfile LOCATION '/apps/hive/warehouse/db_name.db/tb_name/day=xxxxxxxx';             # location指向直到分区(假设分区为day字段,如果指向的表不是分区表,则直到该表即可——不管怎样,要使得该目录里就是数据)

2. 建立分区

alter table db_name.tb_name_2 add partition (day=xxxxxxxx) location '/apps/hive/warehouse/db_name.db/tb_name/day=xxxxxxxx';  

  • 方式二:建立的是外部非分区表,则location直接指向数据地址:

如果指向的数据地址是某个非分区表数据,则直接指向该表:

create external table if not exists db_name.tb_name_2(id string, name string) row format delimited fields terminated by '1' STORED AS textfile LOCATION '/apps/hive/warehouse/db_name.db/tb_name';  

如果指向的数据地址只是某个表的分区,则直接指向分区数据(虽然建立的外部表不是分区表):

create external table if not exists db_name.tb_name_2(id string, name string) row format delimited fields terminated by '1' STORED AS textfile LOCATION '/apps/hive/warehouse/db_name.db/tb_name/day=xxxxxxxx';  

  • 方式三:建立的是内部表:先建表,再load

建表:create table if not exists db_name.tb_name_2(id string, name string) row format delimited fields terminated by '1' STORED AS textfile;  

load数据:load data inpath '/apps/hive/warehouse/db_name.db/tb_name' into table db_name.tb_name_2;

  •  方式四:如果嫌弃命令行麻烦,可以使用python代码完成建表和插入语句(spark-sql)
import os
from pyspark import SparkContext, SparkConf
from pyspark.sql.session import SparkSession
from pyspark.sql import HiveContext
from pyspark.sql import SQLContext
from pyspark.storagelevel import StorageLevel
from pyspark.sql.types import StructField, StructType, StringType
import warnings
warnings.filterwarnings("ignore")

os.environ["PYSPARK_PYTHON"]="/root/anaconda3/envs/my_env/bin/python3.7"  #如果只有一个环境env,则可不必要

# local 模式
# sc = SparkContext('local', 'test')

# cluster模式
conf = SparkConf().setAppName('test')
conf.set(" "," ")    # 各种spark配置
sc = SparkContext(conf=conf)
# ------ spark
= SparkSession.builder.appName("SparkOnHive").enableHiveSupport().getOrCreate() #为了使用hive命令 hive_text = HiveContext(spark) #这个也可 sql_text = SQLContext(sc) import pandas as pd dt = pd.read_csv('xxxxxx') df = dt.rename(columns)=lambda x:x.replace(" ","").replace(" ","").replace(" ","") #变换列名,去掉不必要的字符 #schema=StructType( # RDD变成DataFrame需要schema # [StructField("id", StringType(), True), # StructField("name", StringType(), True), # StructField("sex", StringType(), True)] # ) data = hive_text.createDataFrame(df) # 从pandas的df变成pyspark的DataFrame hive_text.registerDataFrameAsTable(data, tableName="test_table") #此时产生了虚拟表 hive_text.sql("use db_name") hive_text.sql("create table if not exists tb_name(id string, name string, sex string) row format delimited fields terminated by '1' STORED AS textfile") #开始执行sql语句,建表 hive_text.sql("insert overwrite table tb_name select * from test_table") # 虚拟表test_table可以直接使用 # hive_text.sql("alter table tb_name add if not exists partition(day=xxxxxxxx)") # 如果有分区,要先建分区 data_select = hive_text.sql("select * from xxxx") # 注意:得到的值data_select属于pyspark的DataFrame格式 sc.stop()

内部表和外部表:

未被external修饰的是内部表,被external修饰的为外部表;

区别:
内部表数据由Hive自身管理,外部表数据由HDFS管理;
内部表数据存储的位置是hive.metastore.warehouse.dir(默认:/user/hive/warehouse),外部表数据的存储位置由自己制定;
删除内部表会直接删除元数据(metadata)及存储数据;删除外部表仅仅会删除元数据,HDFS上的文件并不会被删除;
对内部表的修改会将修改直接同步给元数据,而对外部表的表结构和分区进行修改,则需要修复(MSCK REPAIR TABLE table_name;)

#

参考:

https://blog.csdn.net/qq_44449767/article/details/99716613

https://www.jianshu.com/p/1a4dfd654786

原文地址:https://www.cnblogs.com/qi-yuan-008/p/14059122.html