Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/scala/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/apache-spark/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Scala 通过.filter或create方法在RDD中添加元素?_Scala_Apache Spark - Fatal编程技术网

Scala 通过.filter或create方法在RDD中添加元素?

Scala 通过.filter或create方法在RDD中添加元素?,scala,apache-spark,Scala,Apache Spark,我想过滤一个.txt文件来创建RDD并生成统计数据。 filter方法(.filter)允许我创建RDD,但我限制了它,因为我的能力非常有限 Il要计算包含以下内容的字数: special characters >=3 uppercase >=1 lowercase >=1 过滤器使用示例: scala> val data = sc.textFile("file.txt") scala> val specialChars = List('*', '@

我想过滤一个.txt文件来创建RDD并生成统计数据。 filter方法(.filter)允许我创建RDD,但我限制了它,因为我的能力非常有限

Il要计算包含以下内容的字数:

special characters >=3
uppercase >=1
lowercase >=1
过滤器使用示例:

   scala> val data = sc.textFile("file.txt") 
   scala> val specialChars = List('*', '@', '&', '=', '#', '?', '!', '%', '+', '-', '<', '>', ' ', ',', '_', '$', '"', '[', ']', ';', ''', '(', ')', '.', '/') 
   scala> val upper = List('A' to 'Z')
   scala> val lower = List('a' to 'z')           
   scala> val data_low = data.filter(_.length < 13).filter(line => !specialChars.exists(char => line.contains(char)))
scala>val data=sc.textFile(“file.txt”)
scala>val specialChars=List(“*”、“@”、“&”、“=”、“#”、“?”、“!”、“%”、“+”、“-”、“、”、“、”、“$”、“、”、“[”、“]、“;”、“(”、“)、“/”)
scala>val upper=List('A'到'Z')
scala>val lower=List('a'到'z')
scala>val data\u low=data.filter(\u.length<13).filter(line=>!specialChars.exists(char=>line.contains(char)))
这是我的另一种方法,但我不知道如何在RDD中实现结果(这里用println演示)

scala> for (line <- data) {
     | var spe_sum = 0;
     | for (c <- specialChars) {
     | spe_sum = spe_sum + line.count(_ == c);
     | }
     | if (spe_sum >= 3 & nombre.exists(char => line.contains(char)) & maj.exists(char => line.contains(char)) & minus.exists(char => line.contains(char))) {
     | println(line);
     | }
     | }
scala>for(line.contains(char))&maj.exists(char=>line.contains(char))&minus.exists(char=>line.contains(char))){
|println(行);
| }
| }

是否可以在执行相同结果的.filter或write.filter中执行我的代码?

欢迎使用堆栈溢出

filter
方法在您提供的列表上循环,并使用您提供的函数测试集合的每个元素。您的函数必须返回
true
false
,并且
filter
返回函数返回
true
的列表元素e集合中的元素

实现结果的方法如下

val rdd: RDD[String] = // load your RDD and tokenize each word
val specialChars = List('*', '@', '&', '=', '#', '?', '!', '%', '+', '-', '<', '>', ' ', ',', '_', '$', '"', '[', ']', ';', ''', '(', ')', '.', '/')
val upper = ('A' to 'Z')
val lower = ('a' to 'z')

// count the words satysfying all constraints
rdd.map(_.toList) // map each word to a list of chars
   .filter(x => specialChars.intersect(x).size > 2 && upper.intersect(x).nonEmpty && lower.intersect(x).nonEmpty)
   .count()

// count the words that satisfies at least a constraint
rdd.map(_.toList)
   .map(x => // map each word to a tuple of three elements, each element is to 1 if it satisfies the respective constraint
             (if (specialChars.intersect(x).size > 2) 1 else 0, // check the intersection with special characters
              if (upper.intersect(x).nonEmpty) 1 else 0,  // check the intersection with upper-case characters
              if (lower.intersect(x).nonEmpty) 1 else 0)) // check the intersection with lower-case characters
   .reduce((a, b) => (a._1 + b._1, a._2 + b._2, a._3 + b._3)) // sum up the results
val-rdd:rdd[String]=//加载您的rdd并标记每个单词
val specialChars=List(“*”、“@”、“&”、“=”、“#”、“?”、“!”、“%”、“+”、“-”、“、”、“、”、“$”、“、”、“[”、“]”、“;”、“(”、“)、“/”)
val上限=('A'到'Z')
下限值=('a'到'z')
//计算所有约束条件下的satysysing单词数
map(u.toList)//将每个单词映射到一个字符列表
.filter(x=>specialChars.intersect(x).size>2&&upper.intersect(x).nonEmpty&&lower.intersect(x).nonEmpty)
.count()
//计算至少满足约束条件的单词数
rdd.map(u.toList)
.map(x=>//将每个单词映射到一个由三个元素组成的元组,如果每个元素满足相应的约束,则每个元素都是1
(如果(specialChars.intersect(x.size>2)1或0,//使用特殊字符检查相交
如果(upper.intersect(x.nonEmpty)1为0,则//使用大写字符检查相交
if(lower.intersect(x.nonEmpty)1 else 0))//检查带有小写字符的交集
.reduce((a,b)=>(a._1+b._1,a._2+b._2,a._3+b._3))//总结结果

结果元组的第一个元素是包含3个以上特殊字符的行数,第二个元素是包含至少一个大写字符的行数,第三个元素是包含至少一个小写字符的行数

哇,很清楚了,谢谢你
.intersect()
这非常有用。如果这是您问题的正确解决方案,那么您应该接受这一回答:)()