码迷,mamicode.com
首页 > 数据库 > 详细

SparkSQL使用之JDBC代码访问Thrift JDBC Server

时间:2014-09-13 18:42:05      阅读:497      评论:0      收藏:0      [点我收藏+]

标签:style   blog   color   io   os   使用   java   ar   for   

启动ThriftJDBCServer:

cd $SPARK_HOME/sbin
start-thriftserver.sh &

 

使用jdbc访问ThriftJDBCServer代码段:

package com.luogankun.spark.sql

import java.sql.DriverManager
import java.sql.Connection
import java.sql.PreparedStatement

/**
 * JDBC代码访问Thrift JDBC Server
 * @author luogankun
 */
object JdbcThriftServer {
  def main(args: Array[String]) {
    Class.forName("org.apache.hive.jdbc.HiveDriver")

    val conn = DriverManager.getConnection("jdbc:hive2://hadoop000:10000/default", "hadoop", "")
    val pstat = conn.prepareStatement("SELECT track_time, url, session_id, referer, ip, end_user_id, city_id FROM page_views WHERE city_id = -1000 limit 10")
    val rs = pstat.executeQuery()
    while (rs.next()) {
      println("track_time: " + rs.getString("track_time")
        + ", url: " + rs.getString("url")
        + ", session_id: " + rs.getString("session_id")
        + ", referer: " + rs.getString("referer")
        + ", ip: " + rs.getString("ip")
        + ", end_user_id: " + rs.getString("end_user_id")
        + ", city_id: " + rs.getString("city_id"))

    }
    rs.close()
    pstat.close()
    conn.close()
  }
}

 

SparkSQL使用之JDBC代码访问Thrift JDBC Server

标签:style   blog   color   io   os   使用   java   ar   for   

原文地址:http://www.cnblogs.com/luogankun/p/3970124.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!