1. Before the linker runs, all packages under the hive file should be imported.
2. It is normal that the running time may be long, as long as you wait patiently.
3. After inputting Hive-Service Hiveserver, it is normal for the command box to get stuck. If you want it to be a background program, just change the input to Hive-Service Hive server &;; Just do it.
4. The real application is that metadata needs to be put on another machine, and importing mysql is a common method.
Generally speaking, we operate hive through cli, which is the console of Linux. However, in essence, each connection stores a metadata, and each metadata is different. So I suggest that this model is suitable for some tests, not for product development and application.
I. Environment
Hadoop version 0.20.2, Hive-0.5.0, JDK 1.6.
Second, the purpose of use
1. Generally speaking, we operate hive through cli, which is the console of Linux. However, in essence, each connection stores a metadata, and each metadata is different. So for this model, I suggest that it is more appropriate to do some tests, which is not suitable for product development and application.
2. Therefore, there are JDBC connection methods, and of course there are other connection methods, such as ODBC.
Third, the configuration of the connection.
1. Modify hive-site.xml
& lt attribute & gt
& lt name & gt javax.jdo.option.connectionurl < /name & gt;
& lt! -means to use embedded derby, and if create is true, it means to automatically create a database named Metastore _ DB->;
& lt value & gtJDBC:derby:;:; databaseName = metastore _ dbcreate = true & lt/value & gt;
& lt! -stands for derby using customer service mode, hadoopor is the database name, 192. 168.0.3 is the IP address of derby server, and 4567 is the port number of the server->;
& lt! -& lt; Value & gt JDBC: Derby:/192.168.0.3: 4567/Hadoop or; create = true & lt/value & gt; —& gt;
& lt/description & gt; JDBC connection string of JDBC metastore & lt/description >
& lt/property & gt;
& lt attribute & gt
& lt name & gt javax.jdo.option.connectiondriver name < /name & gt;
& lt value & gt org. Apache. derby.jdbc.embeddeddriver < /value & gt; Represents the use of embedded derby.
& lt! -& lt; Value & gtorg.apache.derby.jdbc.clientdriver <; /value & gt; -& gt; Use the customer service pattern to represent derby.
& lt/description & gt; driver class name of JDBC metastore & lt/description >
& lt/property & gt;
2. For embedded derby, the file derby.jar is required in the lib directory of hive, while for customer service mode derby, the file derbyclient.jar is required and needs to be downloaded by itself.
3. After the configuration is completed, enter hive-service hiveserver to start the service.
Fourth, the sample code
Mainly through JDBC to connect the source code of the hive.
Public class HiveJdbcClient {
Private static string drivername = "org.apache.hadoop.hive.jdbc.hive driver";
/**
* @param args
* @ throw SQLException
*/
Public static void main(String[] args) throws SQLException {
Try {
class . for name(driver name);
} catch(ClassNotFoundException e){
// TODO automatically generated catch block
e . printstacktrace();
system . exit( 1);
}
connection con = driver manager . getconnection(" JDBC:hive://IP: 10000/default ",""," ");
statement stmt = con . create statement();
String tableName = " http _ test
//stmt . execute query(" drop table "+tableName);
//ResultSet RES = stmt . execute query(" create table "+tableName+"(key int,value string)");
//Display table
String sql = " show tables
system . out . println(" Running:"+SQL);
ResultSet RES = stmt . execute query(SQL);
if (res.next()) {
system . out . println(RES . getstring( 1));
}
//Description table
SQL = " describe "+tableName;
system . out . println(" Running:"+SQL);
RES = stmt . execute query(SQL);
while (res.next()) {
system . out . println(RES . getstring( 1)+" \ t "+RES . getstring(2));
}
//Load data into the table
//Note: The file path must be the local path of the hive server.
//Note: /tmp/a.txt is a ctrl- a delimited file with two fields per line.
/**
* String file path = "/tmp/a . txt "; SQL = " load data local in path " "+file path+" ' into table "+tableName;
* system . out . println(" Running:"+SQL); RES = stmt . execute query(SQL);
*
*//select * query SQL = " select * from "+tableName; system . out . println(" Running:"+SQL); res =
* stmt . execute query(SQL); while(RES . next()){ system . out . println(string . value of(RES . getint( 1))+" \ t "+
* RES . getstring(2)); }
*
*//General hive query SQL = "select count (1from"+tablename; system . out . println(" Running:"+SQL); res =
* stmt . execute query(SQL); while(RES . next()){ system . out . println(RES . getstring( 1)); }
*/
}
}