1.为hive配置mysql数据库

conf/hive-site.xml

<property>
<name>javax.jdo.option.ConnectionURL</name>
<value>jdbc:mysql://weekend01:3306/hive?createDatabaseIfNotExist=true</value> //hive为数据库名称,后面的语义为如果库不存在则创建
</property>
<property>
<name>javax.jdo.option.ConnectionDriverName</name>
<value>com.mysql.jdbc.Driver</value>
</property>
<property>
<name>javax.jdo.option.ConnectionUserName</name>
<value>root</value>
</property>
<property>
<name>javax.jdo.option.ConnectionPassword</name>
<value>root</value>
</property>

还需要mysql的驱动jar包

2.建表

CREATE TABLE page_view(viewTime INT, userid BIGINT,
     page_url STRING, referrer_url STRING,
     ip STRING COMMENT 'IP Address of the User')
 COMMENT 'This is the page view table'
 PARTITIONED BY(dt STRING, country STRING)
 ROW FORMAT DELIMITED
   FIELDS TERMINATED BY '\001'   #按某分隔符分隔字段
STORED AS SEQUENCEFILE;   TEXTFILE

SEQUENCEFILE:二进制文本,以键值对来组织的

执行创建操作后将相关的元数据表结构记录到数据库中

然后hdfs中会在相应的路径下生成数据库相关的数据文件夹和文件

 

3.导入数据

//从本地或者hdfs其他路径下导入数据到hive的表中(实质就是将文件上传到hdfs中hive管理目录下)
load data local inpath '/home/hadoop/ip.txt' into table tab_ext;

就是将数据文件拷贝到hdfs相关的数据库路径下去

4.如果数据文件中某一行数据中存在字段缺失,则在查询的结果中在最后一列用NULL字符进行标识。

 

5.hive表有两大类,MANAGED TABLE和EXTERNAL TABLE

EXTERNAL TABLE不需要数据文件必须存在于特定的目录中去

/external外部表
CREATE EXTERNAL TABLE tab_ip_ext(id int, name string,
     ip STRING,
     country STRING)
 ROW FORMAT DELIMITED FIELDS TERMINATED BY ','
 STORED AS TEXTFILE
 LOCATION '/external/user';

MANAGED TABLE被执行drop操作时,hdfs中的数据库文件都被删除掉了,并且数据库中的元数据也被删除了

EXTERNAL TABLE被执行drop操作时,hdfs中的数据库文件并没有被删除,只是删除了数据库中的元数据。

6.// CTAS  根据select语句建表结构
CREATE TABLE tab_ip_ctas
   AS
SELECT id new_id, name new_name, ip new_ip,country new_country
FROM tab_ip_ext
SORT BY new_id;

7.//insert from select   通过select语句批量插入数据到别的表
create table tab_ip_like like tab_ip;
insert overwrite table tab_ip_like
    select * from tab_ip;

8.//PARTITION  分区表
create table tab_ip_part(id int,name string,ip string,country string)
    partitioned by (year string)
    row format delimited fields terminated by ',';

load data local inpath '/home/hadoop/data.log' overwrite into table tab_ip_part
     partition(year='1990');
load data local inpath '/home/hadoop/data2.log' overwrite into table tab_ip_part
     partition(year='2000');

分区查询

select count(*) from tab_ip_part  where part_flag='part2';

9.hql语法

 //array
create table tab_array(a array<int>,b array<string>)
row format delimited
fields terminated by '\t'
collection items terminated by ',';

//map
create table tab_map(name string,info map<string,string>)
row format delimited
fields terminated by '\t'
collection items terminated by ','
map keys terminated by ':';

 

load data local inpath '/home/hadoop/hivetemp/tab_map.txt' overwrite into table tab_map; insert into table tab_map select name,map('name',name,'ip',ip) from tab_ext;

//struct

create table tab_struct(name string,info struct<age:int,tel:string,addr:string>) row format delimited fields terminated by '\t' collection items terminated by ','

load data local inpath '/home/hadoop/hivetemp/tab_st.txt' overwrite into table tab_struct; insert into table tab_struct select name,named_struct('age',id,'tel',name,'addr',country) from tab_ext;

 

//cli shell  通过shell执行hive的hql语句
hive -S -e 'select country,count(*) from tab_ext' > /home/hadoop/hivetemp/e.txt 

创建自定义函数

hive>add jar /home/hadoop/myudf.jar;
hive>CREATE TEMPORARY FUNCTION fanyi AS 'cn.itcast.hive.Fanyi';
select id,name,ip,fanyi(country) from tab_ip_ext;

10.hbase  

mysql和oracle是传统的关系型数据库,优势在于处理复杂的多表之间的关系

hbase不提供表的关联查询,属于nosql的范畴,只适合简单的查询

hbase的表容量可以非常大

HBase表结构:建表时,不需要限定表中的字段,只需要指定若干个列族,列族中可以存储任意多个列(KeyValue:列名和列值)

一个value可以有多个版本,通过版本号来区分,默认为时间戳

要查询某一个具体字段的值,需要指定的坐标:表明---->行键---->列族(ColumnFamily):列名(qualifier)---->版本号

11.hbase的集群架构及表存储机制

将数据切分成不同的region,放置于不同的region Server中(以HFile的形式放置于HDFS的DataNo的中)

 HMaster:不负责存储表数据,负责管理RegionServer的状态,负责RegionServer的负载均衡(HBase集群中,HMaster可以有多个,实现高可用)

hbase的寻址实现:将用户数据表记录按行键划分为不同的region,然后将相关的标识信息存储到系统表中(META表),较大的META表也会按这种形式继续拆分进行存储(ROOT表)

vim hbase-site.xml
<configuration>
        <!-- 指定hbase在HDFS上存储的路径 -->
        <property>
                <name>hbase.rootdir</name>
                <value>hdfs://ns1/hbase</value>
        </property>
        <!-- 指定hbase是分布式的 -->
        <property>
                <name>hbase.cluster.distributed</name>
                <value>true</value>
        </property>
        <!-- 指定zk的地址,多个用“,”分割 -->
        <property>
                <name>hbase.zookeeper.quorum</name>
                          <value>weekend04:2181,weekend05:2181,weekend06:2181</value>
        </property>
</configuration>

在哪执行命令启动HBase,哪个节点就是HMaster,然后根据regionservers文件中的配置启动region servers
vim regionservers
 weekend03
 weekend04
 weekend05
 weekend06

 

posted on 2018-04-11 14:54  sonofthesea  阅读(137)  评论(0编辑  收藏  举报