Skip to content

Commit a6a31ea

Browse files
authored
feat: support write json object (#55)
1 parent 8c0b036 commit a6a31ea

File tree

7 files changed

+64
-4
lines changed

7 files changed

+64
-4
lines changed

ingester-grpc/pom.xml

-2
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@
2727
<artifactId>ingester-grpc</artifactId>
2828

2929
<properties>
30-
<gson.version>2.9.1</gson.version>
3130
<io.grpc.version>1.56.1</io.grpc.version>
3231
<limits.version>0.3.6</limits.version>
3332
</properties>
@@ -68,7 +67,6 @@
6867
<dependency>
6968
<groupId>com.google.code.gson</groupId>
7069
<artifactId>gson</artifactId>
71-
<version>${gson.version}</version>
7270
</dependency>
7371

7472
<!-- https://mvnrepository.com/artifact/com.netflix.concurrency-limits/concurrency-limits-core -->

ingester-protocol/pom.xml

+4
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,10 @@
4949
<groupId>com.google.guava</groupId>
5050
<artifactId>guava</artifactId>
5151
</dependency>
52+
<dependency>
53+
<groupId>com.google.code.gson</groupId>
54+
<artifactId>gson</artifactId>
55+
</dependency>
5256

5357
<!-- test -->
5458
<dependency>

ingester-protocol/src/main/java/io/greptime/models/RowHelper.java

+3-1
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,6 @@ public static void addValue(
7474
valueBuilder.setBinaryValue(UnsafeByteOperations.unsafeWrap((byte[]) value));
7575
break;
7676
case STRING:
77-
case JSON:
7877
valueBuilder.setStringValue((String) value);
7978
break;
8079
case DATE:
@@ -119,6 +118,9 @@ public static void addValue(
119118
case DECIMAL128:
120119
valueBuilder.setDecimal128Value(ValueUtil.getDecimal128Value(dataTypeExtension, value));
121120
break;
121+
case JSON:
122+
valueBuilder.setStringValue(ValueUtil.getJsonString(value));
123+
break;
122124
default:
123125
throw new IllegalArgumentException(String.format("Unsupported `data_type`: %s", dataType));
124126
}

ingester-protocol/src/main/java/io/greptime/models/ValueUtil.java

+11
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
package io.greptime.models;
1818

19+
import com.google.gson.Gson;
1920
import io.greptime.common.util.Ensures;
2021
import io.greptime.v1.Common;
2122
import java.math.BigDecimal;
@@ -104,4 +105,14 @@ static Common.Decimal128 getDecimal128Value(Common.ColumnDataTypeExtension dataT
104105

105106
return Common.Decimal128.newBuilder().setHi(high64Bits).setLo(low64Bits).build();
106107
}
108+
109+
// Gson's instances are Thread-safe we can reuse them freely across multiple threads.
110+
private static final Gson GSON = new Gson();
111+
112+
static String getJsonString(Object value) {
113+
if (value instanceof String) {
114+
return (String) value;
115+
}
116+
return GSON.toJson(value);
117+
}
107118
}

ingester-protocol/src/test/java/io/greptime/WriteClientTest.java

+6-1
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@
2828
import io.greptime.v1.Common;
2929
import io.greptime.v1.Database;
3030
import java.math.BigDecimal;
31+
import java.util.HashMap;
32+
import java.util.Map;
3133
import java.util.concurrent.ExecutionException;
3234
import java.util.concurrent.ForkJoinPool;
3335
import org.junit.After;
@@ -104,7 +106,10 @@ public void testWriteSuccess() throws ExecutionException, InterruptedException {
104106
// spotless:off
105107
Object[] row1 = new Object[]{"tag1", ts, 1, 2, 3, 4L, 5, 6, 7, 8L, 0.9F, 0.10D, true, new byte[0], 13L, 14L, 15L, 16L, 17L, 18L, 19L, 20L, 21L, 22L, 23, 24L, new IntervalMonthDayNano(1, 2, 3), BigDecimal.valueOf(123.456), "{\"a\": 1}"};
106108
Object[] row2 = new Object[]{"tag2", ts, 1, 2, 3, 4L, 5, 6, 7, 8L, 0.9F, 0.10D, true, new byte[0], 13L, 14L, 15L, 16L, 17L, 18L, 19L, 20L, 21L, 22L, 23, 24L, new IntervalMonthDayNano(4, 5, 6), BigDecimal.valueOf(123.456), "{\"b\": 2}"};
107-
Object[] row3 = new Object[]{"tag3", ts, 1, 2, 3, 4L, 5, 6, 7, 8L, 0.9F, 0.10D, true, new byte[0], 13L, 14L, 15L, 16L, 17L, 18L, 19L, 20L, 21L, 22L, 23, 24L, new IntervalMonthDayNano(7, 8, 9), BigDecimal.valueOf(123.456), "{\"c\": 3}"};
109+
Map<String, Object> json = new HashMap<>();
110+
json.put("name", "test");
111+
json.put("value", 123);
112+
Object[] row3 = new Object[]{"tag3", ts, 1, 2, 3, 4L, 5, 6, 7, 8L, 0.9F, 0.10D, true, new byte[0], 13L, 14L, 15L, 16L, 17L, 18L, 19L, 20L, 21L, 22L, 23, 24L, new IntervalMonthDayNano(7, 8, 9), BigDecimal.valueOf(123.456), json};
108113
// spotless:on
109114

110115
table.addRow(row1);

ingester-protocol/src/test/java/io/greptime/models/ValueUtilTest.java

+33
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@
2323
import java.time.Instant;
2424
import java.time.LocalDate;
2525
import java.util.Calendar;
26+
import java.util.HashMap;
27+
import java.util.Map;
2628
import java.util.Random;
2729
import java.util.TimeZone;
2830
import org.junit.Assert;
@@ -109,4 +111,35 @@ public void testGetDecimal128Value() {
109111
Assert.assertEquals(value, value2);
110112
}
111113
}
114+
115+
@Test
116+
public void testGetJsonStringShouldReturnJsonStringForObject() {
117+
String jsonString = ValueUtil.getJsonString(new TestObject("test", 123));
118+
Assert.assertEquals("{\"name\":\"test\",\"value\":123}", jsonString);
119+
120+
Map<String, Object> map = new HashMap<>();
121+
map.put("name", "test");
122+
map.put("value", 123);
123+
String jsonString2 = ValueUtil.getJsonString(map);
124+
Assert.assertEquals("{\"name\":\"test\",\"value\":123}", jsonString2);
125+
}
126+
127+
@Test
128+
public void testGetJsonStringShouldReturnStringForString() {
129+
String jsonString = ValueUtil.getJsonString("test");
130+
Assert.assertEquals("test", jsonString);
131+
132+
String jsonString2 = ValueUtil.getJsonString("{\"name\":\"test\",\"value\":123}");
133+
Assert.assertEquals("{\"name\":\"test\",\"value\":123}", jsonString2);
134+
}
135+
136+
private static class TestObject {
137+
String name;
138+
int value;
139+
140+
public TestObject(String name, int value) {
141+
this.name = name;
142+
this.value = value;
143+
}
144+
}
112145
}

pom.xml

+7
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@
7777
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
7878

7979
<!-- libs -->
80+
<gson.version>2.9.1</gson.version>
8081
<guava.version>32.0.1-jre</guava.version>
8182
<protobuf.version>3.21.12</protobuf.version>
8283
<slf4j.version>1.7.36</slf4j.version>
@@ -160,6 +161,12 @@
160161
<scope>provided</scope>
161162
</dependency>
162163

164+
<dependency>
165+
<groupId>com.google.code.gson</groupId>
166+
<artifactId>gson</artifactId>
167+
<version>${gson.version}</version>
168+
</dependency>
169+
163170
<!-- test -->
164171
<dependency>
165172
<groupId>junit</groupId>

0 commit comments

Comments
 (0)