-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtable.rs
124 lines (117 loc) · 4.15 KB
/
table.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
use anyhow::Result;
// make sure you have read the database.rs example first, and created a database named 'book2'
// this example shows how to use the table api.
use mochow_sdk_rust::mochow::{
api::{self, FieldType, IndexType, MetricType, PartitionType},
client,
};
const ACCOUNT: &str = "your_account";
const PASSWORD: &str = "your_password";
const ENDPOINT: &str = "http://127.0.0.1:5287";
#[tokio::main]
async fn main() -> Result<()> {
let client = client::MochowClient::new(ACCOUNT, PASSWORD, ENDPOINT).unwrap();
// table 'book_segments2' is created in the database 'book2'
let _drop_table_resp = client.drop_table("book2", "book_segments2").await.unwrap();
// create table 'book_segments'
// field: id, bookName, author, page, vector,
// index: book_name_idx, vector_idx
let fields = vec![
api::FieldSchemaBuilder::default()
.field_name("id")
.field_type(FieldType::STRING)
.primary_key(true)
.partition_key(true)
.not_null(true)
.build()?,
api::FieldSchemaBuilder::default()
.field_name("bookName")
.field_type(FieldType::STRING)
.not_null(true)
.build()?,
api::FieldSchemaBuilder::default()
.field_name("author")
.field_type(FieldType::STRING)
.build()?,
api::FieldSchemaBuilder::default()
.field_name("page")
.field_type(FieldType::UINT32)
.build()?,
api::FieldSchemaBuilder::default()
.field_name("vector")
.field_type(FieldType::FLOAT_VECTOR)
.not_null(true)
.dimension(3)
.build()?,
];
let indexes = vec![
api::IndexSchemaBuilder::default()
.index_name("book_name_idx")
.field("bookName")
.index_type(IndexType::HNSW)
.build()?,
api::IndexSchemaBuilder::default()
.index_name("vector_idx")
.field("vector")
.index_type(IndexType::HNSW)
.metric_type(MetricType::L2)
.params(api::VectorIndexParams::HNSW(api::HNSWIndexParam {
m: 32,
ef_construction: 200,
}))
.build()?,
];
let create_tabke_args = api::CreateTableArgsBuilder::default()
.database("book2")
.table("book_segments")
.description("basic test")
.replication(3 as u32)
.partition(api::Partition {
partition_type: PartitionType::HASH,
partition_num: 3,
})
.schema(api::TableSchema {
fields: fields,
indexes: indexes,
})
.build()?;
let create_table_resp = client.create_table(&create_tabke_args).await?;
println!("create table resp: {:?}", create_table_resp);
// get table info
let description_of_table = client.desc_table("book2", "book_segments").await?;
println!(
"description of table fields: {:?}, filed count: {}",
description_of_table.table.schema.fields,
description_of_table.table.schema.fields.len()
);
println!(
"description of table index: {:?}",
description_of_table.table.schema.indexes
);
// you can add a field to table book_segments
let add_fields = vec![api::FieldSchemaBuilder::default()
.field_name("bookAlias")
.field_type(FieldType::STRING)
.build()?];
let add_field_args = api::AddFieldArgsBuilder::default()
.database("book2")
.table("book_segments")
.schema(api::TableSchema {
fields: add_fields,
indexes: vec![],
})
.build()?;
let _add_field_resp = client.add_field(&add_field_args).await?;
// get table info after add a field
let description_of_table = client.desc_table("book2", "book_segments").await?;
println!(
"description of table fields: {:?}, filed count: {}",
description_of_table.table.schema.fields,
description_of_table.table.schema.fields.len()
);
println!(
"description of table index: {:?}",
description_of_table.table.schema.indexes
);
Ok(())
}