考虑以下代码

    try (final ColumnFamilyOptions cfOpts = new ColumnFamilyOptions().optimizeUniversalStyleCompaction()) {
        final List<ColumnFamilyDescriptor> cfDescriptors = Arrays.asList(
                new ColumnFamilyDescriptor(RocksDB.DEFAULT_COLUMN_FAMILY, cfOpts),
                new ColumnFamilyDescriptor("my-first-columnfamily".getBytes(), cfOpts)
        );

        final List<ColumnFamilyHandle> columnFamilyHandleList =
                new ArrayList<>();

        try (final DBOptions options = new DBOptions()
                .setCreateIfMissing(true)
                .setCreateMissingColumnFamilies(true);
             final RocksDB db = RocksDB.open(options,
                     "/mnt/my_db", cfDescriptors,
                     columnFamilyHandleList)) {
            try {
                // Question: How do I refer to a particular column family by name, for example my-first-columnfamily
                db.put(columnFamilyHandleList.get(0), "myKey".getBytes(), "myValue".getBytes());

            } finally {
                for (final ColumnFamilyHandle columnFamilyHandle :
                        columnFamilyHandleList) {
                    columnFamilyHandle.close();
                }
            }
        } catch(Exception e) {

        }
    }
}

在下一行
db.put(columnFamilyHandleList.get(0), "myKey".getBytes(), "myValue".getBytes());
我希望put能够选择要转换为column familyput,但是put采用ColumnFamilyHandle,而ColumnFamilyHandle不包含描述符名称,因此很难选择放置目标。

有人可以指出实现上述目标的方向吗?

谢谢

最佳答案

ColumnFamilyHandle具有getName方法,请参见here

因此,您可以创建一个助手方法,该方法在列族句柄列表中搜索名称。

 public ColumnFamilyHandle getColumnFamilyHandle(byte[] name) {
    return columnFamilyHandles
        .stream()
        .filter(
            handle -> {
              try {
                return Arrays.equals(handle.getName(), name);
              } catch (Exception ex) {
                throw new RuntimeException(ex);
              }
            })
        .findAny()
        .orElse(null);
  }

10-01 12:06