Hive JDBC connection using java (using beeline and hiveserver2)

First hive has been installed on the virtual machine.

The following is required for connecting the operation hive.

A configuration.

1. Find the ip virtual machine

    Enter ifconfig

 

 

 2. Profiles

and core-site.xml hdfs-site.xml under (1) Configuration directory hadoop

 

Add the following arranged in the core-site.xml:

<property>
    <name>hadoop.proxyuser.hadoop.hosts</name>
    <value>*</value>
</property>
<property>
    <name>hadoop.proxyuser.hadoop.groups</name>
    <value>*</value>
</property>

 

 

 Add the following configuration in hdfs-site.xml:

<property>

 <name>dfs.webhdfs.enabled</name>

 <value>true</value>

</property>

 

 

 (2) Configuration hive-site.xml file hive file conf directory under the folder (this is the hive-site.xml entire contents of the file, modify it according to your configuration)

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<?
xml-stylesheet type="text/xsl" href="configuration.xsl"?> <configuration> <property> <name>javax.jdo.option.ConnectionURL</name> <value>jdbc:mysql://localhost:3306/hive?createDatabaseIfNotExist=true</value> <description>JDBC connect string for a JDBC metastore</description> </property> <property> <name>javax.jdo.option.ConnectionDriverName</name> <value>com.mysql.jdbc.Driver</value> <description>Driver class name for a JDBC metastore</description> </property> <property> <name>javax.jdo.option.ConnectionUserName</name> <value>hive</value> //连接 hive 的用户名 <description>username to use against metastore database</description> </property> <property> <name>javax.jdo.option.ConnectionPassword</name> <value>hive</value> //连接 hive 的密码 <description>password to use against metastore database</description> </property>// ------------------------- ---------------------------split line--------------------- -----------
// The above configuration is part of the hive when installing on the need for, and has been configured not to modify

<!-- 这是hiveserver2 --> <property> <name>hive.metastore.warehouse.dir</name> <value>/usr/hive/warehouse</value> //(hive所在集群的IP地址) <description>location of default database for the warehouse</description> </property> <property> <name>hive.server2.thrift.port</name> <value>10000</value> <description>Port number of HiveServer2 Thrift interface. Can be overridden by setting $HIVE_SERVER2_THRIFT_PORT</description> </property> <property> <name>hive.server2.thrift.bind.host</name> <value>192.168.43.66</value> //主机地址(修改为自己的主机ip) <description>Bind host on which to run the HiveServer2 Thrift interface. Can be overridden by setting $HIVE_SERVER2_THRIFT_BIND_HOST</description> </property> <property> <name>hive.server2.long.polling.timeout</name> <value>5000</value> <description>Time in milliseconds that HiveServer2 will wait, before responding to asynchronous calls that use long polling</description> </property> </configuration>

Third, the use beeline / hiveserver2 connected hive

1. Start hadoop, view the process with jps

 

 

 2. Enter hiveserver2, wait for a while, to open a new terminal (the emergence of a new process RunJar)

 

 

 3. In the new terminal input beeline

 

 

 4. connect (connect jdbc:! Hive2: //192.168.43.66: 10000) (which is the ip address 192.168.43.66 own virtual machine)

 

 

 Arrow indicates where two (2) arranged in hive-site.xml mentioned user name and password in the above process a .2 (modify his user name and password)

Is there such an interface, the connection is successful.

 

Here is eclipe connectivity test.

 We first need to import the jar package to New Project

Connect mysql jar package (mysql-connector-java-5.1.44-bin.jar); all in a jar under hadoop share / hadoop / common / lib; hadoop the share / hadoop / common lower hadoop-common-2.7. 7.jar

And all the packages in the jar lib hive

Test code :( because I already have a data table and data, to find an example)

package com.jdbc.hive.test;
 
import java.sql.*;
import java.sql.SQLException;
 
public class JDBCHive {
 
    private static String driverName = "org.apache.hive.jdbc.HiveDriver";
    public static void main(String[] args) throws SQLException {
        try {
          Class.forName(driverName);
        }catch (ClassNotFoundException e) {
          // TODO Auto-generated catch block
          e.printStackTrace();
          System.exit(1);
        }
        CON Connection = the DriverManager.getConnection ( "JDBC: hive2: //192.168.43.66: 10000 / Hive", "Hive", "Hive"); // latter two parameters are user name and password 
        IF (CON == null )
            System.out.println ( "connection failed" );
         the else {
        Statement stmt = con.createStatement();
        String sql = "select * from data ";  
        //String sql = "select id,count(*) total from data  group by id order by total desc limit 10";     
        System.out.println("Running: " + sql);
        RES the ResultSet = stmt.executeQuery (SQL);
         the while (res.next ()) {
           // of System.out.print (res.getString (. 1) + "");
           // System.out.println (res.getString ( "Total")); 
            System.out.println (res.getString (. 1)); // data output of the first column 
            }

        }
      }
 
 
}

The results are shown below

 

 

 

 

Guess you like

Origin www.cnblogs.com/birdmmxx/p/11871531.html
Recommended