Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Last Update:2016-12-09 17:41:07
- #include <rados/librados.hpp>
- #include <string>
- #include <list>
- int main(int argc, const char *argv[])
- {
- int ret = 0 ;
- // Get cluster handle and connect to cluster
- std::cout<<"ceph Cluster connect begin."<<std::endl;
- std::string cluster_name("ceph");
- std::string user_name("client.admin");
- librados::Rados cluster ;
- ret = cluster.init2(user_name.c_str(), cluster_name.c_str(), 0);
- if (ret < 0)
- {
- std::cerr << "Couldn't initialize the cluster handle! error " << ret << std::endl;
- ret = EXIT_FAILURE;
- return 1;
- } else {
- std::cout << "Created a cluster handle." << std::endl;
- }
- ret = cluster.conf_read_file("/etc/ceph/ceph.conf");
- if (ret < 0)
- {
- std::cerr << "Couldn't read the Ceph configuration file! error " << ret << std::endl;
- ret = EXIT_FAILURE;
- return 1;
- } else {
- std::cout << "Read the Ceph configuration file Succeed." << std::endl;
- }
- ret = cluster.connect();
- if (ret < 0) {
- std::cerr << "Couldn't connect to cluster! error " << ret << std::endl;
- ret = EXIT_FAILURE;
- return 1;
- }else{
- std::cout << "Connected to the cluster." << std::endl;
- }
- std::cout<<"ceph Cluster connect end."<<std::endl;
- // IO context poolname pool-1
- std::cout<<"ceph Cluster create io context for pool begin."<<std::endl;
- librados::IoCtx io_ctx ;
- std::string pool_name("pool-1");
- ret = cluster.ioctx_create(pool_name.c_str(), io_ctx);
- if (ret < 0)
- {
- std::cerr << "Couldn't set up ioctx! error " << ret << std::endl;
- exit(EXIT_FAILURE);
- } else {
- std::cout << "Created an ioctx for the pool." << std::endl;
- }
- std::cout<<"ceph Cluster create io context for pool end."<<std::endl;
- // Write an object synchronously
- std::cout<<"Write an object synchronously begin."<<std::endl;
- librados::bufferlist bl;
- std::string objectId("hw");
- std::string objectContent("Hello World!");
- bl.append(objectContent);
- ret = io_ctx.write_full("hw", bl);
- if (ret < 0) {
- std::cerr << "Couldn't write object! error " << ret << std::endl;
- exit(EXIT_FAILURE);
- } else {
- std::cout << "Wrote new object 'hw' " << std::endl;
- }
- std::cout<<"Write an object synchronously end."<<std::endl;
- // Add an xattr to the object.
- librados::bufferlist lang_bl;
- lang_bl.append("en_US");
- io_ctx.setxattr(objectId, "lang", lang_bl);
- // Read the object back asynchronously
- librados::bufferlist read_buf;
- int read_len = 4194304;
- //Create I/O Completion.
- librados::AioCompletion *read_completion = librados::Rados::aio_create_completion();
- //Send read request.
- io_ctx.aio_read(objectId, read_completion, &read_buf, read_len, 0 );
- // Wait for the request to complete, and print content
- read_completion->wait_for_complete();
- read_completion->get_return_value();
- std::cout<< "Object name: " << objectId << "\n"
- << "Content: " << read_buf.c_str() << std::endl ;
- // Read the xattr.
- librados::bufferlist lang_res;
- io_ctx.getxattr(objectId, "lang", lang_res);
- std::cout<< "Object xattr: " << lang_res.c_str() << std::endl ;
- // Print the list of pools
- std::list<std::string> pools ;
- cluster.pool_list(pools );
- std::cout << "List of pools from this cluster handle" << std::endl ;
- for (std::list<std::string>::iterator i = pools.begin(); i != pools.end(); ++i)
- std::cout << *i << std::endl;
- // Print the list of objects
- librados::ObjectIterator oit=io_ctx.objects_begin();
- librados::ObjectIterator oet=io_ctx.objects_end();
- std::cout<< "List of objects from this pool" << std::endl ;
- for(; oit!= oet; oit++ ) {
- std::cout << "\t" << oit->first << std::endl ;
- }
- // Remove the xattr
- io_ctx.rmxattr(objectId, "lang");
- // Remove the object.
- io_ctx.remove(objectId);
- // Cleanup
- io_ctx.close();
- cluster.shutdown();
- return 0 ;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement