在Apache Druid中保持多值维度中元素的顺序,你可以使用array
类型的维度字段来实现。以下是一个使用Java代码示例的解决方法:
orders
的数据源,其中包含一个名为products
的多值维度字段。import io.druid.data.input.impl.DimensionsSpec;
import io.druid.data.input.impl.JSONParseSpec;
import io.druid.data.input.impl.StringInputRowParser;
import io.druid.java.util.common.parsers.JSONPathSpec;
DimensionsSpec dimensionsSpec = new DimensionsSpec(
DimensionsSpec.getDefaultSchemas(Arrays.asList("products"))
);
JSONPathSpec productPathSpec = new JSONPathSpec(true, new String[]{"$.products"});
JSONParseSpec parseSpec = new JSONParseSpec(
new TimestampSpec("timestamp", "auto", null),
new DimensionsSpec(null, null, null),
null,
null,
Arrays.asList(productPathSpec)
);
StringInputRowParser parser = new StringInputRowParser(parseSpec, null);
parser.startFileFromBeginning(new ByteArrayInputStream(data.getBytes("UTF-8")));
array
类型。你可以使用Druid的expression
函数来实现这一点。在上述示例中,我们使用了JSONPathSpec来定义products
字段的路径,然后将其转换为array
类型。import io.druid.query.expression.Expression;
import io.druid.query.expression.LookupEnabledTestExprMacroTable;
String expression = "array($products)";
LookupEnabledTestExprMacroTable macroTable = new LookupEnabledTestExprMacroTable();
Expression expr = parser.parse(expression, macroTable);
import io.druid.data.input.InputRow;
InputRow row = parser.parse();
row = expr.eval(row);
// 添加到数据源
YourDataStore.add(row);
这样,元素的顺序就会得到保持,并且你可以在查询时按照顺序进行处理。