一、大数据领域的行式存储与列式存储
1、行式存储:
hdfs上一个block存储一或多行数据。
按行压缩,压缩性能受字段类型影响。
字段查询:select id,name from table_emp;
#####全表扫描,字段拼接,效率低。
全表查询:select * from table_emp;
#####直接展现数据,效率高。
2、列式存储:
hdfs上一个block一列或多列数据。
按列压缩,每一列相同数据类型,压缩性能好。
字段查询:select id,name from table_emp;
#####扫描部分字段,直接展现,效率高。
全表查询:select * from table_emp;
#####将分散的行重组, 效率低。
3、策略:
大数据领域的全表查询的场景少之又少,使用较多的还是列式存储。
1、存储格式的三种设置方式
hive建表时,可以使用“stored as file_format”来指定该表数据的存储格式,默认存储格式为TextFile,可通过hive.default.fileformat进行更改。
第一种方式:
create table t1
(
id int,
name string
)
row format delimited
fields terminated by "\t"
stored as textfile;
第二种方式:
create table t2 (
id int,
name string
)
row format delimited
fields terminated by "\t"
stored as
inputformat 'org.apache.hadoop.mapred.TextInputFormat'
outputformat 'org.apache.hadoop.hive.ql.io.HiveIgnoreKeyTextOutputFormat';
#####可以用这种方式执行自定义的存储格式。
第三种方式:
set hive.default.fileformat=textfile; #####该属性默认值就是textfile
create table t1
(
id int,
name string
)
row format delimited
fields terminated by "\t";
######以上三种方式存储的格式都是textfile。
2、存储格式讲解
A)textfile
textfile:
行式存储,默认的存储格式,将所有类型的数据都存储为String类型。
不便于数据的解析,也不具备随机读写的能力,但它却比较通用。
==============================================
测试:
建表:
create table t_text
(
record_time String,
imei String,
cell String,
ph_num int,
call_num int,
drop_num int,
duration int,
drop_rate int,
net_type String,
erl int
)
row format delimited
fields terminated by ','
stored as textfile;
导入数据:
hive> load data local inpath '/root/user.tmp/phone.csv' into table t_text;
统计大小:
hive> dfs -du -h -s /user/hive_remote/warehouse/t_text;
54.7 M /user/hive_remote/warehouse/t_text
相同查询读取的hdfs数据量大小:
hive> select count(1) from t_text where imei='359681';
HDFS Read: 57410034 ####约为54.7MB
========================