从spark sq中选择特定元素时出错

时间:2017-08-22 16:09:27

标签: apache-spark apache-spark-sql

我正在尝试使用spark sql读取Json并打印值

实际上我只想选择数组的第0个元素

当我尝试选择完整的数组时,它可以工作,但是当我尝试从数组中选择第0个元素时它会失败

val events = sc.parallelize("""{"toid":"osgb4000000031043205","point":[508180.748,195333.973],"index":1}""" :: Nil)

 val schema = new StructType().add("toid",StringType,true).add("point",DataTypes.createArrayType(DoubleType,true),true).add("index",StringType,true)

val df = sqlContext.read.schema(schema).json(events)

df.show 

 +--------------------+--------------------+-----+
 |                toid|               point|index|
 +--------------------+--------------------+-----+
 |osgb4000000031043205|[508180.748, 1953...|    1|
 +--------------------+--------------------+-----+


scala> df.select("point")[0].show
<console>:1: error: identifier expected but integer literal found.
   df.select("point")[0].show
                      ^

scala> df.select("point")(0).show
<console>:48: error: type mismatch;
 found   : Int(0)
 required: String
          df.select("point")(0).show

有人可以帮我解决这个错误吗?

1 个答案:

答案 0 :(得分:1)

您应该通过在<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta/css/bootstrap.min.css" integrity="sha384-/Y6pD6FV/Vv2HJnA6t+vslU6fwYXjCFtcEpHbNJ0lyAFsXTsjBbfaDjzALeQsN6M" crossorigin="anonymous"> <script src="https://code.jquery.com/jquery-3.2.1.slim.min.js" integrity="sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN" crossorigin="anonymous"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.11.0/umd/popper.min.js" integrity="sha384-b/U6ypiBEHpOf/4+1nzFpr53nxSS+GLCkfwBdFNTxtclqqenISfwAzpKaMNFNmj4" crossorigin="anonymous"></script> <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta/js/bootstrap.min.js" integrity="sha384-h0AbiXch4ZDo7tp9hKZ4TsHbi047NrKGLO3SEJAg45jXxnGIfYzk4Si90RDIqNm1" crossorigin="anonymous"></script> <div id="myModal" class="modal fade" role="dialog" style="z-index: 150;"> <div class="modal-dialog" style="z-index: 151;"> <div class="modal-content" style="z-index: 152;"> <div class="modal-header" style="z-index: 153;"> <button type="button" class="close" data-dismiss="modal">&times;</button> <h4 class="modal-title">Modal Header</h4> </div> <div class="modal-body" style="z-index: 153;"> <label>Hello @Model.UserName</label> </div> <div class="modal-footer" style="z-index: 153;"> <button type="button" class="btn btn-default" data-dismiss="modal">Close</button> </div> </div> </div> </div> <div id="myModal2" class="modal fade" role="dialog"> <div class="modal-dialog"> <div class="modal-content"> <div class="modal-header"> <button type="button" class="close" data-dismiss="modal">&times;</button> <h4 class="modal-title">Modal Header</h4> </div> <div class="modal-body"> <label>Hello @Model.UserName</label> </div> <div class="modal-footer"> <button type="button" class="btn btn-default" data-dismiss="modal">Close</button> </div> </div> </div> </div> <button type="button" class="btn btn-info btn-lg" data-toggle="modal" data-target="#myModal2">Open Modal without Style attribute</button> <button type="button" class="btn btn-info btn-lg" data-toggle="modal" data-target="#myModal">Open Modal with Style attribute</button>函数中取第0个元素而不是在select函数之外来执行以下操作。

select

您甚至可以使用别名

创建列名
df.select($"point"(0)).show

我希望答案很有帮助