forked from oceanbase/obkv-table-client-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathObTableDatetimeTest.java
More file actions
165 lines (149 loc) · 6.55 KB
/
ObTableDatetimeTest.java
File metadata and controls
165 lines (149 loc) · 6.55 KB
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
/*-
* #%L
* com.oceanbase:obkv-table-client
* %%
* Copyright (C) 2021 - 2025 OceanBase
* %%
* OBKV Table Client Framework is licensed under Mulan PSL v2.
* You can use this software according to the terms and conditions of the Mulan PSL v2.
* You may obtain a copy of Mulan PSL v2 at:
* http://license.coscl.org.cn/MulanPSL2
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
* EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
* MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
* See the Mulan PSL v2 for more details.
* #L%
*/
package com.alipay.oceanbase.rpc;
import com.alipay.oceanbase.rpc.exception.ObTableException;
import com.alipay.oceanbase.rpc.get.Get;
import com.alipay.oceanbase.rpc.mutation.BatchOperation;
import com.alipay.oceanbase.rpc.mutation.InsertOrUpdate;
import com.alipay.oceanbase.rpc.mutation.Row;
import com.alipay.oceanbase.rpc.mutation.result.BatchOperationResult;
import com.alipay.oceanbase.rpc.stream.QueryResultSet;
import com.alipay.oceanbase.rpc.util.ObTableClientTestUtil;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;
import static com.alipay.oceanbase.rpc.mutation.MutationFactory.colVal;
import static com.alipay.oceanbase.rpc.mutation.MutationFactory.row;
import static org.junit.Assert.assertThrows;
import static org.junit.Assert.assertTrue;
/*
CREATE TABLE IF NOT EXISTS `test_datetime_range` (
`c1` bigint(20) NOT NULL,
`c2` datetime(6) NOT NULL,
`c3` varchar(20) DEFAULT NULL,
KEY `idx_c2` (`c2`) LOCAL,
PRIMARY KEY (`c1`, `c2`)
) partition by range columns(c2) subpartition by key(c1) subpartition template (
subpartition `p0`,
subpartition `p1`,
subpartition `p2`,
subpartition `p3`)
(partition `p0` values less than ('2023-12-01 00:00:00'),
partition `p1` values less than ('2024-12-10 00:00:00'),
partition `p2` values less than ('2025-12-20 00:00:00'),
partition `p3` values less than ('2026-12-30 00:00:00'),
partition `p4` values less than ('2027-01-01 00:00:00'),
partition `p5` values less than MAXVALUE);
*/
public class ObTableDatetimeTest {
ObTableClient client;
public static String tableName = "test_datetime_range";
@Before
public void setup() throws Exception {
final ObTableClient obTableClient = ObTableClientTestUtil.newTestClient();
obTableClient.init();
this.client = obTableClient;
}
@Test
public void testSingle() throws Exception {
String dateString = "2023-04-05 14:30:00";
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date date = sdf.parse(dateString);
try {
Row rk = row(colVal("c1", 1L), colVal("c2", date), colVal("c3", 1L));
client.insertOrUpdate(tableName).setRowKey(rk).addMutateColVal(colVal("c4", "c4_val"))
.execute();
Map<String, Object> res = client.get(tableName).setRowKey(rk).select("c4").execute();
Assert.assertEquals("c4_val", res.get("c4"));
client.delete(tableName).setRowKey(rk).execute();
} catch (Exception e) {
System.out.println(e.getMessage());
throw e;
}
}
@Test
public void testBatch() throws Exception {
String dateString1 = "2023-04-05 14:30:00";
String dateString2 = "2024-04-05 14:30:00";
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date date1 = sdf.parse(dateString1);
Date date2 = sdf.parse(dateString2);
try {
Row rk1 = row(colVal("c1", 1L), colVal("c2", date1), colVal("c3", 1L));
Row rk2 = row(colVal("c1", 1L), colVal("c2", date2), colVal("c3", 1L));
BatchOperation batch = client.batchOperation(tableName);
InsertOrUpdate insUp1 = client.insertOrUpdate(tableName).setRowKey(rk1)
.addMutateColVal(colVal("c4", "c4_val"));
InsertOrUpdate insUp2 = client.insertOrUpdate(tableName).setRowKey(rk2)
.addMutateColVal(colVal("c4", "c4_val"));
batch.addOperation(insUp1, insUp2);
BatchOperationResult res = batch.execute();
Assert.assertNotNull(res);
Assert.assertNotEquals(0, res.get(0).getAffectedRows());
Assert.assertNotEquals(0, res.get(1).getAffectedRows());
client.delete(tableName).setRowKey(rk1).execute();
client.delete(tableName).setRowKey(rk2).execute();
} catch (Exception e) {
System.out.println(e.getMessage());
throw e;
}
}
@Test
public void testPkQuery() throws Exception {
String dateString = "2023-04-05 14:30:00";
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date date = sdf.parse(dateString);
try {
Row rk = row(colVal("c1", 1L), colVal("c2", date), colVal("c3", 1L));
client.insertOrUpdate(tableName).setRowKey(rk).addMutateColVal(colVal("c4", "c4_val"))
.execute();
QueryResultSet res = client.query(tableName).select("c4")
.setScanRangeColumns("c1", "c2", "c3")
.addScanRange(new Object[] { 1L, date, 1L }, new Object[] { 1L, date, 1L })
.execute();
Assert.assertTrue(res.next());
Assert.assertEquals("c4_val", res.getRow().get("c4"));
client.delete(tableName).setRowKey(rk).execute();
} catch (Exception e) {
System.out.println(e.getMessage());
throw e;
}
}
@Test
public void testIndexQuery() throws Exception {
String dateString = "2023-04-05 14:30:00";
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date date = sdf.parse(dateString);
try {
Row rk = row(colVal("c1", 1L), colVal("c2", date), colVal("c3", 1L));
client.insertOrUpdate(tableName).setRowKey(rk).addMutateColVal(colVal("c4", "c4_val"))
.execute();
QueryResultSet res = client.query(tableName).indexName("idx_c2").select("c4")
.setScanRangeColumns("c1", "c2")
.addScanRange(new Object[] { 1L, date }, new Object[] { 1L, date }).execute();
Assert.assertTrue(res.next());
Assert.assertEquals("c4_val", res.getRow().get("c4"));
client.delete(tableName).setRowKey(rk).execute();
} catch (Exception e) {
System.out.println(e.getMessage());
throw e;
}
}
}