Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- @ResponseBody
- public String index() throws Exception {
- SparkConf conf = new SparkConf().setAppName("Simple Application")
- .setMaster("local");
- JavaSparkContext sc = new JavaSparkContext(conf);
- sc.setLogLevel("ERROR");
- SQLContext sqlContext = new SQLContext(sc);
- Map<String, String> options = new HashMap<String, String>();
- options.put("url",
- "jdbc:mysql://127.0.0.1:3306/msp?user=root&password=root");
- options.put("dbtable", "user_info");
- DataFrame df = sqlContext.read().format("jdbc").options(options).load();
- System.out.println(df.count());
- return "index";
- }
- public static void main(String[] args) throws Exception {
- File file = new File("/home/miaosipeng/ext_jars/spark-servlet.jar");
- Method method = URLClassLoader.class.getDeclaredMethod("addURL",
- URL.class);
- boolean accessible = method.isAccessible();
- try {
- if (accessible == false) {
- method.setAccessible(true);
- }
- URLClassLoader classLoader = (URLClassLoader) ClassLoader
- .getSystemClassLoader();
- URL url = file.toURI().toURL();
- try {
- method.invoke(classLoader, url);
- System.out.println("读取jar文件[name={}]" + file.getName());
- } catch (Exception e) {
- System.out.println("读取jar文件[name={}]失败" + file.getName());
- }
- } finally {
- method.setAccessible(accessible);
- }
- SparkConf conf = new SparkConf()
- .setAppName("Simple Application")
- .setJars(
- new String[] { "/home/miaosipeng/ext_jars/spark-servlet.jar" })
- .setMaster("local");
- JavaSparkContext sc = new JavaSparkContext(conf);
- sc.setLogLevel("ERROR");
- SQLContext sqlContext = new SQLContext(sc);
- Map<String, String> options = new HashMap<String, String>();
- options.put("url",
- "jdbc:mysql://127.0.0.1:3306/msp?user=root&password=root");
- options.put("dbtable", "user_info");
- DataFrame df = sqlContext.read().format("jdbc").options(options).load();
- System.out.println(df.count());
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement