scala读写hbase和hive经验汇总

使用注意点:

1、读取hbase,设置prefixFilter过滤器时候,必须使用setStartRow(或者直接使用setRowPrefixFilter过滤器),否则每次scan会遍历hbase全量数据,而不是hbase的分区数据。

2、scala的函数式编程,两个变量组合操作时候,需要注意有些操作要用括号包起来,否则会出错。

3、对hive静态分区(static partition)使用insert或load加载数据时,要明确指定partition(分区名1=分区值, 分区名2=分区值)。

4、flatMap是在map基础上,对“列表的列表”拉平为“列表”。

5、使用maven管理依赖包时候,使用-0参数禁止每次package时重新download包。

6、注意scala中的to和until的区别,前者是右闭,后者是右开。

7、scala使用命令行参数,命令行中的第一个参数就是args(0)

8、Java中的byte[]转为Scala中的sting:new Sting()

9、hive访问的几种访问:hue访问、hive命令行访问、beeline命令行访问、hive api访问(本质也是beeline访问)。

10、hive数据表的字段名限制:字段名称中可用包含关键字、$和_,但需要用“·”(键盘上的左上方引号)包围即可,但字段名不能包含点号。

 

使用命令:

1、访问hive:

beeline -u jdbc:hive2://ip地址:10000/default -n 用户名 -p 用户密码

2、hive加载静态分区数据:

insert into table 数据表1 partition(分区字段1='test', 分区字段2='test') select * from 数据表2;

load data local inpath '本地文件path' overwrite into table 数据表1;

 

3、hive表使用压缩编码:

SET hive.exec.compress.output=true;
SET mapred.output.compress=true;
SET mapred.output.compression.codec=org.apache.hadoop.io.compress.GzipCodec;
set io.compression.codecs=org.apache.hadoop.io.compress.GzipCodec;  

4、hive表使用Json存储数据(数据文件中的JSON字符串不能跨行):

row format SERDE 'org.openx.data.jsonserde.JsonSerDe' 

相关设置:

(1)放到Hadoop的lib目录下

(2)将相应json的jar添加到hive环境($HIVE_HOME/lib/ )中:

ADD JAR /opt/cloudera/parcels/CDH/lib/hive/lib/json-serde-1.3.8-jar-with-dependencies.jar;

5、hadoop集群的节点名称修改之后,使用hive时显示hdfs目录不一致问题

使用hive自带的metatool修改:

metatool -updateLocation <new-location> <old-location>

6、新增hive表结构:

alter table 数据表名称add columns(字段名称 字段类型);

7、load或insert时候,区分into和overwrite区别:

into:不覆盖旧数据,append新数据

overwrite:使用新数据覆盖旧数据

8、修改linux的环境变量

/etc/profile是系统级别的,可以使用用户级别的环境变量配置文件:~/.bashrc

source ~/.bashrc立即生效

9、通过Java的Calendar包获取时间和日期:

(1)Calendar.HOUR_OF_DAY是24小时制度,而Calendar.HOUR是12制度

(2)Calendar.MONTH下标从0开始,而Calendar.DAY_OF_MONTH和Calendar.HOUR_OF_DAY的下标从1开始。

 

Hive相关知识点

Hive数据表中可存储复杂数据类型,比如数组、字典等。

字段名称 array<string>,
字段名称 map<string,array<int>>)
使用时候可以使用[]下标获取(类似python中的字典用法)

-- 设置Hive对Json解析时,对异常数据忽略,读物为null。
ALTER TABLE 数据表 SET SERDEPROPERTIES("ignore.malformed.json"="true");

--设置为array[int/string等]之后,遇单元素则单元素数组。

-- 数据表存在保留字时,可以使用数据表字段和Json的key之间的映射做转换。
ROW FORMAT SERDE 'org.openx.data.jsonserde.JsonSerDe'
WITH SERDEPROPERTIES("mapping.字段名称"="JSON字符串中的key名称")


hive的常见命令:
show tables like '****'; -- 模糊查询
desc
show partitions
select table_coulm from table_name where partition_name = '分区字段值';  

-- 导入数据文件到数据表
LOAD DATA LOCAL INPATH '数据文件' OVERWRITE into table 数据表;  

-- 数据表数据导出问文件
insert overwrite local directory '数据文件的path' select 字段列表 from 数据表;


-- 自定义udf函数
step1: 继承UDF类,重写evaluate方法
step2: 打成jar包,hive执行add jar /***/***.jar;
step3: create temporary function 函数名称 as 'jar中类路径'


-- hive shell中kill
hadoop job -kill job_id  


alter table 数据表名称 drop partition(dt='分区值');  


ALTER TABLE 数据表名称old RENAME TO 数据表名称new;  

CREATE TABLE 数据表名称new LIKE 数据表名称new;  

-- 不使用mr引擎
set hive.fetch.task.conversion=more;  

mapjoin的使用场合:
1、关联操作中有一张表非常小 (大表数据倾斜)
2、不等值的链接操作
select /*+ mapjoin(A)*/ f.a,f.b from A t join B f  on ( f.a=t.a and f.ftime=20110802)   

Join的操作情况
(1)Common Join(Reduce阶段完成join)
-- 包含Map、Shuffle、Reduce
(2)Map Join(Map阶段完成join)

-- note
(1)shuffle阶段需要大量的网络数据传输
(2)where字句有不等的判断条件时候,进行表关联会进行笛卡尔积运算。

hive0.7版本之前使用hint:/*+ mapjoin(table) */
0.7版本之后只需要设置下面的选项即可。
set hive.auto.convert.join=true;
-- 设置MapJoin开启关闭的阀值:25M
hive.mapjoin.smalltable.filesize=25000000

MapJoin的操作流程(实际就是将小表数据load到各个mapper的内存中):
step1: MR使用DistributedCache.addCacheFile()记录和拷贝小表文件。
step2: 启动jon之前,JobTracker提前获取文件url,将小表文件分发到各个TaskTracker所在磁盘。
step3: Map类的sepup方法调用DistributedCache.getLocalCacheFiles()将文件读入内存。


--合并MapJoin
set hive.auto.convert.join.noconditionaltask=true;
--n个小表大小的合计
set hive.auto.convert.join.noconditionaltask.size:


set hive.execution.engine=mr;
set hive.execution.engine=spark;
set hive.execution.engine=tez;

 

Maven相关知识点

1、使用离线模式打包,而且跳过test

1)mvn clean package -o -Dmaven.skip.test=true

2)也可以在pom.xml文件中配置:<offline>true</offline>

2、maven pom文件添加第三方仓库:

<repositories>
    <repository>
        <id>Cloudera</id>
        <url>https://repository.cloudera.com/content/repositories/releases/</url>
    </repository>
</repositories>

3、本地maven仓库拷贝到服务器:

step1:拷贝本地~/.m2文件夹到服务器

step2:修改$MAVEN_HOME/conf下的settings.xml,

<localRepository>/usr/local/.m2/repository</localRepository>

 

 

 

基础知识点

1、Hive中静态分区表和动态分区表的区别

 

2、Hive中order by, sort by, distribute by, cluster by的区别

 

3、Hbase的分区机制

 

 

 

 

 

 

 

 

 

 

 

 

 

  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值