In the example given below I have used "opencsv" which is a very simple csv parser library for Java to read the data from the csv file. Opencsv is available under a commercial-friendly Apache 2.0 license.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import au.com.bytecode.opencsv.CSVReader; | |
import me.prettyprint.cassandra.serializers.StringSerializer; | |
import me.prettyprint.cassandra.service.CassandraHostConfigurator; | |
import me.prettyprint.hector.api.Cluster; | |
import me.prettyprint.hector.api.Keyspace; | |
import me.prettyprint.hector.api.ddl.ColumnFamilyDefinition; | |
import me.prettyprint.hector.api.ddl.ComparatorType; | |
import me.prettyprint.hector.api.factory.HFactory; | |
import me.prettyprint.hector.api.mutation.Mutator; | |
import java.io.FileReader; | |
import java.io.IOException; | |
import java.util.HashMap; | |
import java.util.Map; | |
public class DataPopulator { | |
public static final String USERNAME_KEY = "username"; | |
public static final String PASSWORD_KEY = "password"; | |
public static void main(String[] args) throws IOException { | |
String keyspaceName = "Test_KS"; | |
CSVReader reader = new CSVReader(new FileReader("TestList.csv")); | |
String [] nextLine; | |
Map<String, String> credentials = new HashMap<String, String>(); | |
credentials.put(USERNAME_KEY, "cassandra"); | |
credentials.put(PASSWORD_KEY, "cassandra"); | |
Cluster cluster = HFactory.getOrCreateCluster("MonarchCluster", | |
new CassandraHostConfigurator("localhost:9160"), credentials); | |
Keyspace keySpace = HFactory.createKeyspace(keyspaceName, cluster); | |
ColumnFamilyDefinition cf = HFactory.createColumnFamilyDefinition(keyspaceName, "Test_List_CS", ComparatorType.UTF8TYPE); | |
Mutator<String> mutator = HFactory.createMutator(keySpace, StringSerializer.get()); | |
while ((nextLine = reader.readNext()) != null) { | |
mutator.insert(nextLine[0], cf.getName(), HFactory.createStringColumn("column1", nextLine[1])); | |
mutator.insert(nextLine[0], cf.getName(), HFactory.createStringColumn("column2", nextLine[2])); | |
} | |
} | |
} |