Maher HTB
Maher HTB

Reputation: 737

exportdata from cassandra into csv files

Hi guys i would like to get data in a specified table in cassandra and save it into csv file .Here what i did

//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");

Any help would be appreciated in order to save results into csv file. Thank you in advance

Upvotes: 1

Views: 979

Answers (2)

xmas79
xmas79

Reputation: 5180

This is as simple as traversing the RowSet your query is returning. Something like that should be enough:

// 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
...

Upvotes: 0

Ashraful Islam
Ashraful Islam

Reputation: 12830

Use Cqlsh COPY TO Command

Login To Cqlsh then use the below command :

COPY emp TO 'exp.csv';

Upvotes: 1

Related Questions