首先創(chuàng)建一個項目:
要在javascalaApplication這文件的所屬目錄下寫的程序才有效,其他html css jsp js等資源放在resources調(diào)用庶柿。
這里展現(xiàn)了我寫的一個controller層的細(xì)節(jié):
@RequestMapping("/hi.html")
public String wordCount(HttpServletRequest request, Model model){
String text=request.getParameter("hi");
if(text!=null){
System.out.println("進(jìn)來了");
String[] s=text.split(",");
Map<String,Object> word=wordCount.go(s);
System.out.println(word);
model.addAttribute("data",word);
}
return "hi.html";
}
wordCount函數(shù)是scala村怪,需要引用編寫scala的依賴,并且引用寫好的類需要通過import
<dependency>
<groupId>org.scala-lang</groupId>
<artifactId>scala-library</artifactId>
<version>2.12.11</version>
</dependency>
<form action="#" th:action="@{/hi.html}" method="post" class="basic-grey">
<p style="color:black;font-weight:bold;">請輸入一些詞浮庐,我?guī)湍?wordCount :</p>
<p style="color:grey">輸入的例子:什么字符都行(原理 :空格隔開字符甚负,然后統(tǒng)計,多少個空格都行) 諸如輸入: go 屎 @ 屎 得到的結(jié)果是{go:1,屎:2,@:1}</p>
<p> <textarea type="text" name="hi" id="hi"></textarea></p>
<!--/*@thymesVar id="data" type=""*/-->
<p><input type="submit" value="確定" /> <input type="reset" value="重置" /></p>
<span style="color:black;font-weight:bold;">結(jié)果在這 -----》》》</span>
<span th:text="${data}">default message</span>
</form>
這里通過"${data}"獲取到controller層返回model夾帶的值
這里注意一下跳轉(zhuǎn)的地址的寫法审残,這是thymeleaf的寫法(springboot默認(rèn)搭配thymeleaf前端顯示)
thymeleaf 在html頁面的基礎(chǔ)上建立:
<html lang="en" xmlns:th="http://www.thymeleaf.org">
這項目是后臺是運行了spark進(jìn)行計算的梭域,所以需要導(dǎo)進(jìn)spark相關(guān)依賴:
<dependency>
<groupId>org.apache.spark</groupId>
<artifactId>spark-core_2.12</artifactId>
<version>2.4.4</version>
</dependency>
舉個用了spark的例子:
def fileGo(s:String): java.util.Map[String,Int]={
val rdd=sc.textFile("hdfs://172.18.30.253"+s)
print(rdd.collect())
val rdd1=rdd.flatMap(line => line.replaceAll(""""[,.!?']""","").split("""\s+"""))
val rdd2=rdd1.map(word => (word,1)).reduceByKey((a,b) => a+b)
val map:java.util.Map[String,Int]=rdd2.collectAsMap().asJava
map
}
在scala中使用正則表達(dá)式需要用 三引號。
上傳到hdfs上搅轿,使用到hadoop碰辅,需要導(dǎo)入的依賴:
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-client</artifactId>
<version>2.7.3</version>
</dependency>
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-common</artifactId>
<version>2.7.3</version>
</dependency>
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-hdfs</artifactId>
<version>2.7.3</version>
</dependency>
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-mapreduce-client-core</artifactId>
<version>2.7.3</version>
</dependency>
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-mapreduce-client-jobclient</artifactId>
<version>2.7.3</version>
</dependency>
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-mapreduce-client-common</artifactId>
<version>2.7.3</version>
</dependency>
學(xué)到的東西:
1.scala讀文件
import scala.io.Source
object Test {
def main(args: Array[String]) {
println("文件內(nèi)容為:" )
Source.fromFile("test.txt" ).foreach{
print
}
}
}
@RequestBody主要用來接收前端傳遞給后端的json字符串中的數(shù)據(jù)的(請求體中的數(shù)據(jù)的);
GET方式無請求體,所以使用@RequestBody接收數(shù)據(jù)時介时,前端不能使用GET方式提交數(shù)據(jù)没宾,而是用POST方式進(jìn)行提交凌彬。
如果參數(shù)前不寫@RequestParam(xxx)的話,那么就前端可以有可以沒有對應(yīng)的xxx名字才行循衰,如果有xxx名的話铲敛,那么就會自動匹配;沒有的話会钝,請求也能正確發(fā)送伐蒋。
@RequestBody直接以String接收前端傳過來的json數(shù)據(jù)
例如:
@RequestBody也可以直接接收對象,不過傳入的json對象的迁酸,key-value要有對應(yīng)對象的屬性名先鱼。
3.學(xué)到了windows系統(tǒng)下關(guān)閉端口:
netstat -ano|findstr 端口號:查看特定端口被占用情況
關(guān)閉占用端口的程序 命令 :taskkill /pid PID(進(jìn)程號) /f
4.學(xué)到了上傳文件,及其解析:
<div style="color:blue;font-weight:bold;">下面上傳你要進(jìn)行我靠的文件:</div><input type="file" name="file" />
controlller層解析:
@RequestMapping("/uploadWordCount")
public String upload(@RequestParam("file") MultipartFile file,Model model,HttpServletRequest request) throws IOException {
if (!file.isEmpty()) {
Configuration conf = new Configuration();
FileSystem fs = FileSystem.get(conf);
//獲取文件的詳情
System.out.println("文件類型ContentType=" + file.getContentType());
System.out.println("文件組件名稱Name=" + file.getName());
System.out.println("文件原名稱OriginalFileName=" + file.getOriginalFilename());
System.out.println("文件大小Size=" + file.getSize() / 1024 + "KB");
model.addAttribute("data","hi");
String path = request.getServletContext().getRealPath("/upload/");
System.out.println("path = " + path);
String filename = file.getOriginalFilename();
File filepath = new File(path, filename);
// 判斷路徑是否存在奸鬓,不存在則新創(chuàng)建一個
if (!filepath.getParentFile().exists()) {
filepath.getParentFile().mkdirs();
}
// 將上傳文件保存到目標(biāo)文件目錄
String start=path+filename;
String end="/upload/"+filename;
//把文件上傳到本地路徑
file.transferTo(new File(path + File.separator + filename));
Path srcPath = new Path(start);
Path dstPath = new Path(end);
//把本地文件上傳到hdfs上
fs.copyFromLocalFile(srcPath,dstPath);
System.out.println(end);
Map<String,Object> word=wordCount.fileGo(end);
System.out.println(word);
model.addAttribute("data",word);
}
return "uploadWordCount";
}
總結(jié)下文件上傳的要點:
1焙畔、表單method設(shè)置為post,并將enctype設(shè)置為 multipart/form-data
2串远、文件映射為MultipartFile對象進(jìn)行解析
3宏多、上傳文件大小 spring.http.multipart.max-file-size 限制
假如發(fā)生:
org.apache.tomcat.util.http.fileupload.FileUploadBase$FileSizeLimitExceededException: The field file exceeds its maximum permitted size of 1048576 bytes.
這個錯誤是由于springboot默認(rèn)的文件大小是1MB造成的,當(dāng)上傳文件超過1MB時就會報錯澡罚。解決這個報錯可以在application.properties中設(shè)置上傳參數(shù)伸但,參數(shù)項是默認(rèn)的,我們設(shè)置最大上傳文件大小不超過10MB留搔,再次上傳會成功更胖。
spring.http.multipart.max-file-size=10MB
spring.http.multipart.max-request-size=10MB
5.在scala得到的結(jié)果怎樣轉(zhuǎn)化為java的對象傳出去:
//轉(zhuǎn)為java map類型傳出
val map:java.util.Map[String,Int]=rdd2.collectAsMap().asJava
//轉(zhuǎn)為java list類型傳出
val list: List[Int] = rdd.collect().toList.asJava
6.XXXXXXX-1.0-SNAPSHOT.jar 中沒有主清單屬性的解決辦法
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
7."錯誤: 找不到或無法加載主類 springboot.Application"
跳轉(zhuǎn)到項目目錄下執(zhí)行’mvn clean install’,命令后重新運行該項目即可
8.下載hadoop.dll
https://github.com/steveloughran/winutils