database Cassandra:文本与 varchar

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17530230/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 09:00:22  来源:igfitidea点击:

Cassandra: text vs varchar

databasecassandra

提问by darcyq

Does anyone know the difference between the two CQL data types textand varcharin Cassandra? The Cassandra documentation describes both types as "UTF-8 encoded string" and nothing more.

有谁知道两种 CQL 数据类型textvarcharCassandra之间的区别?Cassandra 文档将这两种类型都描述为“UTF-8 编码字符串”,仅此而已。

回答by Lyuben Todorov

textis just an alias for varchar!

text只是一个别名varchar

The documentation:

文档:

EDIT
Here's the link to the C* 1.2 docs. The text vs varchar info is still the same, however this document contains some extra datatypes.

编辑
这是C* 1.2 文档的链接。text vs varchar 信息仍然相同,但是此文档包含一些额外的数据类型。

EDIT v2Documentation links have been updated to the docs for C* 3. I couldn't find a good alternative for the C* 1.2 docs.

编辑 v2文档链接已更新到 C* 3 的文档。我找不到 C* 1.2 文档的好的替代方案。

回答by Nikola Yovchev

Probably you meant the CQL storage types, if not, disregard my answer.

可能您的意思是 CQL 存储类型,如果不是,请忽略我的回答。

In CQL there has been a ongoing trend to try to distance from the internals of cassandra. Whether that is a good thing, or a bad thing, is open to interpretation. What is relevant, however, is in latest versions of CQL developers have been trying to come up with syntax that is more familiar to people who are not that in depth into cassandra's internals.

在 CQL 中,有一种持续的趋势试图与 cassandra 的内部保持距离。这是好事还是坏事,都有待解释。然而,相关的是,在最新版本的 CQL 中,开发人员一直试图提出对不深入了解 cassandra 内部结构的人更熟悉的语法。

If you were to take a look into this SO question, you will get a nice illustration of the situation: Creating column family or table in Cassandra while working Datastax API(which uses new Binary protocol)

如果您要查看这个 SO 问题,您将很好地说明这种情况: 在工作 Datastax API 时在 Cassandra 中创建列族或表(使用新的二进制协议)

In recent CQL versions, some aliases, alien to cassandra, but very well known to DBA's have started to appear. For example, the native to cassandra ColumnFamily has been aliased with Table, and text is just an alias for varchar and vice versa. Again, it is a matter of opinion if that is a good thing or not.

在最近的 CQL 版本中,一些别名与 cassandra 不同,但 DBA 非常熟悉。例如,cassandra 的原生 ColumnFamily 已经使用了 Table 的别名,而 text 只是 varchar 的别名,反之亦然。同样,这是一件好事还是坏事,这是一个见仁见智的问题。

So, in conclusion, you can use varchar and text interchangeably.

因此,总而言之,您可以互换使用 varchar 和 text。

回答by Sarel Esterhuizen

This threw me too when I started with Cassandra.

当我开始使用 Cassandra 时,这也让我感到困惑。

Both text and varchar are UTF8 encoded strings and are synonyms for each other, that is they are exactly the same thing.

text 和 varchar 都是 UTF8 编码的字符串,并且是彼此的同义词,也就是说它们是完全相同的东西。

As an added side note if one comes from a relational world like MS SQL, one would perhaps also be hesitant to use these types (especially TEXT) as the primary field for an entity. TEXT is especially usually associated with big blobs of text content that don't scream primary key to ones 3rd normal form relational mind. But since all Cassandra types are essentially stored as hexadecimal byte arrays on the disk there is no real significant performance when using them as the primary key.

作为补充说明,如果一个人来自像 MS SQL 这样的关系世界,人们可能也会犹豫是否使用这些类型(尤其是 TEXT)作为实体的主要字段。TEXT 通常与大块的文本内容相关联,这些文本内容不会尖叫到第三范式关系思维的主键。但是由于所有 Cassandra 类型本质上都以十六进制字节数组的形式存储在磁盘上,因此将它们用作主键时并没有真正显着的性能。

回答by Putti

Cassandra CQL Data Types textand varcharare synonmys/alias for each other.

Cassandra CQL 数据类型textvarchar是彼此的同义词/别名。

  1. Data Type associated to Varchar is blob(The max theoretical size for a blob is 2 GB)
  2. Data Type associated to textis Varchar (meaning even you have used textbut Cassandra internally treats as Varchar)
  3. blob type association will not create performance issues because Cassandra stores data in constant hexadecimal number.
  4. Reads will be faster due to Cassandra queries the right coordinatesusing primary key (partition key, clustering column)depending on how we design our table. enter image description hereenter image description here
  1. 与 Varchar 关联的数据类型是blobblob的最大理论大小为2 GB
  2. 文本关联的数据类型是 Varchar (意味着即使您使用过text但 Cassandra 在内部将其视为Varchar
  3. blob 类型关联不会产生性能问题,因为 Cassandra 以恒定的十六进制数存储数据。
  4. 由于 Cassandra使用主键(分区键、集群列)查询正确的坐标,读取速度会更快这取决于我们如何设计表。 在此处输入图片说明在此处输入图片说明