string转int的⽅法_Spark——scala实⽤⼩⽅法
这⼀阵刚刚接触scala,主要也是⽤在spark上~完全⼩⽩⼀个,看着Scala感觉与python很像,想着可能⽐较容易上⼿,结果……真是需要处理⼀个就得查⼀个啊,⽤python或Java很容易写出来的代码,⽤scala得查半天,晕死……为了⽅便记忆,现在将这⼀阵⽤到的Scala记录⼀下,也给同样是⼩⽩的你⼀些参考,⼤神请⾃⾏飘过~~~
很多⼈都说scala要⽐Java⽅便的多,可能我熟悉之后也会这么认为吧,但是⽬前真是……突然想到了⼆哈伸⾆头歪头的图⽚,哈哈~
1. 时间戳、字符串互转
A. 时间戳转时间字符串
SimpleDateFormat
def tranTimeToString(tm:Long) :String={
val fm = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")
val tim = fm.format(new Date(tm))
tim // 返回值 tim
}
=>println(tranTimeToString(1502036122000L))
=>2017-08-07 00:15:22
B. 字符串转时间戳,时间字符串格式:yyyy-MM-dd HH:mm:ss
import java.util.Locale
SimpleDateFormat
def strToTranTime(tm: String): Long = {
val loc = new Locale("en")
val fm = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss",loc) val dt2 = fm.parse(tm) // 转成Date类型
}
=>println(strToTranTime("2019-12-18 15:30:12"))
=>1576654212000
C. 获取传⼊时间戳的天数差
import java.util.Calendar
def caculate2Days(t1: Long, t2: Long): Int = {
val calendar = Instance
calendar.setTimeInMillis(t2)
val t2Day = (Calendar.DAY_OF_YEAR)
calendar.setTimeInMillis(t1)
val t1Day = (Calendar.DAY_OF_YEAR)
var days = t2Day - t1Day
if (days < 0) {
caculate2Days(t2, t1)
}
days
}
=>println(caculate2Days(1576664164000l, 1576684800000l)) =>1 (相差1天)
2. 队列Queue
llection.mutable.Queue
A. 队列初始化N个0
def queueInit(N: Int): Queue[Int] = {
var list = List.fill(N)(0) // 先初始化N个0的list
var queue = Queue[Int]() // 定义队列
queue ++= list // 将list转成队列
queue
}
=>println(queueInit(5))
=>Queue(0, 0, 0, 0, 0)
B. 队列增加元素,保持队列总长度不变
def queueAdd(queue: Queue[Int], value: Int): Queue[Int] ={
queue.dequeue() // 删掉第⼀个元素
queue += value // 增加⼀个元素
queue
}
=>var q = queueInit(5)
=>println(q)
=>Queue(0, 0, 0, 0, 0)
=>q = queueAdd(q, 1)
string转date的方法=>println(q)
=>Queue(0, 0, 0, 0, 1)
C. 队列求和
def queueSum(list: Queue[Int]): Int = {
var sum = 0
for(i <- list){
sum += i
}
sum
}
=> println(queueSum(q))
=>1
D. 队列转字符串
def queueToString(list: Queue[Int]): String = {
val b = list.mkString(",")
b
}
=>println(queueToString(q))
=>0,0,0,0,1
3. int列表、Byte列表互转
A. int列表转Byte列表
注:转成Array[Byte],主要是因为写⼊hbase的时候只能Array[Byte]类型写⼊
def listToBytes(list: List[Int]): Array[Byte] = {
val b = list.mkString(",") // 转成字符串
val c = b.getBytes() // 转Array[Byte]
c
}
=>val a= List(0,0,1)
=>val c = listToBytes(a)
=>println(c)
=>[B@22a67b4
B. Byte列表转int列表
def bytesToList(c: Array[Byte]): List[Int] = {
var e : List[Int] = List() // 初始化list
if(c != null && !c.isEmpty){
val d = String(c) // Array[Byte]转成字符串
e = d.split(",").map(_.toInt).toList // 转成int列表
}
e
}
=>println(bytesToList(c))
=>List(0, 0, 1)
4. 字符串的md5值
def hashMD5(content: String): String = {
val md5 = Instance("MD5")
val encoded = md5.digest((content).getBytes)
encoded.map("%02x".format(_)).mkString
}
=>println(hashMD5("9001244008859"))
=>fa5b6a9ab0a0a6aae6e0151a88596b4d
5. 数据写⼊ES
注:⼀定要先配置ES索引
import org.apache.spark.SparkConf
import org.apache.spark.streaming.{Duration, StreamingContext}
def dataToEs():Unit = {
val conf = new SparkConf().setAppName("dataToEs").setMaster("local")
// conf.set("es.ate", "true") // ⾃动建_id
conf.set("cluster.name", "es")
conf.set("es.nodes", "xxx") // host
conf.set("es.port", "xxx") // port
conf.set("es.http.auth.user", "xxx") // user
conf.set("es.http.auth.pass", "xxx") // password
conf.set("ad.pty","true")
conf.set("es.ly","true")
conf.set("es.write.operation","upsert") // 有->更新,没有->添加
val sc = new SparkContext(conf)
val ssc = new StreamingContext(sc, new Duration(10 * 1000))
val appId = "2"
val orgId = "3"
val subscribeType = "4"
val numbers = Map("appId" -> appId, "orgId" -> orgId, "subscribeType" -> subscribeType)
ssc.sparkContext.makeRDD( // 或 sc.makeRDD(
Seq(numbers) // 多个时⽤逗号,Seq(numbers, airports)
).saveToEs("$index/$type", Map("es.mapping.id" -> "orgId")) // 填⼊ES的index和type
}
saveToEs的这种⽅式在本机可以调⽤通,但是上到集,⼜有分区什么的时候,就不⼀定好⽤了,我就是遇到了这种问题,最后⽆奈使⽤Java代码完成的导⼊ES。
6. 字符串分隔成list
def test(): Unit = {
var page = "aa/submitinfo1033/bb/cc"
var elems = List[String]()
var idsList = page.split("/") // 分隔
var id = ""
for(j <- idsList.indices) { // for遍历
breakable{
if(idsList(j).equals("bb")) break else{ // 跳过“bb”
id += idsList(j) + "="
}
}
}
println(id)
}
=>test()
=>aa=submitinfo1033=cc=
scala中没有continue关键字,所以利⽤break实现continue的功能。
⽤scala的时候还遇到了很多问题,⽬前也还在遇到 ~以后再慢慢整理吧~先整理这些,希望对你有些帮助~~~有问题或建议请留意哦~
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论