luoyuxia commented on issue #1569:
URL: https://github.com/apache/fluss/issues/1569#issuecomment-3213932870
We should have a better way to write to lance
```
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.alibaba.fluss.lake.lance.tiering;
import org.apache.arrow.memory.RootAllocator;
import org.apache.arrow.vector.Float8Vector;
import org.apache.arrow.vector.IntVector;
import org.apache.arrow.vector.VarCharVector;
import org.apache.arrow.vector.VectorSchemaRoot;
import org.apache.arrow.vector.ipc.ArrowStreamReader;
import org.apache.arrow.vector.ipc.ArrowStreamWriter;
import org.apache.arrow.vector.types.FloatingPointPrecision;
import org.apache.arrow.vector.types.pojo.ArrowType;
import org.apache.arrow.vector.types.pojo.Field;
import org.apache.arrow.vector.types.pojo.FieldType;
import org.apache.arrow.vector.types.pojo.Schema;
import org.junit.jupiter.api.Test;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;
import static org.assertj.core.api.Assertions.assertThat;
class LanceTest {
public static ArrowStreamReader createArrowStreamReader(List<Object[]>
data)
throws IOException {
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
try (RootAllocator allocator = new RootAllocator(Long.MAX_VALUE)) {
Schema schema = createSchema();
try (VectorSchemaRoot root = VectorSchemaRoot.create(schema,
allocator)) {
ArrowStreamWriter writer = new ArrowStreamWriter(root, null,
outputStream);
writer.start();
for (int batchNum = 0; batchNum < 5; batchNum++) {
writeBatchToStream(root, data);
writer.writeBatch();
}
writer.end();
}
}
// Create ArrowStreamReader from the generated data
byte[] arrowData = outputStream.toByteArray();
ByteArrayInputStream inputStream = new
ByteArrayInputStream(arrowData);
return new ArrowStreamReader(inputStream, new
RootAllocator(Long.MAX_VALUE));
}
private static Schema createSchema() {
return new Schema(
Arrays.asList(
new Field(
"id", new FieldType(true, new
ArrowType.Int(32, true), null), null),
new Field("name", new FieldType(true, new
ArrowType.Utf8(), null), null),
new Field(
"age",
new FieldType(true, new ArrowType.Int(32,
true), null),
null),
new Field(
"salary",
new FieldType(
true,
new
ArrowType.FloatingPoint(FloatingPointPrecision.DOUBLE),
null),
null)));
}
private static void writeBatchToStream(VectorSchemaRoot root,
List<Object[]> data) {
root.clear();
IntVector idVector = (IntVector) root.getVector("id");
VarCharVector nameVector = (VarCharVector) root.getVector("name");
IntVector ageVector = (IntVector) root.getVector("age");
Float8Vector salaryVector = (Float8Vector) root.getVector("salary");
int batchSize = data.size();
// Allocate memory
idVector.allocateNew(batchSize);
nameVector.allocateNew(batchSize);
ageVector.allocateNew(batchSize);
salaryVector.allocateNew(batchSize);
// Fill data
for (int i = 0; i < batchSize; i++) {
Object[] row = data.get(i);
idVector.set(i, (Integer) row[0]);
nameVector.setSafe(i, ((String) row[1]).getBytes());
ageVector.set(i, (Integer) row[2]);
salaryVector.set(i, (Double) row[3]);
}
// Set value counts
idVector.setValueCount(batchSize);
nameVector.setValueCount(batchSize);
ageVector.setValueCount(batchSize);
salaryVector.setValueCount(batchSize);
root.setRowCount(batchSize);
}
@Test
void testLance() throws Exception {
List<Object[]> data =
Arrays.asList(
new Object[] {1, "John Doe", 10, 85.5},
new Object[] {2, "Jane Smith", 12, 92.0},
new Object[] {3, "Bob Johnson", 23, 78.5});
int batchCount = 0;
int totalRowsRead = 0;
try (ArrowStreamReader arrowStreamReader =
createArrowStreamReader(data)) {
while (arrowStreamReader.loadNextBatch()) {
batchCount++;
VectorSchemaRoot root =
arrowStreamReader.getVectorSchemaRoot();
totalRowsRead += root.getRowCount();
assertThat(root.getRowCount()).isEqualTo(data.size());
// Verify the content of the current batch
for (int i = 0; i < root.getRowCount(); i++) {
Object[] expectedRow = data.get(i);
assertThat(root.getVector("id").getObject(i)).isEqualTo(expectedRow[0]);
assertThat(root.getVector("name").getObject(i).toString())
.isEqualTo(expectedRow[1]);
assertThat(root.getVector("age").getObject(i)).isEqualTo(expectedRow[2]);
assertThat(root.getVector("salary").getObject(i)).isEqualTo(expectedRow[3]);
}
}
}
// Assert that we read the correct number of batches and total rows.
assertThat(batchCount).isEqualTo(5);
assertThat(totalRowsRead).isEqualTo(15);
}
}
```
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]