---
title: "[Hadoop]Hive r0.9.0中文文档（四）之Hive变量的使用"
author: "MyEyeOfJava"
date: "2012-10-24T11:42:35Z"
tags: []
categories: ["互联网"]
slug: "hadoop-hive-r0-9-0-hive"
draft: false
lastmod: "2012-10-24T11:42:35Z"
id: 1704345
url: "https://myeyeofjava.iteye.com/blog/1704345"
---

**一、介绍**
例子:


```java
$ a=b
$ hive -e " describe $a "
```

如果你hive数据库中没有b这个表，则会提示Table b does not exist

hive的变量设置可以放在hiveconf中，使变量赋值与查询合并为一句话：
例子:


```java
$ bin/hive -hiveconf a=b -e 'set a; set hiveconf:a; \
create table if not exists b (col int); describe ${hiveconf:a}'
```

Results in:


```java
Hive history file=/tmp/edward/hive_job_log_edward_201011240906_1463048967.txt
a=b
hiveconf:a=b
OK
Time taken: 5.913 seconds
OK
col	int	
Time taken: 0.754 seconds
```


**二、hive变量使用**

hive的变量有3个作用空间hiveconf，system，env。hiveconf就像平时一样设置：


```java
set x=myvalue
```


X变量会被这样调用：

```java
${hiveconf:x}
```

Annotated examples of usage from the test case 


```java
ql/src/test/queries/clientpositive/set_processor_namespaces.q

set zzz=5;
--  sets zzz=5
set zzz;

set system:xxx=5;
set system:xxx;
-- sets a system property xxx to 5

set system:yyy=${system:xxx};
set system:yyy;
-- sets yyy with value of xxx

set go=${hiveconf:zzz};
set go;
-- sets go base on value on zzz

set hive.variable.substitute=false;
set raw=${hiveconf:zzz};
set raw;
-- disable substitution set a value to the literal

set hive.variable.substitute=true;

EXPLAIN SELECT * FROM src where key=${hiveconf:zzz};
SELECT * FROM src where key=${hiveconf:zzz};
--use a variable in a query

set a=1;
set b=a;
set c=${hiveconf:${hiveconf:b}};
set c;
--uses nested variables. 


set jar=../lib/derby.jar;

add file ${hiveconf:jar};
list file;
delete file ${hiveconf:jar};
list file;
```


**三、Disabling**
Variable substitution is on by default. If this causes an issue with an already existing script disable it.


```java
set hive.variable.substitute=false;
```