2012-11-09 35 views
0

我有一個Java應用程序,它允許用戶將數據存儲在數據庫中,但在存儲時我將這些數據存儲爲與cassandra相同的字節數組,現在當我找回字節數組我想要將這些數據轉換爲用戶保存,意味着如果用戶保存爲長我想顯示長值,或者如果用戶保存字符串我想顯示字符串值。現在,如果我將所有的字節數組字段轉換爲字符串,顯然很長的字節數組將顯示爲野生char.string字段會很好。如何從Java中的字節數組中獲取數據類型

請教我如何在java中解決這個問題。它類似於cassandra存儲data.cassandra的方式將所有數據存儲爲字節數組。

基本上我想知道字節數組的數據類型。

+0

http://docs.oracle.com/javaee/6/api/javax/mail/util/ByteArrayDataSource.html – Raghunandan

+0

user1249655:問題對我沒有任何意義。我認爲你應該真的考慮你正在努力解決的問題。 Arent你使一件非常簡單的事情變得複雜嗎? –

+0

@ user1249655:向存儲該類型的表中添加另一列。 :) –

回答

1

你的問題是不是爲你想要什麼非常清楚,但...

你能想出這樣做的像數組的第一個字節一些自定義的協議爲何種類型和其餘的字節是實際的數據。然後您需要編寫代碼將字節[1]通過字節[length-1]轉換爲該給定類型。這對我來說好像很多工作。

我可能會嘗試使用對象序列化。它基本上做你在這裏問你沒有任何自定義代碼。

public static void main(String[] args) throws Exception { 
    String strValue = "hello"; 
    int myInt = 3; 
    long myLong = 45677; 
    short myShort = 1; 
    double myFloat = 4.5; 

    serializeThenDeserialize(strValue); 
    serializeThenDeserialize(myInt); 
    serializeThenDeserialize(myLong); 
    serializeThenDeserialize(myShort); 
    serializeThenDeserialize(myFloat); 
} 

private static void serializeThenDeserialize(Object value) throws Exception { 
    System.out.println("Input Type is " + value.getClass() + " with value '" + value + "'"); 
    ByteArrayOutputStream byteArrayStream = new ByteArrayOutputStream(); 
    ObjectOutputStream out = new ObjectOutputStream(byteArrayStream); 
    out.writeObject(value); 
    out.close(); 

    byte[] objectAsBytes = byteArrayStream.toByteArray(); 
    // Persist here.. 


    // Now lets deserialize the byte array 
    ObjectInputStream in = new ObjectInputStream(new ByteArrayInputStream(objectAsBytes)); 
    Object deserializedValue = in.readObject(); 
    in.close(); 

    System.out.println("Deserialized Type is " + deserializedValue.getClass() + " with Value '" + deserializedValue + "'"); 
    System.out.println(); 
} 

當運行它時,它就像我們想要的。數據被返回並保持類型。

Input Type is class java.lang.String with value 'hello' 
Deserialized Type is class java.lang.String with Value 'hello' 

Input Type is class java.lang.Integer with value '3' 
Deserialized Type is class java.lang.Integer with Value '3' 

Input Type is class java.lang.Long with value '45677' 
Deserialized Type is class java.lang.Long with Value '45677' 

Input Type is class java.lang.Short with value '1' 
Deserialized Type is class java.lang.Short with Value '1' 

Input Type is class java.lang.Double with value '4.5' 
Deserialized Type is class java.lang.Double with Value '4.5' 

好處在於它適用於所有Java對象。不好的一面是,Java對象的序列化會隨着你存儲的對象的進化而變得有些困難(即你刪除方法,字段,編譯w /不同的JDK等)。如果你堅持原語,你應該沒有這個問題。如果你序列化自己的對象,你應該閱讀更多有關兼容和不兼容的更改here

+0

是的蘭斯,看起來像這可能會解決這個問題,但我最初的問題是我存儲數據在卡薩德拉,同時使用節儉api從卡桑德拉檢索數據我得到字節數組,現在我如何真正找回原始數據類型和數據。 – user1249655

+0

用你的字節數組你可以調用他的代碼,對吧? (或者我錯過了什麼)你可以調用的代碼是://現在讓反序列化字節數組 ObjectInputStream in = new ObjectInputStream(new ByteArrayInputStream(objectAsBytes)); Object deserializedValue = in.readObject(); in.close(); – Friso

0

我建議你以某種格式序列化數據,其存儲輸入信息,比如BSON:http://bsonspec.org/或微笑:http://wiki.fasterxml.com/SmileFormat

在這種情況下,反序列化將恢復類信息,並反序列化後,你會得到對象正確的類型。

這些格式非常緊湊:與java standart序列化相比,type info只需要幾個額外的字節,這需要幾個冗餘字節來序列化最簡單的對象。

相關問題