Hive study notes (3)-shell command line

1 hive and non-interactive mode command line

  1. hive -e: Execute the specified HQL from the command line without a semicolon:
hive -e ‘select * from dumy limit 100>a.txt
  1. hive -f: execute HQL foot
hive -f /home/my/hive-script.sql

3) hive -i: execute the HQL statement in the script first when entering the Hive interactive shell

hive -i /home/my/hive-init.sql 

4) hive -v: redundant verbose, which additionally prints out the executed HQL statements;
5) hive -S: silent slient mode, does not display the information of converting MR-Job, only displays the final result

hive -S -e ‘select * from student

6) hive --hiveconf <property=value>: Use the value of a given property

HIVE_HOME/bin/hive --hiveconf mapred.reduce.tasks=2 //启动时,配置reduce个数2(只在此session中有效)

7) hive --service serviceName: start the service

hvie --service hiveserver2

8) hive [–database test]: Enter the CLI interactive interface, and enter the default database by default. Add [] content directly into the test database.

hive --database test

2. Commands in Hive interactive mode:

quit / exit: Exit CLI

reset: Reset all configuration parameters and initialize them to the configuration in hive-site.xml. As before, use the set command to set the number of reduce.

set <key>=<value>: Set Hive runtime configuration parameters, the priority is the highest, the same key, the following settings will overwrite the previous settings.

set –v: Print out all Hive configuration parameters and Hadoop configuration parameters.

//找出和"mapred.reduce.tasks"相关的设置
hive -e 'set -v;' | grep mapred.reduce.tasks

addCommand: Including add File[S]/Jar[S]/Archive[S] *, add one or more files, jar packages, or archives to DistributeCache. After adding, it can be used in Map and Reduce tasks. For example, if you customize a udf function and type it into a jar package, you must use the add jar command to add the jar package before creating the function, otherwise an error will be reported that the class cannot be found.

list 命令: Including list File[S]/Jar[S]/Archive[S]. List the files, jar packages or archives in the current DistributeCache.

delete 命令: Include delete File[S]/Jar[S]/Archive[S] *. Delete files from DistributeCache.

//将file加入缓冲区
add file /root/test/sql;
//列出当前缓冲区内的文件
list file;
//删除缓存区内的指定file
delete file /root/test/sql;

create命令: Create a custom function: hive> create temporary function udfTest as'com.cstore.udfExample';

source <filepath>: Execute the script file in CLI.

//相当于[root@ncst test]# hive -S -f /root/test/sql
hive> source /root/test/sql; 

!: Execute Linux commands in CLI.

dfs: execute hdfs command in CLI

3. Three ways to save query results:

hive -S -e 'select * from dummy' > a.txt //分隔符和hive数据文件的分隔符相同
[root@hadoop01 ~]# hive -S -e "insert overwrite local directory '/root/hive/a'\ 
>  row format delimited fields terminated by '\t' --分隔符\t
>  select * from logs sort by te" 
--使用hdfs命令导出整个表数据
hdfs dfs -get /hive/warehouse/hive01 /root/test/hive01 

4. Import and export between Hive clusters

Use the Export command to export the data table data of the Hive table and the metadata corresponding to the data table

--导出命令
EXPORT TABLE test TO '/hive/test_export'

--dfs命令查看
hdfs dfs -ls /hive/test_export

--结果显示
/hive/test_export/_metadata
/hive/test_export/data

Use the Import command to re-import the exported data into hive (the imported table must be renamed)

--导入到内部表的命令
IMPORT TABLE data_managed FROM '/hive/test_export'

--导入到外部表的命令
Import External Table data_external From '/hive/test_export' Location '/hive/external/data'

--验证是否是外部表
desc formatted data_external

5.Hive - JDBC/ODBC

In the Hive jar package, "org.apache.hadoop.hive.jdbc.HiveDriver" is responsible for providing the JDBC interface. With this package, the client program can use Hive as a database. Most of the operations are different from the traditional The operation of the database is the same. Hive allows applications that support the JDBC protocol to connect to Hive. After Hive starts the hiveserver service on the specified port, the client communicates with the Hive server through Java's Thrift. The process is as follows:

1. Turn on the hiveserver service:

$ hive –service hiveserver 50000(50000)  

2. Establish a connection with Hive:

Class.forName(“org.apache.hadoop.hive.jdbc.HiveDriver”);  

Connectioncon=DriverManager.getConnection(“jdbc:hive://ip:50000/default,”hive”,”hadoop”) 

By default, you can only connect to the default database. After the connection is established through the above two lines of code, other operations are not much different from traditional databases.

3. Hive's JDBC driver is not yet mature and does not support all JDBC APIs.

6.Hive Web Interface

1. Place hive-site.xml

  <property>
        <name>hive.hwi.war.file</name>
        <value>lib/hive-hwi-0.8.1.war</value>
        <description>This sets the path to the HWI war file, relative to ${HIVE_HOME}.</description>    
        </property>
        
        <property>
        <name>hive.hwi.listen.host</name>
        <value>0.0.0.0</value>
        <description>This is the host address the Hive Web Interface will listen on</description>
        </property>
        
        <property>
        <name>hive.hwi.listen.port</name>
        <value>9999</value>
        <description>This is the port the Hive Web Interface will listen on</description>
        </property>

2. Start the Hive web service:
  hive --service hwi

3. Type the address in the browser: http://host_name:9999/hwivisit

4. Click "Create Session" to create a session, and type the query in Query

7. Hive create database

After hive is started, there is a Default database by default, or you can create a new database artificially, command:

--手动指定存储位置
create database hive02 location '/hive/hive02';

--添加其他信息(创建时间及数据库备注)
create database hive03 comment 'it is my first database' with dbproperties('creator'='kafka','date'='2015-08-08');

--查看数据库的详细信息
describe database hive03;
--更详细的查看
describe database extended hive03; 
--最优的查看数据库结构的命令
describe database formatted hive03;

--database只能修改dbproperties里面内容
alter database hive03 set dbproperties('edited-by'='hanmeimei');

Source: https://www.cnblogs.com/skyl/p/4736129.html

Guess you like

Origin blog.csdn.net/u013963379/article/details/90143507