Advertisement
Guest User

Untitled

a guest
Mar 25th, 2016
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.25 KB | None | 0 0
  1. @ResponseBody
  2. public String index() throws Exception {
  3.  
  4. SparkConf conf = new SparkConf().setAppName("Simple Application")
  5. .setMaster("local");
  6.  
  7. JavaSparkContext sc = new JavaSparkContext(conf);
  8.  
  9. sc.setLogLevel("ERROR");
  10.  
  11. SQLContext sqlContext = new SQLContext(sc);
  12.  
  13. Map<String, String> options = new HashMap<String, String>();
  14. options.put("url",
  15. "jdbc:mysql://127.0.0.1:3306/msp?user=root&password=root");
  16. options.put("dbtable", "user_info");
  17.  
  18. DataFrame df = sqlContext.read().format("jdbc").options(options).load();
  19.  
  20. System.out.println(df.count());
  21. return "index";
  22. }
  23.  
  24. public static void main(String[] args) throws Exception {
  25.  
  26. File file = new File("/home/miaosipeng/ext_jars/spark-servlet.jar");
  27. Method method = URLClassLoader.class.getDeclaredMethod("addURL",
  28. URL.class);
  29. boolean accessible = method.isAccessible();
  30. try {
  31. if (accessible == false) {
  32. method.setAccessible(true);
  33. }
  34.  
  35. URLClassLoader classLoader = (URLClassLoader) ClassLoader
  36. .getSystemClassLoader();
  37.  
  38. URL url = file.toURI().toURL();
  39. try {
  40. method.invoke(classLoader, url);
  41. System.out.println("读取jar文件[name={}]" + file.getName());
  42. } catch (Exception e) {
  43. System.out.println("读取jar文件[name={}]失败" + file.getName());
  44. }
  45.  
  46. } finally {
  47. method.setAccessible(accessible);
  48. }
  49.  
  50. SparkConf conf = new SparkConf()
  51. .setAppName("Simple Application")
  52. .setJars(
  53. new String[] { "/home/miaosipeng/ext_jars/spark-servlet.jar" })
  54. .setMaster("local");
  55.  
  56. JavaSparkContext sc = new JavaSparkContext(conf);
  57.  
  58. sc.setLogLevel("ERROR");
  59.  
  60. SQLContext sqlContext = new SQLContext(sc);
  61.  
  62. Map<String, String> options = new HashMap<String, String>();
  63. options.put("url",
  64. "jdbc:mysql://127.0.0.1:3306/msp?user=root&password=root");
  65. options.put("dbtable", "user_info");
  66.  
  67. DataFrame df = sqlContext.read().format("jdbc").options(options).load();
  68.  
  69. System.out.println(df.count());
  70. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement