私はUbuntu 14.04でSpark 2.1を実行しています sparkで検索変数をブロードキャストしようとしています。変数は、型scala.collection.immutable.Mapである[文字列、MyObjectに]Sparkコンテキストのブロードキャスト変数がjava.io.NotSerializableExceptionをスローしても、シリアル化可能です
MyObjectには、フィールド
- String型
- のString型の '名前'
- 'アドレス' 'を以下ました型com.google.common.collectのrangeSet」{TreeRangeSet}
Exception in thread "main" java.io.NotSerializableException: com.google.common.collect.TreeRangeSet
Serialization stack:
- object not serializable (class: com.google.common.collect.TreeRangeSet, value: {[/101.32.168.0‥/101.32.181.255][/4626:7800:4048:0:0:0:0:0‥/4626:7800:4048:ffff:ffff:ffff:ffff:ffff]})
- field (class: com.test.MyObject, name: rangeSet, type: class com.google.common.collect.TreeRangeSet)
- object (class com.test.MyObject, MyObject(Jack,Test,{[/101.32.168.0‥/101.32.181.255][/192.16.10.224‥/192.16.10.255][/4626:7800:4048:0:0:0:0:0‥/4626:7800:4048:ffff:ffff:ffff:ffff:ffff]}))
- writeObject data (class: scala.collection.immutable.HashMap$SerializationProxy)
- object (class scala.collection.immutable.HashMap$SerializationProxy, [email protected])
- writeReplace data (class: scala.collection.immutable.HashMap$SerializationProxy)
MyObject.scala
import com.google.common.collect.{TreeRangeSet}
@SerialVersionUID(123L)
case class MyObject(name:String, address:String,rangeSet:TreeRangeSet[CustomInetAddress]) {
}
CustomInetAddress.java
public class CustomInetAddress implements Comparable<CustomInetAddress>, Serializable {
private InetAddress inetAddress;
public CustomInetAddress(String ip) throws UnknownHostException {
this.inetAddress = InetAddress.getByName(ip);
}
public CustomInetAddress(InetAddress address) throws UnknownHostException {
this.inetAddress = address;
}
@Override
public int compareTo(final CustomInetAddress address){
byte[] ba1 = this.inetAddress.getAddress();
byte[] ba2 = address.inetAddress.getAddress();
if(ba1.length < ba2.length) return -1;
if(ba1.length > ba2.length) return 1;
for(int i = 0; i < ba1.length; i++) {
int b1 = unsignedByteToInt(ba1[i]);
int b2 = unsignedByteToInt(ba2[i]);
if(b1 == b2)
continue;
if(b1 < b2)
return -1;
else
return 1;
}
return 0;
}
@Override
public String toString(){
return this.inetAddress.toString();
}
private int unsignedByteToInt(byte b) {
return (int) b & 0xFF;
}
}
TreeRangeSetは[CustomInetAddress】オブジェクトの実際の型です。 CustomInetAddressには、InetAddress型のフィールドが1つあります。それらのすべてはシリアライズ可能です。なぜこの例外がスローされているのか分かりません。
私はバージョン22.0を使用しています https://google.github.io/guava/releases/22.0/api/docs/com/google/common/collect/TreeRangeSet.html – Satheesh
それはおそらく複数のグアバジャーがあるからですJVMはクラスパス上で間違ったものを選びます。あなたは依存関係のツリーを見てみることができますか? – zsxwing
これは有効な点ですが、私のsbtはすべての古いバージョンを削除し、22.0を追加します。 私はこの問題がTreeRangeSetクラスであることを発見しました。それはシリアライズ可能な実装ですが、私はそれをピクルできません。 (REPLでテスト済み) – Satheesh