 Hello and welcome to a session on import and export of data in Cassandra. This is Dr. Anitha Pooja, Professor of Computer Science and Engineering Department from Volchin Institute of Technology, Solabor. At the end of this session, learners will be able to import the data into table from a CSV file and export the data from table into CSV file. Now let's start with import of data. Data can be imported from a CSV file into an existing table using copy from command. Each line in the source file is imported as a row into the table. All the rows in the data set must contain the same number of fields and have values in the primary key fields. This process verifies the primary key and updates existing records. There can be various options that can be used along with copy from command. One of the important option is the header. If header is set to value false, then when you display the content of the table after importing the data from CSV file, you won't find the header row at all. If header is set to true, then the header row is seen in the table. If no columns are specified, then the fields are imported in the deterministic order. When column names are specified, fields are imported in the order in which the columns are specified. Missing and empty fields are set to null. The source cannot have more fields than the target file. Rather, it can have fever fields. Copy from can be used with tables having less than 2 million rows. If the table size is greater than 2 million rows, that means it is a large data set. And to import large data sets, copy from command cannot be used. Instead, Cassandra bulkloader has to be used. Now let's see the syntax of copy from command. Copy table name, that is, it is a table name to which you want to import the data. Followed by column list, that is, which column values you want to import. From file name, that is, from which file you want to import the data. This file name can be more than one also. Or instead of file name, you can also accept the data or import the data from the standard input device, that is, keyboard. So when you do that, you use std-in instead of file name. Followed by options, which are set to different values. We can set multiple options here. There are seven to eight options that can be used with copy from command. Note, copy from supports a list of one or more comma separated file names. That is, you can copy from more than one file name into the table. Now let's see the example to import data from source file, elearninglist.csv, which is on D drive, into the existing table, elearninglist, present in the students database. So assuming that students database has already been created and the table elearninglist has already been created, we just check for the existence of this table in the students database as step one. So I use the query, select star from elearninglist. So content of table is displayed. Now if I want to clear the content of the table completely before importing the new data, then I have to use truncate elearninglist. This command deletes all the rows from the table. If I don't want to delete all the earlier contents and just update the new data that has been imported, then don't use truncate command at all. So new data that is imported is just updated into the table. Now step two, check for the content of elearninglist.csv file. So first I will check the content of the source file. I will see whether the structure of this source file matches with the structure of the table. So I display the content of the source file and verify it. Step three, execute the command to import the data from the source file into the table. So copy elearninglist followed by column list from the source file elearninglist.csv. So then I will display the content of the table to confirm the import of data. So select star from elearninglist. So I find that all the three rows in the csv file have been imported into the table. Now let's see how to import the data from standard input device. So to import data from the standard input device into an existing table persons which is present in the student's database. So step one is ensure that table persons exist in the student's database using the command describe table persons. So this will be followed by the output which shows the structure of the table persons. Now I will use copy from command. So copy persons followed by the column list which has three columns ID, first name and last name from the standard input device. So let's see what is the input, output. So you find that the first line says that use backslash followed by a dot to end the input which means you start in giving the input three keyboard but as soon as you finish giving the input use backslash followed by dot. Now the next line starts accepting the first row that is one Samuel Jones second row is two that is an ID of the person Sachin first name and Tendulkar is the last name. Similarly third row is accepted and after that we type backslash followed by dot which means that is the end of the input through keyboard. Now to confirm whether data has been imported then I use select star from persons. Now pause the video for a while think and write. Copy from truncate the table before importing the new data is it true or false? Now copy from automatically does not truncate the content of the table before importing the new data. It is a user who has to use the command truncate and delete the content of the table before importing the new data. So answer of this is false. Now second question is when importing data using copy from the default case is that every row in the CSV input is expected to contain the same number of columns which is the same as the number of columns in the Cassandra table metadata. Yes we have seen this condition in the previous slide the content of the CSV file should match with the structure of the table to which you are going to import the data. So answer of this is true. So these are the answers of those questions. Now we will see how to export the data. Export of data means the data is copied from existing table into a CSV file. Now each row is written into a separate line in the CSV file with fields separated by delimiters. All fields are exported when no columns are specified. If you want to drop the columns then you have to specify a column list. Now let's see the syntax for exporting the data. So copy table name so this is the source from where you are going to copy this is followed by the column list means which columns you want to copy to file name which is a target file name. Here again you can use more than one target file names with options, multiple options if you are using then and will come and multiple options can be set to different values. Now let's see the example for exporting the data. Export the contents of the table or column family eLearningList in the students database to a CSV file eLearningList.csv on D drive. So step one, check the records of the table eLearningList present in the student database. So I use select star from eLearningList content of the table is displayed. So table consists of three rows. Now execute the command copy to that is copy eLearningList followed by a column list to a target file eLearningList.csv. Now to confirm whether data has been exported to eLearningList.csv file in D drive I display the content of the CSV file. So content shows here that all three rows have been copied. Now if I want to export the data to the standard output device then export the contents of the table eLearningList present in students database to the standard output device that is STD out. So step one is first I check the records of the table eLearningList present in the students database. So I use a query select star from eLearningList. The content of the table is displayed, table consists of three rows. Now step two is execute the below command. Say copy eLearningList followed by column list to standard output device so STD out. So immediately the output is displayed on console that is it shows three rows all these three rows have been copied to the standard output device. These are some of the references thank you.