【问题标题】:How do you insert a string or text as a blob in Cassandra (specifically CQLSH)?如何在 Cassandra(特别是 CQLSH)中将字符串或文本作为 blob 插入?
【发布时间】:2014-06-07 12:19:24
【问题描述】:

我试图在 CQLSH 中插入文本或某些字符串作为 blob 用于测试目的

insert into test_by_score (commit, delta, test, score)
       values (textAsBlob('bdb14fbe076f6b94444c660e36a400151f26fc6f'), 0,
       textAsBlob('{"prefix": "enwiki", "title": "\"Aghnadarragh\""}'), 100);

它并没有真正起作用,因为在我这样做之后:

select * from test_by_score where commit = 0x0b5db8b91bfdeb0a304b372dd8dda123b3fd1ab6;

它说有 0 列...这有点出乎意料(因为它没有向我抛出错误)但我猜 textAsBlob 在 cqlsh 中不是一个东西。那么有人知道怎么做吗?


架构:

CREATE TABLE IF NOT EXISTS test_by_score (
    commit blob, 
    delta int, 
    score int, 
    test blob, 
    PRIMARY KEY(commit, delta, test)
);

我有点不情愿地发布了我的架构,因为我相信我的问题并不是关于这个特定的架构。我只想知道,如果有一列包含 blob,是否可以通过先将字符串更改为 blob 然后将其插入 cqlsh 来在该位置插入字符串?

【问题讨论】:

  • 您能发布 CQL 架构吗?你是在使用程序使用 CQLSH 插入 Blob 吗?还是要通过 CQLSH 手动插入?
  • 我要手动插入文字。
  • 我的具体架构应该无关紧要。我的问题更笼统。我有一个字符串,我想将它转换为一个 blob 并将其插入到仅包含 blob 的列中。可以手动完成吗?
  • 只是为了澄清,希望架构用您给定的查询重新创建问题...
  • 别担心,无论如何,让问题尽可能清晰总是好的,这样它对我和将来的任何人都一样有用。 :)

标签: cassandra cqlsh


【解决方案1】:

以下似乎工作。 SELECT 语句中的 WHERE 条件可能试图访问不正确的十六进制 0x0b5db8b91bfdeb0a304b372dd8dda123b3fd1ab6

DROP KEYSPACE example;
CREATE KEYSPACE example WITH replication = {'class':'SimpleStrategy', 'replication_factor':1};
USE example;

CREATE TABLE IF NOT EXISTS test_by_score (
    commit blob, -- blob representing the commit hash
    delta int, -- how much the scores have changed
    score int, -- the test score, which is determined by the client
    test blob, -- blob for the test
    PRIMARY KEY(commit, delta, test)
);

INSERT INTO test_by_score  (commit, delta, test, score) VALUES 
  (textAsBlob('bdb14fbe076f6b94444c660e36a400151f26fc6f'), 0, textAsBlob('{"prefix": "enwiki", "title": "\"Aghnadarragh\""}'), 100
);

INSERT INTO test_by_score (commit, delta, test, score) VALUES (
  textAsBlob('cdb14fbe076f6b94444c660e36a400151f26fc6f'), 0, textAsBlob('{"prefix": "enwiki", "title": "\"Aghnadarragh\""}'), 100
);

INSERT INTO test_by_score (commit, delta, test, score) VALUES (
  textAsBlob('adb14fbe076f6b94444c660e36a400151f26fc6f'), 0, textAsBlob('{"prefix": "enwiki", "title": "\"Aghnadarragh\""}'), 100
);

抓取所有行

SELECT * FROM example.test_by_score 

选择特定行。

SELECT * FROM example.test_by_score 
  WHERE commit = 0x62646231346662653037366636623934343434633636306533366134303031353166323666633666;

【讨论】:

猜你喜欢
  • 2016-03-14
  • 2016-04-08
  • 2014-05-05
  • 1970-01-01
  • 2015-09-13
  • 2022-10-18
  • 2015-10-27
  • 2019-02-08
相关资源
最近更新 更多