他的回复:
package com.huawei.bigdata.examples.spark;import java.io.File;import java.sql.DriverManager;import java.sql.PreparedStatement;import java.sql.ResultSet;import java.sql.ResultSetMetaData;import java.util.ArrayList;import org.apache.hadoop.conf.Configuration;import org.apache.hadoop.fs.Path;public class SparkJDBC { @SuppressWarnings("deprecation")public static void main(String[] args) throws Exception { String path; String os = System.getProperty("os.name"); if(os.toLowerCase().startsWith("win")){ path = "D:/projects/keytab/spark/"; }else{ path = "/sjzl/sparkTask/keytab/"; } String userPrincipal = "xx"; String userKeytabPath = path +"user.keytab"; String krb5ConfPath = path +"krb5.conf"; Configuration config = new Configuration(); config.addResource(new Path(path+"core-site.xml")); config.addResource(new Path(path+"hdfs-site.xml")); config.addResource(new Path(path+"hbase-site.xml")); LoginUtil.setJaasConf("Client", userPrincipal, new File(userKeytabPath).getAbsolutePath()); LoginUtil.setZookeeperServerPrincipal("zookeeper.server.principal", "zookeeper/hadoop.hadoop.com"); LoginUtil.login(userPrincipal, userKeytabPath, krb5ConfPath, config); //create the sql to create table ArrayList sqlList = new ArrayList(); sqlList.add("select count(*) from xx.test1 "); String securityConfig = ";saslQop=auth-conf;auth=KERBEROS;principal=spark2x/hadoop.hadoop.com@HADOOP.COM;user.principal="+userPrincipal+";user.keytab="+path +"user.keytab;"; String zkUrl = "xx:24002"; String zkNamespace = "/sparkthriftserver2x"; if (zkNamespace != null) { //从配置项中删除冗余字符 zkNamespace = zkNamespace.substring(1); } StringBuilder sb = new StringBuilder("jdbc:hive2://" + zkUrl + ";serviceDiscoveryMode=zooKeeper;zooKeeperNamespace=" + zkNamespace + securityConfig); String url = sb.toString(); executeSql(url, sqlList); } private static void executeSql(String url, ArrayList sqlList) throws Exception { try { Class.forName("org.apache.hive.jdbc.HiveDriver").newInstance(); } catch (Exception e) { e.printStackTrace(); } java.sql.Connection connection = null; PreparedStatement statement = null; try { connection = DriverManager.getConnection(url); for (int i =0 ; i sqlList.size(); i++) { String sql = sqlList.get(i); System.out.println("---- Begin executing sql: " + sql + " ----"); statement = connection.prepareStatement(sql); ResultSet result = statement.executeQuery(); ResultSetMetaData resultMetaData = result.getMetaData(); Integer colNum = resultMetaData.getColumnCount(); for (int j =1; j = colNum; j++) { System.out.println(resultMetaData.getColumnLabel(j) + "\t"); } System.out.println(); while (result.next()) { for (int j =1; j = colNum; j++){ System.out.println(result.getString(j) + "\t"); } System.out.println(); } System.out.println("---- Done executing sql: " + sql + " ----"); } } catch (Exception e) { e.printStackTrace(); } finally { if (null != statement) { statement.close(); } if (null != connection) { connection.close(); } } }}