Hive教程之metastore的三种模式

本文介绍了Hive中元数据存储的三种方式:内嵌Derby、Local Mysql及Remote Mysql,并详细展示了每种方式的配置过程及注意事项。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Hive中metastore(元数据存储)的三种方式

  • 内嵌Derby方式
  • Local方式
  • Remote方式

[一]、内嵌Derby方式

这个是Hive默认的启动模式,一般用于单元测试,这种存储方式有一个缺点:在同一时间只能有一个进程连接使用数据库。

hive-site.xml 中jdbc URL、驱动、用户名、密码等的配置信息如下:

1 <property>
2   <name>javax.jdo.option.ConnectionURL</name>
3   <value>jdbc:derby:;databaseName=metastore_db;create=true</value>
4   <description>JDBC connect string for a JDBC metastore</description>
5 </property>
6  
7 <property>
8   <name>javax.jdo.option.ConnectionDriverName</name>
9   <value>org.apache.derby.jdbc.EmbeddedDriver</value>
10   <description>Driver class name for a JDBC metastore</description>
11 </property>
12  
13 <property>
14   <name>javax.jdo.option.ConnectionUserName</name>
15   <value>APP</value>
16   <description>username to use against metastore database</description>
17 </property>
18  
19 <property>
20   <name>javax.jdo.option.ConnectionPassword</name>
21   <value>mine</value>
22   <description>password to use against metastore database</description>
23 </property>
24  
25 <property>
26   <name>hive.metastore.warehouse.dir</name>
28   <description>unit test data goes in here on your local filesystem</description>
29 </property>
30  
31 <!-- micmiu.com -->

执行初始化命令:schematool -dbType derby -initSchema

查看初始化后的信息: schematool -dbType derby -info

配置完成后就可在shell中以CLI的方式访问hive 进行操作验证。

[二]、Local方式

以本地Mysql数据库为例:创建好用户:hive;database:hive

配置文件 hive-site.xml 中jdbc URL、驱动、用户名、密码等属性值配置如下:

1 <property>
2   <name>javax.jdo.option.ConnectionURL</name>
4   <description>JDBC connect string for a JDBC metastore</description>
5 </property>
6  
7 <property>
8   <name>javax.jdo.option.ConnectionDriverName</name>
9   <value>com.mysql.jdbc.Driver</value>
10   <description>Driver class name for a JDBC metastore</description>
11 </property>
12  
13 <property>
14   <name>javax.jdo.option.ConnectionUserName</name>
15   <value>hive</value>
16   <description>username to use against metastore database</description>
17 </property>
18  
19 <property>
20   <name>javax.jdo.option.ConnectionPassword</name>
21   <value>micmiu</value>
22   <description>password to use against metastore database</description>
23 </property>
24  
25 <property>
26   <name>hive.metastore.warehouse.dir</name>
27   <!-- base hdfs path -->
28   <value>/user/hive/warehouse</value>
29   <description>location of default database for the warehouse</description>
30 </property>
31 <!-- micmiu.com -->

ps:需要把mysql的驱动包copy到目录 <HIVE_HOME>/lib 中

如果是第一次需要执行初始化命令:schematool -dbType mysql -initSchema

1 micmiu-mbp:mysql micmiu$ schematool -dbType mysql -initSchema
2 14/02/17 14:46:08 INFO Configuration.deprecation: mapred.input.dir.recursive is deprecated. Instead, use mapreduce.input.fileinputformat.input.dir.recursive
3 14/02/17 14:46:08 INFO Configuration.deprecation: mapred.max.split.size is deprecated. Instead, use mapreduce.input.fileinputformat.split.maxsize
4 14/02/17 14:46:08 INFO Configuration.deprecation: mapred.min.split.size is deprecated. Instead, use mapreduce.input.fileinputformat.split.minsize
5 14/02/17 14:46:08 INFO Configuration.deprecation: mapred.min.split.size.per.rack is deprecated. Instead, use mapreduce.input.fileinputformat.split.minsize.per.rack
6 14/02/17 14:46:08 INFO Configuration.deprecation: mapred.min.split.size.per.node is deprecated. Instead, use mapreduce.input.fileinputformat.split.minsize.per.node
7 14/02/17 14:46:08 INFO Configuration.deprecation: mapred.reduce.tasks is deprecated. Instead, use mapreduce.job.reduces
8 14/02/17 14:46:08 INFO Configuration.deprecation: mapred.reduce.tasks.speculative.execution is deprecated. Instead, use mapreduce.reduce.speculative
9 Metastore connection URL:    jdbc:mysql://localhost/hive?createDatabaseIfNotExist=true
10 Metastore Connection Driver :    com.mysql.jdbc.Driver
11 Metastore connection User:   hive
12 Starting metastore schema initialization to 0.12.0
13 Initialization script hive-schema-0.12.0.mysql.sql
14 Initialization script completed
15 schemaTool completeted

查看初始化后信息 schematool -dbType mysql -info

初始化后查看mysql中表情况:show tables;

1 mysql> show tables;
2 +---------------------------+
3 | Tables_in_hive            |
4 +---------------------------+
5 | BUCKETING_COLS            |
6 | CDS                       |
7 | COLUMNS_V2                |
8 | DATABASE_PARAMS           |
9 | DBS                       |
10 | DB_PRIVS                  |
11 | DELEGATION_TOKENS         |
12 | GLOBAL_PRIVS              |
13 | IDXS                      |
14 | INDEX_PARAMS              |
15 | MASTER_KEYS               |
16 | NUCLEUS_TABLES            |
17 | PARTITIONS                |
18 | PARTITION_EVENTS          |
19 | PARTITION_KEYS            |
20 | PARTITION_KEY_VALS        |
21 | PARTITION_PARAMS          |
22 | PART_COL_PRIVS            |
23 | PART_COL_STATS            |
24 | PART_PRIVS                |
25 | ROLES                     |
26 | ROLE_MAP                  |
27 | SDS                       |
28 | SD_PARAMS                 |
29 | SEQUENCE_TABLE            |
30 | SERDES                    |
31 | SERDE_PARAMS              |
32 | SKEWED_COL_NAMES          |
33 | SKEWED_COL_VALUE_LOC_MAP  |
34 | SKEWED_STRING_LIST        |
35 | SKEWED_STRING_LIST_VALUES |
36 | SKEWED_VALUES             |
37 | SORT_COLS                 |
38 | TABLE_PARAMS              |
39 | TAB_COL_STATS             |
40 | TBLS                      |
41 | TBL_COL_PRIVS             |
42 | TBL_PRIVS                 |
43 | TYPES                     |
44 | TYPE_FIELDS               |
45 | VERSION                   |
46 +---------------------------+
47 41 rows in set (0.00 sec)

配置完成后就可在shell中以CLI的方式访问hive 进行操作验证。

[三]、Remote方式

以Mysql数据库(192.168.6.77)为例:创建好用户:hive;database:hive_meta。Remote方式需要分别配置服务端和客户端的配置文件:

服务端的 hive-site.xml 中jdbc URL、驱动、用户名、密码等属性值配置如下:

1 <property>
2   <name>javax.jdo.option.ConnectionURL</name>
4   <description>JDBC connect string for a JDBC metastore</description>
5 </property>
6  
7 <property>
8   <name>javax.jdo.option.ConnectionDriverName</name>
9   <value>com.mysql.jdbc.Driver</value>
10   <description>Driver class name for a JDBC metastore</description>
11 </property>
12  
13 <property>
14   <name>javax.jdo.option.ConnectionUserName</name>
15   <value>hive</value>
16   <description>username to use against metastore database</description>
17 </property>
18  
19 <property>
20   <name>javax.jdo.option.ConnectionPassword</name>
21   <value>micmiu</value>
22   <description>password to use against metastore database</description>
23 </property>
24  
25 <property>
26   <name>hive.metastore.warehouse.dir</name>
27   <!-- base hdfs path -->
28   <value>/user/hive/warehouse</value>
29   <description>base hdfs path :location of default database for the warehouse</description>
30 </property>
31 <!-- micmiu.com -->

ps:需要把mysql的驱动包copy到目录 <HIVE_HOME>/lib 中

如果是第一次需要执行初始化命令:schematool -dbType mysql -initSchema

客户端中配置内容修改如下:

1 <!-- thrift://<host_name>:<port> 默认端口是9083 -->
2 <property>
3   <name>hive.metastore.uris</name>
4   <value>thrift://192.168.6.77:9083</value>
5   <description>Thrift uri for the remote metastore. Used by metastore client to connect to remote metastore.</description>
6 </property>
7  
8 <!--  hive表的默认存储路径 -->
9 <property>
10   <name>hive.metastore.warehouse.dir</name>
11   <value>/user/hive/warehouse</value>
12   <description>location of default database for the warehouse</description>
13 </property>

hive metastore 服务端启动命令:
hive --service metastore -p <port_num>
如果不加端口默认启动:hive --service metastore,则默认监听端口是:9083 ,注意客户端中的端口配置需要和启动监听的端口一致。服务端启动正常后,客户端就可以执行hive操作了。

<think>好的,我需要帮助用户了解如何在Hive本地模式下正确配置hive-site.xml文件。首先,我得回忆一下Hive的本地模式配置的关键点。本地模式通常意味着Hive不依赖Hadoop集群的资源管理器(如YARN),而是使用本地文件系统和单JVM来执行任务,这样可以简化开发和测试环境。 根据引用[1]的内容,hive-site.xml的配置项需要区分服务端和客户端,但用户现在问的是本地模式,可能更关注如何配置基本的元数据存储和执行引擎。本地模式下,Hive默认使用Derby数据库作为元存储,但用户可能希望配置独立的元存储或者调整其他参数。 接下来,需要确保配置的语法正确,比如使用正确的XML格式,以及Hive特定的属性名。例如,设置hive.exec.mode.local.auto为true可以启用本地模式优化。此外,数据仓库目录的位置(hive.metastore.warehouse.dir)需要指向本地路径,如file:///usr/local/hive/warehouse,而不是HDFS路径。 还要注意执行引擎的设置,本地模式下可能需要使用mr(MapReduce)而不是tez,但用户可能也需要了解如何切换到本地模式的其他配置。另外,引用[2]提到了环境变量的配置,比如HIVE_HOME和PATH,这也是配置的一部分,确保Hive命令可以正确执行。 用户可能遇到的问题包括权限错误,比如本地目录的写入权限,或者Derby数据库的并发访问限制。因此,建议配置MySQL作为元存储而不是Derby,但用户可能需要基础教程,所以先提供Derby的配置,再提到MySQL的替代方案。 最后,根据用户提供的引用[3],一些表属性的继承配置可能也需要关注,但在本地模式下可能不是首要问题。需要确保所有配置项都放置在hive-site.xml中,并且文件的位置正确,通常是Hive的conf目录下。 总结步骤应该是:创建hive-site.xml,配置元存储连接,设置本地执行模式,指定仓库目录,环境变量配置,然后验证配置。可能还需要提醒用户检查Derby的锁问题,以及如何启动Hive CLI进行测试。</think>### Hive本地模式hive-site.xml配置教程Hive本地模式下,核心配置需关注**元数据存储**和**执行引擎**的本地化设置。以下是具体步骤: --- #### 1. 创建配置文件 进入Hive的`conf`目录,创建或修改`hive-site.xml`: ```xml <?xml version="1.0"?> <?xml-stylesheet type="text/xsl" href="configuration.xsl"?> <configuration> <!-- 本地元数据存储配置 --> <property> <name>javax.jdo.option.ConnectionURL</name> <value>jdbc:derby:;databaseName=/path/to/metastore_db;create=true</value> <description>Derby本地数据库路径</description> </property> <!-- 启用本地执行模式 --> <property> <name>hive.exec.mode.local.auto</name> <value>true</value> <description>自动优化为本地模式(小数据集时生效)</description> </property> <!-- 数据仓库目录 --> <property> <name>hive.metastore.warehouse.dir</name> <value>file:///usr/local/hive/warehouse</value> <description>本地文件系统路径</description> </property> </configuration> ``` --- #### 2. 关键配置项说明 - **`hive.exec.mode.local.auto`**: 当输入数据量小于`hive.exec.mode.local.auto.inputbytes.max`(默认128MB)时自动触发本地模式[^3] - **`hive.metastore.warehouse.dir`**: 本地模式必须使用`file://`协议而非HDFS路径 - **Derby配置**: 默认嵌入式数据库,但存在并发访问限制,建议开发环境改用MySQL: ```xml <property> <name>javax.jdo.option.ConnectionURL</name> <value>jdbc:mysql://localhost/hive_meta?createDatabaseIfNotExist=true</value> </property> ``` --- #### 3. 环境变量配置 根据引用[2],在`/etc/profile`中添加: ```bash export HIVE_HOME=/usr/soft/apache-hive-x.x.x-bin export PATH=$PATH:$HIVE_HOME/bin ``` 执行`source /etc/profile`使配置生效 --- #### 4. 验证配置 ```bash hive --service cli # 执行测试查询 hive> CREATE TABLE test_local(id int); hive> SHOW TABLES; ``` --- #### 常见问题处理 1. **Derby锁定问题**:检查`metastore_db`目录是否被其他进程占用 2. **权限错误**:确保本地仓库目录有写入权限 3. **配置未生效**:确认配置文件位于`$HIVE_HOME/conf`且无拼写错误
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值