-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Mapping Row=> Bean and deserialization
- Loading branch information
Showing
11 changed files
with
216 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
45 changes: 45 additions & 0 deletions
45
astra-db-java/src/test/java/com/datastax/astra/test/unit/DataApiVectorSerializationTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package com.datastax.astra.test.unit; | ||
|
||
import com.datastax.astra.client.core.options.DataAPIClientOptions; | ||
import com.datastax.astra.client.core.vector.DataAPIVector; | ||
import com.datastax.astra.internal.serdes.tables.RowSerializer; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import java.time.Instant; | ||
|
||
import static org.assertj.core.api.AssertionsForClassTypes.assertThat; | ||
|
||
public class DataApiVectorSerializationTest { | ||
|
||
@Test | ||
public void binarySerialization() { | ||
byte[] bytes = "hello".getBytes(); | ||
RowSerializer serializer = new RowSerializer(); | ||
String json = serializer.marshall(bytes); | ||
// {"$binary":"aGVsbG8="} | ||
|
||
System.out.println(json); | ||
byte[] bytes2 = serializer.unMarshallBean(json, byte[].class); | ||
assertThat(new String(bytes2)).isEqualTo("hello"); | ||
} | ||
|
||
@Test | ||
public void testVectorSerialization() { | ||
DataAPIClientOptions.getSerdesOptions().encodeDataApiVectorsAsBase64(true);; | ||
RowSerializer serializer = new RowSerializer(); | ||
DataAPIVector vector = new DataAPIVector(new float[]{0.4f, -0.6f, 0.2f}); | ||
System.out.println(serializer.marshall(vector)); | ||
|
||
String json1 = "{\"$binary\":\"PszMzb8ZmZo+TMzN\"}"; | ||
DataAPIVector vector2 = serializer.unMarshallBean(json1, DataAPIVector.class); | ||
System.out.println(vector2.getEmbeddings()); | ||
|
||
String json2 = "[0.4, -0.6, 0.2]"; | ||
} | ||
|
||
@Test | ||
public void serializationInstant() { | ||
String sample = "2024-12-04T15:04:07.203Z"; | ||
Instant.parse(sample); | ||
} | ||
} |
50 changes: 50 additions & 0 deletions
50
examples/src/main/java/com/datastax/astra/client/tables/FindOne.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package com.datastax.astra.client.tables; | ||
|
||
import com.datastax.astra.client.DataAPIClients; | ||
import com.datastax.astra.client.core.query.Filter; | ||
import com.datastax.astra.client.core.query.Sort; | ||
import com.datastax.astra.client.core.vector.DataAPIVector; | ||
import com.datastax.astra.client.databases.Database; | ||
import com.datastax.astra.client.tables.commands.options.TableFindOneOptions; | ||
import com.datastax.astra.client.tables.definition.rows.Row; | ||
import com.datastax.astra.internal.serdes.tables.RowSerializer; | ||
|
||
import java.util.Optional; | ||
|
||
import static com.datastax.astra.client.core.query.Filters.and; | ||
import static com.datastax.astra.client.core.query.Filters.eq; | ||
import static com.datastax.astra.client.core.query.Filters.gt; | ||
|
||
public class FindOne { | ||
public static void main(String[] args) { | ||
Database db = DataAPIClients.localDbWithDefaultKeyspace(); | ||
// Database astraDb = new DataAPIClient(token).getDatabase(endpoint); | ||
|
||
Table<Row> table = db.getTable("games"); | ||
|
||
Filter filter = and( | ||
eq("match_id", "mtch_0"), | ||
gt("round", 1), | ||
eq("winner", "Victor")); | ||
|
||
TableFindOneOptions options = new TableFindOneOptions() | ||
//.projection(include("match_id", "winner", "field3")) | ||
.sort(Sort.vector("m_vector", new DataAPIVector(new float[] {0.4f, -0.6f, 0.2f}))) | ||
.includeSimilarity(true); | ||
|
||
Optional<Row> row = table.findOne(filter, options); | ||
row.ifPresent(r -> { | ||
System.out.println("Row: " + r); | ||
DataAPIVector v = r.getVector("m_vector"); | ||
System.out.println(r.getInstant("when")); | ||
}); | ||
|
||
Table<Game> tableGame = db.getTable("games", Game.class); | ||
Optional<Game> row2 = tableGame.findOne(filter, options); | ||
row2.ifPresent(game -> { | ||
System.out.println("game: " + game.getVector().dimension()); | ||
System.out.println(game.getFighters()); | ||
System.out.println(game.getMatchId()); | ||
}); | ||
} | ||
} |
Oops, something went wrong.