这几周一直在查找驱动、数据库-之类的问题,上午正好有机会和大家讨论一下.
1,用使Jdbc式方链接hive,首先须要动启hive的Thrift Server,否则会致使误错
hive --service hiveserver 是两”-“,
Could not establish connection to localhost:10000/default: java.net.ConnectException: Connection refused
2,简略的Java代码实现;
----通过Class.forName("org.apache.hadoop.hive.jdbc.HiveDriver");加载hive驱动
----通过Connection conn = DriverManager.getConnection("jdbc:hive://localhost:10000/default","","");立建与数据库的连接
eg:
package hive.jdbc;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
public class HiveJdbcDriver {
public static void main(String[] args) throws Exception {
Class.forName("org.apache.hadoop.hive.jdbc.HiveDriver");
Connection conn = DriverManager.getConnection("jdbc:hive://localhost:1000/default","","");
Statement stmt = conn.createStatement();
String tablename = "u_data_new";
String quary_sql = "select weekday,count(*) from " + tablename + " group by weekday";
ResultSet rs = stmt.executeQuery(quary_sql);
while(rs.next()){
System.out.println("weekday: "+rs.getInt(1)+"count: "+rs.getInt(2));
}
}
}
result:
weekday: 2 count: 13579
weekday: 3 count: 14430
weekday: 4 count: 15114
weekday: 5 count: 14743
weekday: 6 count: 18229
weekday: 7 count: 11651
hive执行结果:
hive> select weekday,count(*) from u_data_new group by weekday;
文章结束给大家分享下程序员的一些笑话语录: 人工智能今天的发展水平:8乘8的国际象棋盘其实是一个体现思维与创意的强大媒介。象棋里蕴含了天文数字般的变化。卡斯帕罗夫指出,国际象棋的合法棋步共有1040。在棋局里每算度八步棋,里面蕴含的变化就已经超过银河系里的繁星总数。而地球上很少有任何数量达到这个级别。在金融危机之前,全世界的财富总和大约是1014人民币,而地球人口只有1010。棋盘上,所有可能的棋局总数达到10120,这超过了宇宙里所有原子的总数!经典语录网