SPARk连接

这篇博客介绍了如何在Scala中使用Source类读取文件,包括按字符、单词和网络文件的方式。同时,文章涉及了SparkConf和SparkContext的配置,Kafka生产者的连接方法,以及Akka的远程Actor连接示例。
摘要由CSDN通过智能技术生成
a.readChar() 读取输入字符

println(“请输入任意字符:”)
val ch = StdIn.readChar()

//scala中如何读取文件中的内容
val lines = Source.fromFile("E:/data/hello.txt").getLines()

Source源文件,地址,getLines()转化流数据成迭代数据

隐式转换中

rich 这个类是什么 引用在文件读取richFile 和 比较器接口中comparable

导入scala.io.Source后 即可引用Source中的方法读取文件信息

import scala.io.Source
object FileDemo extends App{
val source = Source.fromFile(“E:/data/hello.txt”)
//返回一个迭代器
val lines = source.getLines()
for(i <- lines)
println(i)
//内容也可以放到数组中
// val arr = source.getLines().toArray
// for(elem <- arr)
// println(elem)
//文件内容直接转换成一个字符串
// val contents = source.mkString
// println(contents)
}

读取字符

按字符读取文件
import scala.io.Source
object FileDemo extends App{
val source = Source.fromFile(“E:/data/hello.txt”)
for(c <- source)
println©
}

读取单词

将文件中的内容 转换成一个单词数组

import scala.io.Source
object FileDemo extends App{
val source = Source.fromFile(“E:/data/hello.txt)
val contents = source.mkString.split(” ") //转化为string然后切割成单词
for(word <- contents)
println(word)
}

读取网络文件

Source 可以直接读取来自URL、等非文件源的内容

import scala.io.Source
object FileDemo extends App{
val source = Source.fromURL(“http://www.baidu.com”, “UTF-8”)
val lines = source.getLines()
for(i <- lines)
println(i)
}

netty
//1.创建SparkConf对象进行配置,然后在创建SparkContext进行操作
 val conf = new SparkConf().setAppName("BaseStationDemo").setMaster("local[2]")
 val sc = new SparkContext(conf)
kafka 连接

连接生产者
获取配置ÿ

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值