【问题标题】:exportdata from cassandra into csv files从 cassandra 导出数据到 csv 文件
【发布时间】:2017-05-18 13:26:28
【问题描述】:

大家好,我想在 cassandra 的指定表中获取数据并将其保存到 csv 文件中。我做了什么

//using c# api
            //  Cluster cluster = Cluster.Builder().AddContactPoint("192.168.0.214").Build();
              Cluster cluster = Cluster.Builder().AddContactPoint("127.0.0.1").Build();
             List<string> lstOfKeyspaces =cluster.Metadata.GetKeyspaces().ToList<string>();
             ISession session = cluster.Connect("integrationobjects");
             //get tables in the keysapce
             List<string> lstString = cluster.Metadata.GetTables("integrationobjects").ToList<string>();
            Console.WriteLine("Connection succeeded");
         //   Console.ReadLine();
            RowSet resultRequest = session.Execute("select json * from emp");

为了将结果保存到 csv 文件中,我们将不胜感激。 提前谢谢你

【问题讨论】:

    标签: c# cassandra export-to-csv


    【解决方案1】:

    使用 Cqlsh COPY TO 命令

    登录到 Cqlsh 然后使用以下命令:

    COPY emp TO 'exp.csv';
    

    【讨论】:

    • 这是一个cqlsh命令,不能在Cassandra Driver中使用
    • 有没有办法使用 cassandra 驱动程序获得相同的结果?谢谢
    【解决方案2】:

    这就像遍历查询返回的 RowSet 一样简单。这样的事情就足够了:

    // Open the file for output...
    ...
    
    // Loop over all the rows
    IEnumerable<Row> rowEnumerator = resultRequest.GetEnumerator();
    while (rowEnumerator.MoveNext()) {
        // Get the row
        Row r = rowEnumerator.Current;
    
        // Prepare a new line
        List<string> values = new List<string>;
    
        // Loop over all the columns of the row
        IEnumerator<Object> colEnumerator = r.GetEnumerator();
        while (colEnumerator.MoveNext()) {
            values.Add(...); // Stack the strings here 
        }
        // Create the row
        string line = String.Join(",", values.ToArray());
    
        // Output line to file
        // ...
    }
    // Close the file
    ...
    

    【讨论】:

    • 请问我应该在这里放什么 values.Add(...);在添加,谢谢和抱歉
    • 你能帮我完成你给的代码吗,我的堆栈非常感谢你
    猜你喜欢
    • 2015-09-01
    • 2014-09-13
    • 2016-06-21
    • 2014-01-09
    • 1970-01-01
    • 1970-01-01
    • 2019-10-03
    • 1970-01-01
    • 2019-01-11
    相关资源
    最近更新 更多