|
| 1 | +package com.alipay.oceanbase.rpc; |
| 2 | + |
| 3 | +import com.alipay.oceanbase.rpc.exception.ObTableException; |
| 4 | +import com.alipay.oceanbase.rpc.get.Get; |
| 5 | +import com.alipay.oceanbase.rpc.mutation.BatchOperation; |
| 6 | +import com.alipay.oceanbase.rpc.mutation.InsertOrUpdate; |
| 7 | +import com.alipay.oceanbase.rpc.mutation.Row; |
| 8 | +import com.alipay.oceanbase.rpc.mutation.result.BatchOperationResult; |
| 9 | +import com.alipay.oceanbase.rpc.stream.QueryResultSet; |
| 10 | +import com.alipay.oceanbase.rpc.util.ObTableClientTestUtil; |
| 11 | +import org.junit.Assert; |
| 12 | +import org.junit.Before; |
| 13 | +import org.junit.Test; |
| 14 | + |
| 15 | +import java.text.SimpleDateFormat; |
| 16 | +import java.util.Date; |
| 17 | +import java.util.Map; |
| 18 | + |
| 19 | +import static com.alipay.oceanbase.rpc.mutation.MutationFactory.colVal; |
| 20 | +import static com.alipay.oceanbase.rpc.mutation.MutationFactory.row; |
| 21 | +import static org.junit.Assert.assertThrows; |
| 22 | +import static org.junit.Assert.assertTrue; |
| 23 | + |
| 24 | +/* |
| 25 | +CREATE TABLE IF NOT EXISTS `test_datetime_range` ( |
| 26 | + `c1` bigint(20) NOT NULL, |
| 27 | + `c2` datetime(6) NOT NULL, |
| 28 | + `c3` varchar(20) DEFAULT NULL, |
| 29 | + KEY `idx_c2` (`c2`) LOCAL, |
| 30 | + PRIMARY KEY (`c1`, `c2`) |
| 31 | + ) partition by range columns(c2) subpartition by key(c1) subpartition template ( |
| 32 | + subpartition `p0`, |
| 33 | + subpartition `p1`, |
| 34 | + subpartition `p2`, |
| 35 | + subpartition `p3`) |
| 36 | + (partition `p0` values less than ('2023-12-01 00:00:00'), |
| 37 | + partition `p1` values less than ('2024-12-10 00:00:00'), |
| 38 | + partition `p2` values less than ('2025-12-20 00:00:00'), |
| 39 | + partition `p3` values less than ('2026-12-30 00:00:00'), |
| 40 | + partition `p4` values less than ('2027-01-01 00:00:00'), |
| 41 | + partition `p5` values less than MAXVALUE); |
| 42 | + */ |
| 43 | +public class ObTableDatetimeTest { |
| 44 | + ObTableClient client; |
| 45 | + public static String tableName = "test_datetime_range"; |
| 46 | + |
| 47 | + @Before |
| 48 | + public void setup() throws Exception { |
| 49 | + final ObTableClient obTableClient = ObTableClientTestUtil.newTestClient(); |
| 50 | + obTableClient.init(); |
| 51 | + this.client = obTableClient; |
| 52 | + } |
| 53 | + |
| 54 | + @Test |
| 55 | + public void testSingle() throws Exception { |
| 56 | + String dateString = "2023-04-05 14:30:00"; |
| 57 | + SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); |
| 58 | + Date date = sdf.parse(dateString); |
| 59 | + try { |
| 60 | + Row rk = row(colVal("c1", 1L), colVal("c2", date), colVal("c3", 1L)); |
| 61 | + client.insertOrUpdate(tableName).setRowKey(rk) |
| 62 | + .addMutateColVal(colVal("c4", "c4_val")) |
| 63 | + .execute(); |
| 64 | + Map<String, Object> res = client.get(tableName) |
| 65 | + .setRowKey(rk) |
| 66 | + .select("c4") |
| 67 | + .execute(); |
| 68 | + Assert.assertEquals("c4_val", res.get("c4")); |
| 69 | + |
| 70 | + client.delete(tableName).setRowKey(rk).execute(); |
| 71 | + } catch (Exception e) { |
| 72 | + System.out.println(e.getMessage()); |
| 73 | + throw e; |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + @Test |
| 78 | + public void testBatch() throws Exception { |
| 79 | + String dateString1 = "2023-04-05 14:30:00"; |
| 80 | + String dateString2 = "2024-04-05 14:30:00"; |
| 81 | + SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); |
| 82 | + Date date1 = sdf.parse(dateString1); |
| 83 | + Date date2 = sdf.parse(dateString2); |
| 84 | + try { |
| 85 | + Row rk1 = row(colVal("c1", 1L), colVal("c2", date1), colVal("c3", 1L)); |
| 86 | + Row rk2 = row(colVal("c1", 1L), colVal("c2", date2), colVal("c3", 1L)); |
| 87 | + BatchOperation batch = client.batchOperation(tableName); |
| 88 | + InsertOrUpdate insUp1 = client.insertOrUpdate(tableName).setRowKey(rk1) |
| 89 | + .addMutateColVal(colVal("c4", "c4_val")); |
| 90 | + InsertOrUpdate insUp2 = client.insertOrUpdate(tableName).setRowKey(rk2) |
| 91 | + .addMutateColVal(colVal("c4", "c4_val")); |
| 92 | + batch.addOperation(insUp1, insUp2); |
| 93 | + BatchOperationResult res = batch.execute(); |
| 94 | + Assert.assertNotNull(res); |
| 95 | + Assert.assertNotEquals(0, res.get(0).getAffectedRows()); |
| 96 | + Assert.assertNotEquals(0, res.get(1).getAffectedRows()); |
| 97 | + |
| 98 | + client.delete(tableName).setRowKey(rk1).execute(); |
| 99 | + client.delete(tableName).setRowKey(rk2).execute(); |
| 100 | + } catch (Exception e) { |
| 101 | + System.out.println(e.getMessage()); |
| 102 | + throw e; |
| 103 | + } |
| 104 | + } |
| 105 | + |
| 106 | + @Test |
| 107 | + public void testPkQuery() throws Exception { |
| 108 | + String dateString = "2023-04-05 14:30:00"; |
| 109 | + SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); |
| 110 | + Date date = sdf.parse(dateString); |
| 111 | + try { |
| 112 | + Row rk = row(colVal("c1", 1L), colVal("c2", date), colVal("c3", 1L)); |
| 113 | + client.insertOrUpdate(tableName).setRowKey(rk) |
| 114 | + .addMutateColVal(colVal("c4", "c4_val")) |
| 115 | + .execute(); |
| 116 | + |
| 117 | + QueryResultSet res = client.query(tableName) |
| 118 | + .select("c4") |
| 119 | + .setScanRangeColumns("c1", "c2", "c3") |
| 120 | + .addScanRange(new Object[]{1L, date, 1L}, new Object[]{1L, date, 1L}) |
| 121 | + .execute(); |
| 122 | + Assert.assertTrue(res.next()); |
| 123 | + Assert.assertEquals("c4_val", res.getRow().get("c4")); |
| 124 | + |
| 125 | + client.delete(tableName).setRowKey(rk).execute(); |
| 126 | + } catch (Exception e) { |
| 127 | + System.out.println(e.getMessage()); |
| 128 | + throw e; |
| 129 | + } |
| 130 | + } |
| 131 | + |
| 132 | + @Test |
| 133 | + public void testIndexQuery() throws Exception { |
| 134 | + String dateString = "2023-04-05 14:30:00"; |
| 135 | + SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); |
| 136 | + Date date = sdf.parse(dateString); |
| 137 | + try { |
| 138 | + Row rk = row(colVal("c1", 1L), colVal("c2", date), colVal("c3", 1L)); |
| 139 | + client.insertOrUpdate(tableName).setRowKey(rk) |
| 140 | + .addMutateColVal(colVal("c4", "c4_val")) |
| 141 | + .execute(); |
| 142 | + |
| 143 | + QueryResultSet res = client.query(tableName) |
| 144 | + .indexName("idx_c2") |
| 145 | + .select("c4") |
| 146 | + .setScanRangeColumns("c1", "c2") |
| 147 | + .addScanRange(new Object[]{1L, date}, new Object[]{1L, date}) |
| 148 | + .execute(); |
| 149 | + Assert.assertTrue(res.next()); |
| 150 | + Assert.assertEquals("c4_val", res.getRow().get("c4")); |
| 151 | + |
| 152 | + client.delete(tableName).setRowKey(rk).execute(); |
| 153 | + } catch (Exception e) { |
| 154 | + System.out.println(e.getMessage()); |
| 155 | + throw e; |
| 156 | + } |
| 157 | + } |
| 158 | +} |
0 commit comments