This is an automated email from the ASF dual-hosted git repository.
haonan pushed a commit to branch rel/0.13
in repository https://gitbox.apache.org/repos/asf/iotdb.git
The following commit(s) were added to refs/heads/rel/0.13 by this push:
new 2676a8e385 [To rel/0.13][IOTDB-3803] failed to insert data of TEXT by
session (#6646)
2676a8e385 is described below
commit 2676a8e385dc22fc64ec7639187e9970b95b997a
Author: Chen YZ <[email protected]>
AuthorDate: Wed Jul 13 09:29:19 2022 +0800
[To rel/0.13][IOTDB-3803] failed to insert data of TEXT by session (#6646)
---
docs/UserGuide/API/Programming-Java-Native-API.md | 11 +++
.../UserGuide/API/Programming-Java-Native-API.md | 11 +++
.../main/java/org/apache/iotdb/SessionExample.java | 42 +++++++++
.../apache/iotdb/session/IoTDBSessionSimpleIT.java | 99 ++++++++++++++++++++++
.../apache/iotdb/session/util/SessionUtils.java | 13 ++-
.../apache/iotdb/tsfile/write/record/Tablet.java | 6 +-
6 files changed, 179 insertions(+), 3 deletions(-)
diff --git a/docs/UserGuide/API/Programming-Java-Native-API.md
b/docs/UserGuide/API/Programming-Java-Native-API.md
index 2f9d7e97f3..c56de900ab 100644
--- a/docs/UserGuide/API/Programming-Java-Native-API.md
+++ b/docs/UserGuide/API/Programming-Java-Native-API.md
@@ -374,6 +374,17 @@ void insertTablets(Map<String, Tablet> tablet)
* Insert a Record, which contains multiple measurement value of a device at a
timestamp. This method is equivalent to providing a common interface for
multiple data types of values. Later, the value can be cast to the original
type through TSDataType.
+ The correspondence between the Object type and the TSDataType type is shown
in the following table.
+
+ | TSDataType | Object |
+ | ---------- | -------------- |
+ | BOOLEAN | Boolean |
+ | INT32 | Integer |
+ | INT64 | Long |
+ | FLOAT | Float |
+ | DOUBLE | Double |
+ | TEXT | String, Binary |
+
```java
void insertRecord(String deviceId, long time, List<String> measurements,
List<TSDataType> types, List<Object> values)
diff --git a/docs/zh/UserGuide/API/Programming-Java-Native-API.md
b/docs/zh/UserGuide/API/Programming-Java-Native-API.md
index 3747639ab2..4ca110ac68 100644
--- a/docs/zh/UserGuide/API/Programming-Java-Native-API.md
+++ b/docs/zh/UserGuide/API/Programming-Java-Native-API.md
@@ -364,6 +364,17 @@ void insertTablets(Map<String, Tablet> tablets)
* 插入一个 Record,一个 Record 是一个设备一个时间戳下多个测点的数据。这里的 value 是 Object
类型,相当于提供了一个公用接口,后面可以通过 TSDataType 将 value 强转为原类型
+ 其中,Object 类型与 TSDataType 类型的对应关系如下表所示:
+
+ | TSDataType | Object |
+ | ---------- | -------------- |
+ | BOOLEAN | Boolean |
+ | INT32 | Integer |
+ | INT64 | Long |
+ | FLOAT | Float |
+ | DOUBLE | Double |
+ | TEXT | String, Binary |
+
```java
void insertRecord(String prefixPath, long time, List<String> measurements,
List<TSDataType> types, List<Object> values)
diff --git a/example/session/src/main/java/org/apache/iotdb/SessionExample.java
b/example/session/src/main/java/org/apache/iotdb/SessionExample.java
index b86a4cbcf7..8c654c0d22 100644
--- a/example/session/src/main/java/org/apache/iotdb/SessionExample.java
+++ b/example/session/src/main/java/org/apache/iotdb/SessionExample.java
@@ -31,12 +31,15 @@ import org.apache.iotdb.session.util.Version;
import org.apache.iotdb.tsfile.file.metadata.enums.CompressionType;
import org.apache.iotdb.tsfile.file.metadata.enums.TSDataType;
import org.apache.iotdb.tsfile.file.metadata.enums.TSEncoding;
+import org.apache.iotdb.tsfile.utils.Binary;
import org.apache.iotdb.tsfile.utils.BitMap;
import org.apache.iotdb.tsfile.write.record.Tablet;
import org.apache.iotdb.tsfile.write.schema.MeasurementSchema;
import java.io.IOException;
import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@@ -86,6 +89,7 @@ public class SessionExample {
// insertTabletWithNullValues();
// insertTablets();
// insertRecords();
+ // insertText();
// selectInto();
// createAndDropContinuousQueries();
// nonQuery();
@@ -597,6 +601,44 @@ public class SessionExample {
}
}
+ /**
+ * This example shows how to insert data of TSDataType.TEXT. You can use the
session interface to
+ * write data of String type or Binary type.
+ */
+ private static void insertText() throws IoTDBConnectionException,
StatementExecutionException {
+ String device = "root.sg1.text_type";
+ // the first data is String type and the second data is Binary type
+ List<Object> datas = Arrays.asList("String", new Binary("Binary"));
+ // insertRecord example
+ for (int i = 0; i < datas.size(); i++) {
+ // write data of String type or Binary type
+ session.insertRecord(
+ device,
+ i,
+ Collections.singletonList("s1"),
+ Collections.singletonList(TSDataType.TEXT),
+ datas.get(i));
+ }
+
+ // insertTablet example
+ List<MeasurementSchema> schemaList = new ArrayList<>();
+ schemaList.add(new MeasurementSchema("s2", TSDataType.TEXT));
+ Tablet tablet = new Tablet(device, schemaList, 100);
+ for (int i = 0; i < datas.size(); i++) {
+ int rowIndex = tablet.rowSize++;
+ tablet.addTimestamp(rowIndex, i);
+ // write data of String type or Binary type
+ tablet.addValue(schemaList.get(0).getMeasurementId(), rowIndex,
datas.get(i));
+ }
+ session.insertTablet(tablet);
+ try (SessionDataSet dataSet = session.executeQueryStatement("select s1, s2
from " + device)) {
+ System.out.println(dataSet.getColumnNames());
+ while (dataSet.hasNext()) {
+ System.out.println(dataSet.next());
+ }
+ }
+ }
+
private static void selectInto() throws IoTDBConnectionException,
StatementExecutionException {
session.executeNonQueryStatement(
"select s1, s2, s3 into into_s1, into_s2, into_s3 from root.sg1.d1");
diff --git
a/integration/src/test/java/org/apache/iotdb/session/IoTDBSessionSimpleIT.java
b/integration/src/test/java/org/apache/iotdb/session/IoTDBSessionSimpleIT.java
index 7bc81c11fe..dc7db03795 100644
---
a/integration/src/test/java/org/apache/iotdb/session/IoTDBSessionSimpleIT.java
+++
b/integration/src/test/java/org/apache/iotdb/session/IoTDBSessionSimpleIT.java
@@ -60,6 +60,7 @@ import java.io.IOException;
import java.security.SecureRandom;
import java.util.ArrayList;
import java.util.Arrays;
+import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
@@ -474,6 +475,48 @@ public class IoTDBSessionSimpleIT {
session.close();
}
+ @Test
+ public void testInsertTabletWithStringValues()
+ throws IoTDBConnectionException, StatementExecutionException {
+ session = new Session("127.0.0.1", 6667, "root", "root");
+ session.open();
+ List<MeasurementSchema> schemaList = new ArrayList<>();
+ schemaList.add(new MeasurementSchema("s0", TSDataType.DOUBLE,
TSEncoding.RLE));
+ schemaList.add(new MeasurementSchema("s1", TSDataType.FLOAT,
TSEncoding.RLE));
+ schemaList.add(new MeasurementSchema("s2", TSDataType.INT64,
TSEncoding.RLE));
+ schemaList.add(new MeasurementSchema("s3", TSDataType.INT32,
TSEncoding.RLE));
+ schemaList.add(new MeasurementSchema("s4", TSDataType.BOOLEAN,
TSEncoding.RLE));
+ schemaList.add(new MeasurementSchema("s5", TSDataType.TEXT,
TSEncoding.RLE));
+ schemaList.add(new MeasurementSchema("s6", TSDataType.TEXT,
TSEncoding.RLE));
+
+ Tablet tablet = new Tablet("root.sg1.d1", schemaList);
+ for (long time = 0; time < 10; time++) {
+ int rowIndex = tablet.rowSize++;
+ tablet.addTimestamp(rowIndex, time);
+
+ tablet.addValue(schemaList.get(0).getMeasurementId(), rowIndex, (double)
time);
+ tablet.addValue(schemaList.get(1).getMeasurementId(), rowIndex, (float)
time);
+ tablet.addValue(schemaList.get(2).getMeasurementId(), rowIndex, time);
+ tablet.addValue(schemaList.get(3).getMeasurementId(), rowIndex, (int)
time);
+ tablet.addValue(schemaList.get(4).getMeasurementId(), rowIndex, time % 2
== 0);
+ tablet.addValue(schemaList.get(5).getMeasurementId(), rowIndex, new
Binary("Text" + time));
+ tablet.addValue(schemaList.get(6).getMeasurementId(), rowIndex, "Text" +
time);
+ }
+
+ if (tablet.rowSize != 0) {
+ session.insertTablet(tablet);
+ tablet.reset();
+ }
+
+ SessionDataSet dataSet = session.executeQueryStatement("select * from
root.sg1.d1");
+ while (dataSet.hasNext()) {
+ RowRecord rowRecord = dataSet.next();
+ List<Field> fields = rowRecord.getFields();
+ Assert.assertEquals(fields.get(5).getBinaryV(),
fields.get(6).getBinaryV());
+ }
+ session.close();
+ }
+
@Test
public void createTimeSeriesWithDoubleTicks()
throws IoTDBConnectionException, StatementExecutionException {
@@ -1440,6 +1483,62 @@ public class IoTDBSessionSimpleIT {
session.close();
}
+ @Test
+ public void testInsertBinaryAsText()
+ throws IoTDBConnectionException, StatementExecutionException {
+ session = new Session("127.0.0.1", 6667, "root", "root");
+ session.open();
+ // prepare binary data
+ List<Object> bytesData = new ArrayList<>();
+ for (int i = 0; i < 3; i++) {
+ byte[] bytes = new byte[128];
+ for (int j = 0; j < 128; j++) {
+ bytes[j] = Byte.valueOf("" + (j - i), 10);
+ }
+ bytesData.add(bytes);
+ }
+ // insert data using insertRecord
+ for (int i = 0; i < bytesData.size(); i++) {
+ byte[] data = (byte[]) bytesData.get(i);
+ Binary dataBinary = new Binary(data);
+ session.insertRecord(
+ "root.sg1.d1",
+ i,
+ Collections.singletonList("s0"),
+ Collections.singletonList(TSDataType.TEXT),
+ dataBinary);
+ }
+ // insert data using insertTablet
+ List<MeasurementSchema> schemaList = new ArrayList<>();
+ schemaList.add(new MeasurementSchema("s1", TSDataType.TEXT));
+ Tablet tablet = new Tablet("root.sg1.d1", schemaList, 100);
+ for (int i = 0; i < bytesData.size(); i++) {
+ byte[] data = (byte[]) bytesData.get(i);
+ int rowIndex = tablet.rowSize++;
+ tablet.addTimestamp(rowIndex, i);
+ Binary dataBinary = new Binary(data);
+ tablet.addValue(schemaList.get(0).getMeasurementId(), rowIndex,
dataBinary);
+ }
+ session.insertTablet(tablet);
+ // check result
+ SessionDataSet dataSet = session.executeQueryStatement("select ** from
root.sg1.d1");
+ Assert.assertArrayEquals(
+ dataSet.getColumnNames().toArray(new String[0]),
+ new String[] {"Time", "root.sg1.d1.s0", "root.sg1.d1.s1"});
+ while (dataSet.hasNext()) {
+ RowRecord rowRecord = dataSet.next();
+ for (int i = 0; i < 2; i++) {
+ // get Binary value from SessionDataSet
+ byte[] actualBytes =
rowRecord.getFields().get(i).getBinaryV().getValues();
+ // compare Binary value to origin bytesData
+ byte[] expectedBytes = (byte[]) bytesData.get((int)
rowRecord.getTimestamp());
+ Assert.assertArrayEquals(expectedBytes, actualBytes);
+ }
+ }
+ dataSet.closeOperationHandle();
+ session.close();
+ }
+
private void initTreeTemplate(String path)
throws IoTDBConnectionException, StatementExecutionException,
IOException {
Template sessionTemplate = new Template("treeTemplate", true);
diff --git
a/session/src/main/java/org/apache/iotdb/session/util/SessionUtils.java
b/session/src/main/java/org/apache/iotdb/session/util/SessionUtils.java
index f2f8a663ad..30bdd9b1db 100644
--- a/session/src/main/java/org/apache/iotdb/session/util/SessionUtils.java
+++ b/session/src/main/java/org/apache/iotdb/session/util/SessionUtils.java
@@ -107,7 +107,11 @@ public class SessionUtils {
break;
case TEXT:
res += Integer.BYTES;
- res += ((String)
values.get(i)).getBytes(TSFileConfig.STRING_CHARSET).length;
+ if (values.get(i) instanceof Binary) {
+ res += ((Binary) values.get(i)).getValues().length;
+ } else {
+ res += ((String)
values.get(i)).getBytes(TSFileConfig.STRING_CHARSET).length;
+ }
break;
default:
throw new IoTDBConnectionException(MSG_UNSUPPORTED_DATA_TYPE +
types.get(i));
@@ -149,7 +153,12 @@ public class SessionUtils {
ReadWriteIOUtils.write((Double) values.get(i), buffer);
break;
case TEXT:
- byte[] bytes = ((String)
values.get(i)).getBytes(TSFileConfig.STRING_CHARSET);
+ byte[] bytes;
+ if (values.get(i) instanceof Binary) {
+ bytes = ((Binary) values.get(i)).getValues();
+ } else {
+ bytes = ((String)
values.get(i)).getBytes(TSFileConfig.STRING_CHARSET);
+ }
ReadWriteIOUtils.write(bytes.length, buffer);
buffer.put(bytes);
break;
diff --git
a/tsfile/src/main/java/org/apache/iotdb/tsfile/write/record/Tablet.java
b/tsfile/src/main/java/org/apache/iotdb/tsfile/write/record/Tablet.java
index dd919f448c..bb66da22a9 100644
--- a/tsfile/src/main/java/org/apache/iotdb/tsfile/write/record/Tablet.java
+++ b/tsfile/src/main/java/org/apache/iotdb/tsfile/write/record/Tablet.java
@@ -145,7 +145,11 @@ public class Tablet {
case TEXT:
{
Binary[] sensor = (Binary[]) values[indexOfSchema];
- sensor[rowIndex] = value != null ? (Binary) value :
Binary.EMPTY_VALUE;
+ if (value instanceof Binary) {
+ sensor[rowIndex] = (Binary) value;
+ } else {
+ sensor[rowIndex] = value != null ? new Binary((String) value) :
Binary.EMPTY_VALUE;
+ }
break;
}
case FLOAT: