2017-02-25 19 views
5

我有Dataset<Tuple2<String,DeviceData>>並希望將其轉換爲Iterator<DeviceData>如何將數據集<Tuple2 <String,DeviceData >>轉換爲迭代器<DeviceData>

下面是我的代碼,我使用collectAsList()方法,然後得到Iterator<DeviceData>

Dataset<Tuple2<String,DeviceData>> ds = ...; 
List<Tuple2<String, DeviceData>> listTuple = ds.collectAsList(); 

ArrayList<DeviceData> myDataList = new ArrayList<DeviceData>(); 
for(Tuple2<String, DeviceData> tuple : listTuple){ 
    myDataList.add(tuple._2()); 
} 

Iterator<DeviceData> myitr = myDataList.iterator(); 

我不能使用collectAsList()作爲我的數據是巨大的,它會妨礙性能。我看着數據集API,但無法獲得任何解決方案。我GOOGLE了它,但無法找到任何答案。有人能指導我嗎?如果解決方案是在Java中,那將是很好的。謝謝。

編輯:

DeviceData類是簡單的JavaBean。這裏是ds的printSchema()輸出。

root 
|-- value: string (nullable = true) 
|-- _2: struct (nullable = true) 
| |-- deviceData: string (nullable = true) 
| |-- deviceId: string (nullable = true) 
| |-- sNo: integer (nullable = true) 

回答

1

您可以直接提取dsDeviceData而不是再次收集和建築物。

的Java:

Function<Tuple2<String, DeviceData>, DeviceData> mapDeviceData = 
    new Function<Tuple2<String, DeviceData>, DeviceData>() { 
     public DeviceData call(Tuple2<String, DeviceData> tuple) { 
     return tuple._2(); 
     } 
    }; 

Dataset<DeviceData> ddDS = ds.map(mapDeviceData) //extracts DeviceData from each record 

斯卡拉:

val ddDS = ds.map(_._2) //ds.map(row => row._2) 
相關問題