如何从对象元组数组中访问对象的成员

时间:2018-03-18 19:09:17

标签: scala apache-spark tuples

对象类是

class VertexAttributes(val m: Boolean, n: Any){

        val rootParentCustNumber: String = if(n == null) "Was Null" else n.toString
        val firstMsgFlg = m

}

我有这个对象类型的RDD:

scala> myGraph.vertices
res92: org.apache.spark.graphx.VertexRDD[VertexAttributes] = VertexRDDImpl[2280] at RDD at VertexRDD.scala:57

过滤RDD,我得到以下内容:

scala> res92.filter{case(k,m) => k == 964088677}.collect
res94: Array[(org.apache.spark.graphx.VertexId, VertexAttributes)] = Array((964088677,VertexAttributes@2612b83f))

如何在VertexAttributes@2612b83f.rootParentCustNumber

中访问Array((964088677,VertexAttributes@2612b83f))

我试过了res92.filter{case(k,m) => k == 964088677}.map{case Array(k,m)=> m.rootParentCustNumber}

但是我收到以下错误:

<console>:243: error: pattern type is incompatible with expected type;
 found   : Array[T]
 required: (org.apache.spark.graphx.VertexId, VertexAttributes)
    (which expands to)  (Long, VertexAttributes)
       res92.filter{case(k,m) => k == 964088677}.map{case Array(k,m)=> m.rootParentCustNumber}
                                                               ^

1 个答案:

答案 0 :(得分:1)

过滤阶段不会改变RDD的类型(fieldset, label { margin: 0; padding: 0; margin-bottom: 20px; } .rating { border: none; float: left; } .rating>input { display: none; } .rating>label:before { margin: 5px; font-size: 1.25em; font-family: FontAwesome; display: inline-block; content: "\f005"; } .rating>.half:before { content: "\f089"; position: absolute; } .rating>label { color: #ddd; float: right; } .rating>input:checked~label, /* show gold star when clicked */ .rating:not(:checked)>label:hover, /* hover current star */ .rating:not(:checked)>label:hover~label { color: #FFD700; } /* hover previous stars in list */ .rating>input:checked+label:hover, /* hover current star when changing rating */ .rating>input:checked~label:hover, .rating>label:hover~input:checked~label, /* lighten current selection */ .rating>input:checked~label:hover~label { color: #FFED85; })。

因此,您可以使用地图阶段管理过滤阶段的返回RDD,并以与过滤阶段相同的方式处理每个记录:

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<link href="https://maxcdn.bootstrapcdn.com/font-awesome/4.2.0/css/font-awesome.min.css" rel="stylesheet"/>

<fieldset class="rating">
<input type="hidden" value="1">
  <input type="radio" id="5star" name="rating" value="5" />
  <label class="full" for="5star" title="Excellent"></label>

  <input type="radio" id="4halfstar" name="rating" value="4.5" />
  <label class="half" for="4halfstar" title="Good"></label>

  <input type="radio" id="4star" name="rating" value="4" />
  <label class="full" for="4star" title="Pretty good"></label>

  <input type="radio" id="3halfstar" name="rating" value="3.5" />
  <label class="half" for="3halfstar" title="Nice"></label>

  <input type="radio" id="3star" name="rating" value="3" />
  <label class="full" for="3star" title="Ok"></label>

  <input type="radio" id="2halfstar" name="rating" value="2.5" />
  <label class="half" for="2halfstar" title="Kinda bad"></label>

  <input type="radio" id="2star" name="rating" value="2" />
  <label class="full" for="2star" title="Bad"></label>

  <input type="radio" id="1halfstar" name="rating" value="1.5" />
  <label class="half" for="1halfstar" title="Meh"></label>

  <input type="radio" id="1star" name="rating" value="1" />
  <label class="full" for="1star" title="Umm"></label>

  <input type="radio" id="halfstar" name="rating" value="0.5" />
  <label class="half" for="halfstar" title="Worst"></label>

</fieldset>

<br><br>

<fieldset class="rating">
<input type="hidden" value="2">
  <input type="radio" id="second-5star" name="rating" value="5" />
  <label class="full" for="second-5star" title="Excellent"></label>

  <input type="radio" id="second-4halfstar" name="rating" value="4.5" />
  <label class="half" for="second-4halfstar" title="Good"></label>

  <input type="radio" id="second-4star" name="rating" value="4" />
  <label class="full" for="second-4star" title="Pretty good"></label>

  <input type="radio" id="second-3halfstar" name="rating" value="3.5" />
  <label class="half" for="second-3halfstar" title="Nice"></label>

  <input type="radio" id="second-3star" name="rating" value="3" />
  <label class="full" for="second-3star" title="Ok"></label>

  <input type="radio" id="second-2halfstar" name="rating" value="2.5" />
  <label class="half" for="second-2halfstar" title="Kinda bad"></label>

  <input type="radio" id="second-2star" name="rating" value="2" />
  <label class="full" for="second-2star" title="Bad"></label>

  <input type="radio" id="second-1halfstar" name="rating" value="1.5" />
  <label class="half" for="second-1halfstar" title="Meh"></label>

  <input type="radio" id="second-1star" name="rating" value="1" />
  <label class="full" for="second-1star" title="Umm"></label>

  <input type="radio" id="second-halfstar" name="rating" value="0.5" />
  <label class="half" for="second-halfstar" title="Worst"></label>

</fieldset>

我认为你已经被收集阶段误导了,它将RDD转换为数组。