Start Thriftjdbcserver:
CD $SPARK _home/sbinstart-thriftserver. SH &
To access the Thriftjdbcserver code snippet using JDBC:
PackageCom.luogankun.spark.sqlImportJava.sql.DriverManagerImportjava.sql.ConnectionImportjava.sql.PreparedStatement/*** JDBC Code access thrift JDBC Server *@authorLuogankun*/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 using JDBC code to access thrift JDBC Server