Skip to content

Commit

Permalink
feat: support write json object (#55)
Browse files Browse the repository at this point in the history
  • Loading branch information
fengjiachun authored Nov 12, 2024
1 parent 8c0b036 commit a6a31ea
Show file tree
Hide file tree
Showing 7 changed files with 64 additions and 4 deletions.
2 changes: 0 additions & 2 deletions ingester-grpc/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
<artifactId>ingester-grpc</artifactId>

<properties>
<gson.version>2.9.1</gson.version>
<io.grpc.version>1.56.1</io.grpc.version>
<limits.version>0.3.6</limits.version>
</properties>
Expand Down Expand Up @@ -68,7 +67,6 @@
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>${gson.version}</version>
</dependency>

<!-- https://mvnrepository.com/artifact/com.netflix.concurrency-limits/concurrency-limits-core -->
Expand Down
4 changes: 4 additions & 0 deletions ingester-protocol/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,10 @@
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
</dependency>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
</dependency>

<!-- test -->
<dependency>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,6 @@ public static void addValue(
valueBuilder.setBinaryValue(UnsafeByteOperations.unsafeWrap((byte[]) value));
break;
case STRING:
case JSON:
valueBuilder.setStringValue((String) value);
break;
case DATE:
Expand Down Expand Up @@ -119,6 +118,9 @@ public static void addValue(
case DECIMAL128:
valueBuilder.setDecimal128Value(ValueUtil.getDecimal128Value(dataTypeExtension, value));
break;
case JSON:
valueBuilder.setStringValue(ValueUtil.getJsonString(value));
break;
default:
throw new IllegalArgumentException(String.format("Unsupported `data_type`: %s", dataType));
}
Expand Down
11 changes: 11 additions & 0 deletions ingester-protocol/src/main/java/io/greptime/models/ValueUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

package io.greptime.models;

import com.google.gson.Gson;
import io.greptime.common.util.Ensures;
import io.greptime.v1.Common;
import java.math.BigDecimal;
Expand Down Expand Up @@ -104,4 +105,14 @@ static Common.Decimal128 getDecimal128Value(Common.ColumnDataTypeExtension dataT

return Common.Decimal128.newBuilder().setHi(high64Bits).setLo(low64Bits).build();
}

// Gson's instances are Thread-safe we can reuse them freely across multiple threads.
private static final Gson GSON = new Gson();

static String getJsonString(Object value) {
if (value instanceof String) {
return (String) value;
}
return GSON.toJson(value);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@
import io.greptime.v1.Common;
import io.greptime.v1.Database;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ForkJoinPool;
import org.junit.After;
Expand Down Expand Up @@ -104,7 +106,10 @@ public void testWriteSuccess() throws ExecutionException, InterruptedException {
// spotless:off
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}"};
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}"};
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}"};
Map<String, Object> json = new HashMap<>();
json.put("name", "test");
json.put("value", 123);
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};
// spotless:on

table.addRow(row1);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
import java.time.Instant;
import java.time.LocalDate;
import java.util.Calendar;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;
import java.util.TimeZone;
import org.junit.Assert;
Expand Down Expand Up @@ -109,4 +111,35 @@ public void testGetDecimal128Value() {
Assert.assertEquals(value, value2);
}
}

@Test
public void testGetJsonStringShouldReturnJsonStringForObject() {
String jsonString = ValueUtil.getJsonString(new TestObject("test", 123));
Assert.assertEquals("{\"name\":\"test\",\"value\":123}", jsonString);

Map<String, Object> map = new HashMap<>();
map.put("name", "test");
map.put("value", 123);
String jsonString2 = ValueUtil.getJsonString(map);
Assert.assertEquals("{\"name\":\"test\",\"value\":123}", jsonString2);
}

@Test
public void testGetJsonStringShouldReturnStringForString() {
String jsonString = ValueUtil.getJsonString("test");
Assert.assertEquals("test", jsonString);

String jsonString2 = ValueUtil.getJsonString("{\"name\":\"test\",\"value\":123}");
Assert.assertEquals("{\"name\":\"test\",\"value\":123}", jsonString2);
}

private static class TestObject {
String name;
int value;

public TestObject(String name, int value) {
this.name = name;
this.value = value;
}
}
}
7 changes: 7 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>

<!-- libs -->
<gson.version>2.9.1</gson.version>
<guava.version>32.0.1-jre</guava.version>
<protobuf.version>3.21.12</protobuf.version>
<slf4j.version>1.7.36</slf4j.version>
Expand Down Expand Up @@ -160,6 +161,12 @@
<scope>provided</scope>
</dependency>

<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>${gson.version}</version>
</dependency>

<!-- test -->
<dependency>
<groupId>junit</groupId>
Expand Down

0 comments on commit a6a31ea

Please sign in to comment.