如何使用c#从Cassandra中删除一系列行

时间:2014-09-28 19:29:05

标签: c# cassandra

我想让这个程序运行。数据库已创建,其中包含值。 BigInt类型的列“ts”是主键。当我尝试执行

session.Execute(deleteAll);

它会引发异常。从c#中删除Cassandra中所有行的正确方法是什么,其中键位于value1和value2之间?

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Cassandra;
using Cassandra.Data;

namespace CasandraGettingStarted
{
    class Program
    {
        static long ninetySeventyTicks = new DateTime(1970, 1, 1).Ticks;
        static string symbol = "SPX";

        static void Main(string[] args)
        {
            StartCassandra();
        }

        protected static long ToUnixTimestampMicro(DateTime dateTime)
        {
            return (dateTime.Ticks - ninetySeventyTicks) / 10;
        }

        static DateTime ConvertFromUnixTimestampMicro(double timestamp)
        {
            return new DateTime((long)timestamp * 10).AddYears(1969);//.ToLocalTime();
        }

        static void StartCassandra()
        {
            try
            {
                System.Random random = new Random(123456);

                Cluster cluster = Cluster.Builder().AddContactPoint("127.0.0.1").Build();
                ISession session = cluster.Connect("timeseries");

                double minPrice = 1100.00;
                double maxPrice = 1200.00;

                for (int i = 0; i < 10; i++)
                {
                    //long timestamp = (DateTime.Now.Ticks - new DateTime(1970, 1, 1).Ticks) / 10;
                    System.DateTime nowMicro = System.DateTime.Now;
                    long timestamp = ToUnixTimestampMicro(nowMicro);
                    System.DateTime nowMicro1 = ConvertFromUnixTimestampMicro(timestamp);

                    Console.WriteLine(nowMicro.ToString("HH:mm:ss.ffffff"));
                    Console.WriteLine(nowMicro1.ToString("HH:mm:ss.ffffff"));

                    double bidD = random.NextDouble();
                    bidD = bidD * (maxPrice - minPrice) + minPrice;
                    double askD = bidD + random.NextDouble();

                    decimal bid = (decimal)bidD;
                    decimal ask = (decimal)askD;

                    bid = decimal.Round(bid, 2);
                    ask = decimal.Round(ask, 2);

                    Console.WriteLine("Adding {0} {1} {2} {3}", timestamp, symbol, bid, ask);

                    var batchStmt = new BatchStatement();
                    var v2Insert = new SimpleStatement("insert into underlying_data " +
                        "(ts, symbol, bid, ask) values(?, ?, ?, ?);");
                    batchStmt.Add(v2Insert.Bind(timestamp, symbol, bid, ask));

                    session.Execute(batchStmt);
                    // Insert Data
                    //session.Execute("insert into underlying_data (ts, symbol, bid, ask) values (timestamp, 'SPX', bid, ask)");

                    //Thread.Sleep(1);
                }

                RowSet result = session.Execute("select * from underlying_data");

                foreach (Row row in result)
                    Console.WriteLine("{0} {1} {2} {3}", row["ts"], row["symbol"], row["bid"], row["ask"]);


                string deleteAll = @"select * from underlying_data where ts > ";
                deleteAll += ninetySeventyTicks.ToString();

                session.Execute(deleteAll);

                RowSet rows = session.Execute("select * from underlying_data");

                foreach (Row row in result)
                    Console.WriteLine("{0} {1} {2} {3}", row["ts"], row["symbol"], row["bid"], row["ask"]);
            }
            catch(Exception ex)
            {
                Console.WriteLine(ex.ToString());
            }

            // Wait for enter key before exiting
            Console.ReadLine();
        }
    }
}

1 个答案:

答案 0 :(得分:0)

session.Execute(deleteAll);

首先,看起来deleteAll定义为SELECT查询:

string deleteAll = @"select * from underlying_data where ts > ";
    deleteAll += ninetySeventyTicks.ToString();

所以这不会删除任何东西。实际上,Cassandra只允许主键=上的INSELECT运算符。虽然您没有明确提及它,但我确信您看到的例外情况如下:

  

[无效查询] message =“分区键仅支持EQ和IN关系(除非您使用token()函数)”

如异常所示,除非使用>函数封装值,否则不能使用<token()。但是token()不会给你想要的,因为主键令牌不是有序的。

同样,如果您尝试使用除DELETE以外的任何内容的实际=,您将获得此信息:

  

[无效查询] message =“无效的运算符&lt; for PRIMARY KEY part id”

SELECT不同,DELETE不允许您使用token()函数使用大于/小于运算符。使用=运算符和主键删除的唯一替代方法是为DELETE提供多个主键的IN子句。

您还应该知道,定期删除数据会产生大量的墓碑,因此Cassandra通常不鼓励删除大量使用模式。有关更多信息,请查看CQL DELETE command上的文档。