How do you insert a string or text as a blob in Cassandra (specifically CQLSH)?

旧街凉风 提交于 2019-12-12 08:23:51

问题


I was trying to insert text or some string as a blob for testing purposes in CQLSH

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

It didn't really work, because after I did:

select * from test_by_score where commit = 0x0b5db8b91bfdeb0a304b372dd8dda123b3fd1ab6;

its said there were 0 columns...which was a little unexpected (because it didn't throw an error at me) but I guess textAsBlob is not a thing in cqlsh. Then does someone know how to do this?


Schema:

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

I have posted my schema a little reluctantly because I believe my question is not really about this specific schema. What I simply want to to know is, if there is one column that holds blobs, is it possible to insert a string in that position by first changing it to a blob and then inserting it in cqlsh?


回答1:


The following seems to be working. Your WHERE condition in your SELECT statement may be trying to access the incorrect hex 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
);

To grab all the rows

SELECT * FROM example.test_by_score 

To select a specific row.

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


来源:https://stackoverflow.com/questions/23231207/how-do-you-insert-a-string-or-text-as-a-blob-in-cassandra-specifically-cqlsh

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!