記一次spring boot開發(fā)wordCount

首先創(chuàng)建一個項目:


圖片.png
圖片.png

要在javascalaApplication這文件的所屬目錄下寫的程序才有效,其他html css jsp js等資源放在resources調(diào)用。

這里展現(xiàn)了我寫的一個controller層的細節(jié):

 @RequestMapping("/hi.html")
  public String wordCount(HttpServletRequest request, Model model){
        String text=request.getParameter("hi");
        if(text!=null){
            System.out.println("進來了");
            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>

圖片.png

圖片.png
<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默認搭配thymeleaf前端顯示)
thymeleaf 在html頁面的基礎(chǔ)上建立:
<html lang="en" xmlns:th="http://www.thymeleaf.org">

這項目是后臺是運行了spark進行計算的,所以需要導(dǎo)進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中使用正則表達式需要用 三引號。

上傳到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>

學到的東西:
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方式進行提交。
如果參數(shù)前不寫@RequestParam(xxx)的話,那么就前端可以有可以沒有對應(yīng)的xxx名字才行,如果有xxx名的話,那么就會自動匹配;沒有的話,請求也能正確發(fā)送。
@RequestBody直接以String接收前端傳過來的json數(shù)據(jù)
例如:


圖片.png

圖片.png

@RequestBody也可以直接接收對象,不過傳入的json對象的,key-value要有對應(yīng)對象的屬性名。


圖片.png

3.學到了windows系統(tǒng)下關(guān)閉端口:
netstat -ano|findstr 端口號:查看特定端口被占用情況
關(guān)閉占用端口的程序 命令 :taskkill /pid PID(進程號) /f

4.學到了上傳文件,及其解析:

    <div style="color:blue;font-weight:bold;">下面上傳你要進行我靠的文件:</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();
            }

            // 將上傳文件保存到目標文件目錄
            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對象進行解析
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默認的文件大小是1MB造成的,當上傳文件超過1MB時就會報錯。解決這個報錯可以在application.properties中設(shè)置上傳參數(shù),參數(shù)項是默認的,我們設(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

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

友情鏈接更多精彩內(nèi)容