CASSANDRA CQL3:将值设置为整列

时间:2015-08-05 14:24:09

标签: database cassandra cql cql3 cqlsh

对于我的Cassandra数据库,我需要在列中为表中的所有行设置一个值。

我在SQL中看到,我们可以这样做:

UPDATE table SET column1= XXX;

但是在CQL(在cqlsh中),它不起作用! 我不想逐行更新,直到9500行。

你有什么建议吗?

谢谢:)

3 个答案:

答案 0 :(得分:4)

您可以将更新查询与IN子句一起使用,而不是执行9500查询。 首先从表中选择primary_key,然后将值复制到此查询:

UPDATE table SET column1 = XXX WHERE primary_key IN (p1, p2, p3, ...);

答案 1 :(得分:1)

我刚刚向表(+60000行)中添加了一个新列,并且我找到了用某种东西(不是null)初始化该列的所有值的方法,但是我什么也没发现。这里的问题不尽相同,但是如果您删除并添加该列,我的解决方案将解决该问题。所以,这就是我所做的:

cqlsh> COPY tablename (primary_key, newcolumn) TO 'FILE.txt'

在记事本++上打开FILE.TXT,然后按Ctrl + H(“替换”选项),并将所有\r\n替换为'something \ r \ n'

最后

cqlsh> COPY tablename (primary_key, newcolumn) FROM 'FILE.txt'

注意1:如果primary_key中包含\r\n,则应格外小心。

注意2:可能在您的SO中,行的结尾不是\r\n

答案 2 :(得分:0)

正如您所知,CQL!= SQL。没有办法做你在CQL中提出的问题,而不是迭代你表中的每一行。

罗伯特关于将public class FirstPath extends BasePath { public static final String TAG = " FirstPath"; public final int parameter; public FirstPath(int parameter) { this.parameter = parameter; } //... @Override public int getLayout() { return R.layout.path_first; } @Override public FirstViewComponent createComponent() { FirstPath.FirstViewComponent firstViewComponent = DaggerFirstPath_FirstViewComponent.builder() .applicationComponent(InjectorService.obtain()) .firstViewModule(new FirstPath.FirstViewModule(parameter)) .build(); return firstViewComponent; } @Override public String getScopeName() { return TAG + "_" + parameter; } @ViewScope //needed @Component(dependencies = {ApplicationComponent.class}, modules = {FirstViewModule.class}) public interface FirstViewComponent extends ApplicationComponent { String data(); FirstViewPresenter firstViewPresenter(); void inject(FirstView firstView); void inject(FirstViewPresenter firstViewPresenter); } @Module public static class FirstViewModule { private int parameter; public FirstViewModule(int parameter) { this.parameter = parameter; } @Provides public String data(Context context) { return context.getString(parameter); } @Provides @ViewScope //needed to preserve scope public FirstViewPresenter firstViewPresenter() { return new FirstViewPresenter(); } } public static class FirstViewPresenter extends ViewPresenter<FirstView> { public static final String TAG = FirstViewPresenter.class.getSimpleName(); @Inject String data; public FirstViewPresenter() { Log.d(TAG, "First View Presenter created: " + toString()); } @Override protected void onEnterScope(MortarScope scope) { super.onEnterScope(scope); FirstViewComponent firstViewComponent = scope.getService(DaggerService.TAG); firstViewComponent.inject(this); Log.d(TAG, "Data [" + data + "] and other objects injected to first presenter."); } @Override protected void onSave(Bundle outState) { super.onSave(outState); FirstView firstView = getView(); outState.putString("input", firstView.getInput()); } @Override protected void onLoad(Bundle savedInstanceState) { super.onLoad(savedInstanceState); if(!hasView()) { return; } FirstView firstView = getView(); if(savedInstanceState != null) { //needed check firstView.setInput(savedInstanceState.getString("input")); } } public void goToNextActivity() { FirstPath firstPath = Path.get(getView().getContext()); if(firstPath.parameter != R.string.hello_world) { Flow.get(getView()).set(new FirstPath(R.string.hello_world)); } else { Flow.get(getView()).set(new SecondPath()); } } } } 重新定义为each screen has its own "path", and each path has its own component的建议可能会有所帮助。但静态列与其分区键相关联,因此您仍需要指定:

column1

此外,听起来您只希望为所有行设置列值。如果您希望分区内的CQL行的列值不同(从DataStax文档中的示例),静态列对您不起作用:

aploetz@cqlsh:stackoverflow2> UPDATE t SET s='XXX' WHERE k='k';

请注意,列aploetz@cqlsh:stackoverflow2> INSERT INTO t (k, s, i) VALUES ('k', 'I''m shared', 0); aploetz@cqlsh:stackoverflow2> INSERT INTO t (k, s, i) VALUES ('k', 'I''m still shared', 1); aploetz@cqlsh:stackoverflow2> SELECT * FROM t; k | i | s ---+---+------------------ k | 0 | I'm still shared k | 1 | I'm still shared (2 rows) 的值在分区键s下的所有CQL行中都是相同的。只是让你了解它是如何运作的。