使用java连接hive,并执行hive语句详解

安装hadoop 和 hive我就不多说了,网上太多文章 自己看去

首先,在机器上打开hiveservice

[html]  view plain  copy
  1. hive --service hiveserver -p 50000 &  

打开50000端口,然后java就可以使用java连了,需要的jar包我发个图片

就这多jar包,必须的


不多说,直接上代码

[java]  view plain  copy
  1. package asia.wildfire.hive.service;  
  2.   
  3. import java.sql.*;  
  4. import java.sql.Date;  
  5. import java.text.SimpleDateFormat;  
  6. import java.util.*;  
  7.   
  8. /** 
  9.  * User: liuxiaochen 
  10.  * Date: 13-9-24 
  11.  * Time: 下午5:47 
  12.  * 修改描述 
  13.  */  
  14. public class HiveService {  
  15.     private static final String URLHIVE = "jdbc:hive://ip:50000/default";  
  16.     private static Connection connection = null;  
  17.   
  18.     public static Connection getHiveConnection() {  
  19.         if (null == connection) {  
  20.             synchronized (HiveService.class) {  
  21.                 if (null == connection) {  
  22.                     try {  
  23.                         Class.forName("org.apache.hadoop.hive.jdbc.HiveDriver");  
  24.                         connection = DriverManager.getConnection(URLHIVE, """");  
  25.                     } catch (SQLException e) {  
  26.                         e.printStackTrace();  
  27.                     } catch (ClassNotFoundException e) {  
  28.                         e.printStackTrace();  
  29.                     }  
  30.                 }  
  31.             }  
  32.         }  
  33.         return connection;  
  34.     }  
  35.   
  36.     public static void createTable() throws SQLException {  
  37.         String tweetTableSql = "DROP TABLE IF EXISTS hive_crm_tweet2222";  
  38.         String createTable1 = "CREATE EXTERNAL TABLE hive_crm_tweet2222(tweet_id string, cuser_id string, created_at bigint, year bigint, month bigint, day bigint, hour bigint, text string, comments_count bigint, reposts_count bigint, source string, retweeted_id string, post_type string, sentiment string, positive_tags_string string, predict_tags_string string, tags_string string) STORED BY 'org.apache.hadoop.hive.dynamodb.DynamoDBStorageHandler' TBLPROPERTIES (\"dynamodb.table.name\" = \"crm_tweet\",\"dynamodb.column.mapping\" = \"tweet_id:tweet_id,cuser_id:cuser_id,created_at:created_at,year:year,month:month,day:day,hour:hour,text:text,comments_count:comments_count,reposts_count:reposts_count,source:source,retweeted_id:retweeted_id,post_type:post_type,sentiment:sentiment,positive_tags_string:positive_tags_string,predict_tags_string:predict_tags_string,tags_string:tags_string\")";  
  39.         String commentTableSql = "DROP TABLE IF EXISTS hive_tweet_comment2222";  
  40.         String createTable2 = "CREATE EXTERNAL TABLE hive_tweet_comment2222(tweet_id string,comment_id string, cuser_id string, user_id string, created_at bigint, year bigint, month bigint, day bigint, hour bigint, text string, comments_count bigint, reposts_count bigint, source string, topic_id string, post_type string, sentiment string) STORED BY 'org.apache.hadoop.hive.dynamodb.DynamoDBStorageHandler' TBLPROPERTIES (\"dynamodb.table.name\" = \"crm_tweet_comment\",\"dynamodb.column.mapping\" = \"tweet_id:tweet_id,comment_id:comment_id,cuser_id:cuser_id,user_id:user_id,created_at:created_at,year:year,month:month,day:day,hour:hour,text:text,comments_count:comments_count,reposts_count:reposts_count,source:source,topic_id:tweet_id,post_type:post_type,sentiment:sentiment\")";  
  41.         String retweetTableSql = "DROP TABLE IF EXISTS hive_tweet_retweet2222";  
  42.         String createTable3 = "CREATE EXTERNAL TABLE hive_tweet_retweet2222(tweet_id string, cuser_id string, user_id string, retweet_id string, created_at BIGINT, year BIGINT, month BIGINT, day BIGINT, hour BIGINT, text string, comments_count BIGINT, reposts_count BIGINT, source string, topic_id string, verified_type BIGINT, post_type string, sentiment string) STORED BY 'org.apache.hadoop.hive.dynamodb.DynamoDBStorageHandler' TBLPROPERTIES (\"dynamodb.table.name\" = \"crm_tweet_retweet\",\"dynamodb.column.mapping\" = \"tweet_id:tweet_id,cuser_id:cuser_id,user_id:user_id,retweet_id:retweet_id,created_at:created_at,year:year,month:month,day:day,hour:hour,text:text,comments_count:comments_count,reposts_count:reposts_count,source:source,topic_id:tweet_id,verified_type:verified_type,post_type:post_type,sentiment:sentiment\")";  
  43.   
  44.         Statement stmt = getHiveConnection().createStatement();  
  45.         stmt.executeQuery(tweetTableSql);  
  46.         stmt.executeQuery(createTable1);  
  47.         stmt.executeQuery(commentTableSql);  
  48.         stmt.executeQuery(createTable2);  
  49.         stmt.executeQuery(retweetTableSql);  
  50.         stmt.executeQuery(createTable3);  
  51.     }  
  52.   
  53.     public static void selectTweet() throws SQLException {  
  54.         long aaa = System.currentTimeMillis();  
  55.         long start = DateUtils.getNDaysAgo(DateUtils.getMidNight(), 15).getTime().getTime();  
  56.         long end = DateUtils.getNDaysAgo(DateUtils.getMidNight(), 13).getTime().getTime();  
  57.         String sql = "select cuser_id, count(*) as tw_hour, year, month, day from hive_crm_tweet2222 where created_at > ? and created_at < ? and cuser_id = ? group by cuser_id, year, month, day, hour";  
  58.         PreparedStatement pstm = getHiveConnection().prepareStatement(sql);  
  59.         pstm.setLong(1, start);  
  60.         pstm.setLong(2, end);  
  61.         pstm.setString(3"2176270443");  
  62.         ResultSet rss = pstm.executeQuery();  
  63.         while (rss.next()) {  
  64.             System.out.println("1: " + rss.getString("cuser_id") + "   2: " + rss.getInt("tw_hour") + "   3: " + rss.getInt("year") + "   4: " + rss.getInt("month") + "   5: " + rss.getInt("day"));  
  65.         }  
  66.   
  67.         System.out.println(System.currentTimeMillis() - aaa);  
  68.   
  69.     }  
  70.   
  71.     public static void selectTweet22() throws SQLException {  
  72.         long aaa = System.currentTimeMillis();  
  73.         long start = DateUtils.getNDaysAgo(DateUtils.getMidNight(), 15).getTime().getTime();  
  74.         long end = DateUtils.getNDaysAgo(DateUtils.getMidNight(), 13).getTime().getTime();  
  75.         String sql = "select cuser_id, created_at, tweet_id from hive_crm_tweet2222 where created_at > ? and created_at < ? and cuser_id = ?";  
  76.         PreparedStatement pstm = getHiveConnection().prepareStatement(sql);  
  77.         pstm.setLong(1, start);  
  78.         pstm.setLong(2, end);  
  79.         pstm.setString(3"2176270443");  
  80.         ResultSet rss = pstm.executeQuery();  
  81.         SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH");  
  82.         while (rss.next()) {  
  83.             long cc = Long.valueOf(String.valueOf(rss.getInt("created_at")) + "000");  
  84.             java.util.Date date = new java.util.Date(cc);  
  85.             System.out.println(dateFormat.format(date));  
  86.             System.out.println(rss.getString("cuser_id") + " " + rss.getString("tweet_id"));  
  87.         }  
  88.   
  89.         System.out.println(System.currentTimeMillis() - aaa);  
  90.   
  91.     }  
  92.   
  93.     public static void main(String[] args) throws ClassNotFoundException, SQLException {  
  94. //        Class.forName("org.apache.hadoop.hive.jdbc.HiveDriver");  
  95. //        String querySQL = "SELECT a.* FROM test_time a";  
  96. //  
  97. //        Connection con = DriverManager.getConnection(URLHIVE, "", "");  
  98. //        Statement stmt = con.createStatement();  
  99. //        ResultSet res = stmt.executeQuery(querySQL);   // 执行查询语句  
  100. //  
  101. //        while (res.next()) {  
  102. //            System.out.println("Result: key:" + res.getString(1) + "  –>  value:" + res.getString(2));  
  103. //        }  
  104.         selectTweet22();  
  105.   
  106. //        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH");  
  107. //        System.out.println(dateFormat.format(new java.util.Date()));  
  108.     }  
  109. }  
代码如上,就这么多,当然复杂的逻辑,还需要你自己处理,谢谢

你可能感兴趣的:(大数据-Hive)