scala wordcount,scalikejdbc,生成文件

wordcount

getlines

import scala.collection.mutable.{ArrayBuffer, ListBuffer}
import scala.io.Source



object WordCount {

  def main(args: Array[String]): Unit = {

    var file = Source.fromFile("F:\\asd.txt")
    var first = ArrayBuffer[String]()

    //返回List(asd asd asd, s d f g s, a),不加tolist就是個生成器,其實和下面循環做法是一樣的
//    print(file.getLines().toList)


    for (line <- file.getLines()){

       first += line
     //  println(first)
    }

//    println(first)
    var second = first.flatMap(_.split(" "))
   // println(second)
    //這裏用map返回的是個地址
//    var second2 = first.map(_.split((" ")))
//    println(second2)

    var third = second.map((_,1)).groupBy(_._1).mapValues(_.size).toList
   // println(third)

  }

}

scalalikejdbc

pom.xml

電腦用的scala版本是2.11.8

<properties>
    <scala.version>2.11.8</scala.version>
    <scalikejdbc.version>3.3.1</scalikejdbc.version>
  </properties>

鏡像網站,下載更快

<repository>
      <id>cloudera</id>
      <url>https://repository.cloudera.com/artifactory/cloudera-repos/</url>
    </repository>

放在dependencies裏的

<dependency>
      <groupId>org.scalikejdbc</groupId>
      <artifactId>scalikejdbc_2.11</artifactId>
      <version>${scalikejdbc.version}</version>
    </dependency>

    <dependency>
      <groupId>org.scalikejdbc</groupId>
      <artifactId>scalikejdbc-core_2.11</artifactId>
      <version>${scalikejdbc.version}</version>
    </dependency>
    <dependency>
      <groupId>org.scalikejdbc</groupId>
      <artifactId>scalikejdbc-config_2.11</artifactId>
      <version>${scalikejdbc.version}</version>
    </dependency>

    <dependency>
      <groupId>mysql</groupId>
      <artifactId>mysql-connector-java</artifactId>
      <version>5.1.27</version>
    </dependency>

如果使用DBs的方式去加載數據庫配置,默認在這個路徑下找加載文件

src/main/resources/application.conf

在這裏插入圖片描述

application.conf

db.default.driver="com.mysql.jdbc.Driver"
db.default.url="jdbc:mysql://hadoop000/ruozedb?characterEncoding=utf-8"
db.default.user="ruoze"
db.default.password="123456"

代碼

import scalikejdbc._
import scalikejdbc.config._


object scalalikejdbctask {
  //加載數據庫配置,從src/main/resources/application.conf加載
  DBs.setup()
  //自動選擇模式吧,全體指定,如果這裏不寫,要在每個sql的def裏指定
  implicit val session = AutoSession

  //注意這裏也是設置數據庫的配置,和上面DBs方式2選一
//  val driverClass = "com.mysql.jdbc.Driver"
//  val jdbcUrl = "jdbc:mysql://192.168.137.190:3306/ruozedb";
//  val username = "ruoze";
//  val pwd = "123456";
//  Class.forName(driverClass)
//  ConnectionPool.singleton(jdbcUrl, username, pwd)

  def main(args: Array[String]): Unit = {


        createTable("asd")
    //    insertable(12,"14yhuang")
    //    insertable(1,2,3)
    //      updatable(3,"ert")
    //      deletetable(id = 3)

    println(selectable2())
    //droptable

  }

  //這個注意給表名傳參的話,string類型帶"",相當於創建表時是"table",報錯
  //所以要用replace去替代掉",注意直接在sql語句後面跟replace會找不到這個方法
  //但是注意小寫sql會出這個問題,但是大寫SQL不會
  def createTable(af:String): Unit = {

//    var sqlcreate =
//      """
//        CREATE TABLE IF NOT EXISTS ${af}(
//                          id int PRIMARY KEY NOT NULL auto_increment,
//                          name varchar(64),
//                          created_time timestamp not null DEFAULT current_timestamp
//                       )ENGINE=InnoDB  DEFAULT CHARSET=utf8 AUTO_INCREMENT=1
//      """.stripMargin.replace("\"","")
//
//
//
//    sql"${sqlcreate}".execute.apply()

    SQL("""
      CREATE TABLE IF NOT EXISTS ${af}(
      id int PRIMARY KEY NOT NULL auto_increment,
      name varchar(64),
      created_time timestamp not null DEFAULT current_timestamp
      )ENGINE=InnoDB  DEFAULT CHARSET=utf8 AUTO_INCREMENT=1""").execute().apply()

// 錯誤事例
//    sql"""
//         CREATE TABLE IF NOT EXISTS (?)(
//                  id int PRIMARY KEY NOT NULL auto_increment,
//                  name varchar(64),
//                  created_time timestamp not null DEFAULT current_timestamp
//               )ENGINE=InnoDB  DEFAULT CHARSET=utf8 AUTO_INCREMENT=1
//      """.bind(af).execute.apply()
  }

  def insertable(id: Int, name: String) = {
    sql"insert into asd (id,name) values(${id},${name})".update().apply()
  }

  def insertable(id: Int*) = {
    id.foreach(
      id => sql"insert into asd (id) values (${id})".update().apply()
    )
  }

  //sql裏面不能有"",會報錯
  def updatable(id: Int, name: String) = {
    sql"update asd set name=${name} where id =${id}".execute().apply()
  }

  //研究發現execute和update沒什麼區別,execute返回時bool,update是int
  def deletetable(id: Int) = {
    sql"delete from asd where id=${id}".update().apply()
  }

  //這個sql後面不跟東西,返回的是個迭代器
  def selectable() = {
    //調用case class
    sql"select * from asd".map(rs => toselect(rs.int("id"), rs.string("name"), rs.string("created_time"))).list().apply()

  }

  //注意這裏因爲只要一個字段,而class是3個字段,所以不寫
  def selectable2() = {
    sql"select id from asd".map(rs => rs.int("id")).list().apply()

  }

  def selectable3() = {
//    bind也可以指定前面?的值,single如果返回的是多行數據,那麼會拋異常報錯
//    這種相當於先用object,自動調用apply方法,裏面的操作和上面的方式一樣的
    sql"select * from asd where id=(?)".bind(12).map((toselect(_))).list().single().apply()
  }

//相當於模板
  case class toselect(id: Int, name: String, create_time: String)

  object toselect extends SQLSyntaxSupport[toselect] {
    override val tableName = "toselect"

    def apply(rs: WrappedResultSet) = {
      new toselect(
        rs.int("id"), rs.string("name"), rs.string("created_time")
      )

    }

  }

  def droptable={
    sql"drop table if exists asd".update().apply()
  }

}

scala生成文件

PrintWriter
Random.nextInt
StringBuilder

import java.io.{File, PrintWriter}


import scala.io.Source
import scala.util.Random

object makefile{

  val urls = Array("https://www.baidu.com/",
    "https://fenian7788.github.io/",
    "https://me.csdn.net/weixin_39702831",
    "https://blog.csdn.net/umdfml00",
    "https://blog.csdn.net/Jaserok",
    "https://www.jianshu.com/u/32a08e1b7af0",
    "https://blog.csdn.net/Sylvia_D507",
    "https://blog.csdn.net/qq_29416019",
    "https://blog.csdn.net/qq_39892028",
    "https://blog.csdn.net/u010854024",
    "https://blog.csdn.net/qq_34382453",
    "https://blog.csdn.net/weixin_42733888"
  )

  def generateurl()={
    //左閉右開
    urls(Random.nextInt(urls.length))
  }

  val year =Array("2011", "2012", "2013", "2014", "2015", "2016", "2017", "2018", "2019")

  def main(args: Array[String]): Unit = {
    //該類可用來創建一個文件並向文本文件寫入數據
    val writer = new PrintWriter(new File("generatefile"))

    for (i <- 1 to 10){
      //random.nextint是(0到值)的random
      writer.println(generatefile())
    }
    //注意不能直接關,如果緩存區的數據沒有全部寫入,會丟失,這裏就是強制寫完
    writer.flush()
    //關了才能輸出在控制檯
    writer.close()

    val x = Source.fromFile("generatefile")
    println(x)

    }

  //生成文件每一行拼湊的內容
    def generatefile()={
      //一種string類,效率高但線程不安全,適合單線程
      val fl = new StringBuilder()
      fl.append(generateurl()).append("\t").append(generatetime)
        .append("\t").append(generateliuliang2(30))
     // fl.toString()
    }


    def generateliuliang(range:Int)={
      Random.nextInt(range)
    }

  //造數範圍0到9,如果range參數的隨機對3的餘數爲0,則造的數後面加-
    def generateliuliang2(range:Int)={
      if (Random.nextInt(range) % 3 ==0){
        generateliuliang(10).toString + "-"
      }
        else {
        generateliuliang(10)
      }
    }




    def generatetime={
      var date = new StringBuilder()
        date.append(year(Random.nextInt(year.length))).append("-")
      //至少兩位整數位,不足補0
        date.append("%02d".format(Random.nextInt(12)+1)).append("-")
        date.append("%02d".format(Random.nextInt(30)+1))
      if (Random.nextInt(30) % 4 ==0){
        date.append("-")
      }
      date
    }


  }

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章