آموزش اتصال به casandra با java

d986d8add988d987 d8a7d8aad8b5d8a7d984 d8a8d987 daa9d8a7d8b3d8a7d986d8afd8b1d8a7 d8a8d8a7 d8acd8a7d988d8a7 d982d8b3d985d8aa d8afd988d985

import com.datastax.driver.core.*;

/**

* this class is use to handel all interaction between application and data base

*/

public class CassandraDAO {

private static Cluster cluster;

private static Session session;

private final static String KEYSPACE_NAME = “example_keyspace”;

private final static String REPLICATION_STRATEGY = “SimpleStrategy”;

private final static int REPLICATION_FACTOR = 1;

private final static String TABLE_NAME = “Employe_table”;

private static void openCassandra() throws Exception {

// Setup a cluster to your local instance of Cassandra

cluster = Cluster.builder()

.addContactPoint(“192.168.174.141”)

.withPort(9042)

.build();

// Create a session to communicate with Cassandra

session = cluster.connect();

System.out.println(“Cassandra is Connecting…”);

}

private static void closeCassandra() throws Exception {

session.close();

cluster.close();

System.out.println(“Cassandra is closing… \n”);

}

public static void createKeyspace () throws Exception {

openCassandra();

// Create a new Keyspace (database) in Cassandra

String createkeyspace1 = String.format(

“CREATE KEYSPACE IF NOT EXISTS %s WITH replication = “ +

“{‘class’:’%s’,’replication_factor’:%s};”,

KEYSPACE_NAME,

REPLICATION_STRATEGY,

REPLICATION_FACTOR

);

session.execute(createkeyspace1);

System.out.println(“create Keyspace…”);

closeCassandra();

}

public static void createTable () throws Exception {

openCassandra();

// Create a new table in our Keyspace

String createTable = String.format(

“CREATE TABLE IF NOT EXISTS %s.%s “ + “” +

“(emp_id int, emp_city text, emp_name text, emp_phone text, emp_sal int, “ +

“PRIMARY KEY ((emp_id , emp_city), emp_name)) “ +

“WITH CLUSTERING ORDER BY (emp_name ASC);”,

KEYSPACE_NAME,

TABLE_NAME

);

session.execute(createTable);

System.out.println(“create column family…”);

closeCassandra();

}

public static void insertEmploye(EmployePOJO employe) throws Exception {

openCassandra();

// Create an insert statement to add a new item to our table

PreparedStatement insertPrepared = session.prepare(String.format(

“INSERT INTO %s.%s (emp_id, emp_city, emp_name, emp_phone, emp_sal) values (?, ?, ?, ?, ?)”,

KEYSPACE_NAME,

TABLE_NAME

));

int id =employe.getEmploye_id();

String city= employe.getEmploye_city();

String name= employe.getEmploye_name();

String phone=employe.getEmploye_phone();

int sal=employe.getEmploye_sal();

// Bind the data to the insert statement and execute it

BoundStatement insertBound = insertPrepared.bind(id,city,name,phone,sal);

session.execute(insertBound);

System.out.println(“insert data…”);

closeCassandra();

}

public static void getEmploye(int id, String city) throws Exception{

openCassandra();

// Create a select statement to retrieve the item we just inserted

PreparedStatement selectPrepared = session.prepare(String.format(

“SELECT emp_id, emp_city, emp_name FROM %s.%s WHERE emp_id = ? and emp_city = ?”,

KEYSPACE_NAME,

TABLE_NAME));

// Bind the id to the select statement and execute it

// UUID id = UUID.fromString(“1e4d26ed-922a-4bd2-85cb-6357b202eda8”);

BoundStatement selectBound = selectPrepared.bind(id,city);

ResultSet resultSet = session.execute(selectBound);

// Print the retrieved data

resultSet.forEach(row -> System.out.println(

String.format(“id: %s, name: %s”,

row.getInt(“emp_id”),

row.getString(“emp_name”))));

closeCassandra();

}

public static void removeEmploye(EmployePOJO employe) throws Exception {

}

public static void updateEmploye(EmployePOJO employe) throws Exception{

}

}

مدیریت سرور پشتیبانی و مشاوره – ثبت دامنه

نوشته های مشابه