2016-02-12 27 views
0

我有一個簡單的avro模式,我使用avro-maven-plugin生成了一個java類。作爲json的Avro java序列化不能正常工作

的Avro的模式如下:

{ 
    "type": "record", 
    "name": "addressGeo", 
    "namespace": "com.mycompany", 
    "doc": "Best record address and list of geos", 
    "fields": [ 

    { 
     "name": "version", 
     "type": "int", 
     "default": 1, 
     "doc": "version the class" 
    }, 
    { 
     "name": "eventType", 
     "type": "string", 
     "default": "addressGeo", 
     "doc": "event type" 
    }, 
    { 
     "name": "parcelId", 
     "type": "long", 
     "doc": "ParcelID of the parcel. Join parcelid and sequence with ParcelInfo" 
    }, 
    { 
     "name": "geoCodes", 
     "type": {"type": "array", "items": "com.mycompany.geoCode"}, 
     "doc": "Multiple Geocodes, with restrictions information" 
    }, 
    { 
     "name": "brfAddress", 
     "type": ["null", "com.mycompany.address"], 
     "doc": "Address cleansed version of BRF" 
    } 
    ] 
} 

如果我使用構建器構建一個簡單的對象,並使用JSON序列化,我得到下面的輸出:

{ 
    "version": 1, 
    "eventType": { 
    "bytes": [ 
     97, 
     100, 
     100, 
     114, 
     101, 
     115, 
     115, 
     71, 
     101, 
     111 
    ], 
    "length": 10, 
    "string": null 
    }, 
    "parcelId": 1, 
    "geoCodes": [ 
    { 
     "version": 1, 
     "latitude": 1, 
     "longitude": 1, 
     "geoQualityCode": "g", 
     "geoSourceTypeID": 1, 
     "restrictions": "NONE" 
    } 
    ], 
    "brfAddress": { 
    "version": 1, 
    "houseNumber": "1", 
    "houseNumberFraction": null, 
    "streetDirectionPrefix": null, 
    "streetName": "main", 
    "streetSuffix": "street", 
    "streetDirectionSuffix": null, 
    "fullStreetAddress": "1 main street, seattle, wa, 98101", 
    "unitPrefix": null, 
    "unitNumber": null, 
    "city": "seattle", 
    "state": "wa", 
    "zipCode": "98101", 
    "zipPlusFour": null, 
    "addressDPV": "Y", 
    "addressQualityCode": "good", 
    "buildingNumber": "1", 
    "carrierRoute": "t", 
    "censusTract": "c", 
    "censusTractAndBlock": "b", 
    "dataCleanerTypeID": 1, 
    "restrictions": "NONE" 
    } 
} 

注輸出的eventType字段。它以字節數組的形式出現,而字段的類型是CharSequence。

任何想法爲什麼序列化是這樣做的?它適用於其他字符串類型。

我正在使用google-gson將對象序列化爲json。

回答

1

您可能正在使用舊版本的avro,它使用CharSequence。理想情況下,字符串類型應該是java String類型。我會建議更新avro版本或看看這一個 - Apache Avro: map uses CharSequence as key

+0

我想我使用最新的一個,但我會給雙重檢查。謝謝! – feroze

+0

不,這不是版本問題。我正在使用avro v 1.8.0,這是最新的按照此鏈接:http://search.maven.org/#search%7Cgav%7C1%7Cg%3A%22org.apache.avro%22%20AND%20a% 3A%22avro%22 – feroze

+0

此外,這個問題只發生在默認的字符串字段中。所有其他字段都以字符串的形式正確序列化,即使它們在java類中聲明爲charsequence。 – feroze