Advertisement
Guest User

Untitled

a guest
Dec 9th, 2016
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.74 KB | None | 0 0
  1. // Last Update:2016-12-09 17:41:07
  2.  
  3. #include <rados/librados.hpp>
  4. #include <string>
  5. #include <list>
  6.  
  7. int main(int argc, const char *argv[])
  8. {
  9. int ret = 0 ;
  10. // Get cluster handle and connect to cluster
  11. std::cout<<"ceph Cluster connect begin."<<std::endl;
  12. std::string cluster_name("ceph");
  13. std::string user_name("client.admin");
  14. librados::Rados cluster ;
  15.  
  16. ret = cluster.init2(user_name.c_str(), cluster_name.c_str(), 0);
  17. if (ret < 0)
  18. {
  19. std::cerr << "Couldn't initialize the cluster handle! error " << ret << std::endl;
  20. ret = EXIT_FAILURE;
  21. return 1;
  22. } else {
  23. std::cout << "Created a cluster handle." << std::endl;
  24. }
  25.  
  26. ret = cluster.conf_read_file("/etc/ceph/ceph.conf");
  27. if (ret < 0)
  28. {
  29. std::cerr << "Couldn't read the Ceph configuration file! error " << ret << std::endl;
  30. ret = EXIT_FAILURE;
  31. return 1;
  32. } else {
  33. std::cout << "Read the Ceph configuration file Succeed." << std::endl;
  34. }
  35.  
  36. ret = cluster.connect();
  37. if (ret < 0) {
  38. std::cerr << "Couldn't connect to cluster! error " << ret << std::endl;
  39. ret = EXIT_FAILURE;
  40. return 1;
  41. }else{
  42. std::cout << "Connected to the cluster." << std::endl;
  43. }
  44.  
  45. std::cout<<"ceph Cluster connect end."<<std::endl;
  46. // IO context poolname pool-1
  47. std::cout<<"ceph Cluster create io context for pool begin."<<std::endl;
  48. librados::IoCtx io_ctx ;
  49. std::string pool_name("pool-1");
  50.  
  51. ret = cluster.ioctx_create(pool_name.c_str(), io_ctx);
  52. if (ret < 0)
  53. {
  54. std::cerr << "Couldn't set up ioctx! error " << ret << std::endl;
  55. exit(EXIT_FAILURE);
  56. } else {
  57. std::cout << "Created an ioctx for the pool." << std::endl;
  58. }
  59. std::cout<<"ceph Cluster create io context for pool end."<<std::endl;
  60. // Write an object synchronously
  61. std::cout<<"Write an object synchronously begin."<<std::endl;
  62.  
  63. librados::bufferlist bl;
  64. std::string objectId("hw");
  65. std::string objectContent("Hello World!");
  66. bl.append(objectContent);
  67.  
  68. ret = io_ctx.write_full("hw", bl);
  69. if (ret < 0) {
  70. std::cerr << "Couldn't write object! error " << ret << std::endl;
  71. exit(EXIT_FAILURE);
  72. } else {
  73. std::cout << "Wrote new object 'hw' " << std::endl;
  74. }
  75.  
  76. std::cout<<"Write an object synchronously end."<<std::endl;
  77. // Add an xattr to the object.
  78. librados::bufferlist lang_bl;
  79. lang_bl.append("en_US");
  80. io_ctx.setxattr(objectId, "lang", lang_bl);
  81. // Read the object back asynchronously
  82. librados::bufferlist read_buf;
  83. int read_len = 4194304;
  84.  
  85. //Create I/O Completion.
  86. librados::AioCompletion *read_completion = librados::Rados::aio_create_completion();
  87. //Send read request.
  88. io_ctx.aio_read(objectId, read_completion, &read_buf, read_len, 0 );
  89. // Wait for the request to complete, and print content
  90. read_completion->wait_for_complete();
  91. read_completion->get_return_value();
  92. std::cout<< "Object name: " << objectId << "\n"
  93. << "Content: " << read_buf.c_str() << std::endl ;
  94. // Read the xattr.
  95. librados::bufferlist lang_res;
  96. io_ctx.getxattr(objectId, "lang", lang_res);
  97. std::cout<< "Object xattr: " << lang_res.c_str() << std::endl ;
  98. // Print the list of pools
  99. std::list<std::string> pools ;
  100. cluster.pool_list(pools );
  101. std::cout << "List of pools from this cluster handle" << std::endl ;
  102. for (std::list<std::string>::iterator i = pools.begin(); i != pools.end(); ++i)
  103. std::cout << *i << std::endl;
  104. // Print the list of objects
  105. librados::ObjectIterator oit=io_ctx.objects_begin();
  106. librados::ObjectIterator oet=io_ctx.objects_end();
  107. std::cout<< "List of objects from this pool" << std::endl ;
  108. for(; oit!= oet; oit++ ) {
  109. std::cout << "\t" << oit->first << std::endl ;
  110. }
  111. // Remove the xattr
  112. io_ctx.rmxattr(objectId, "lang");
  113. // Remove the object.
  114. io_ctx.remove(objectId);
  115. // Cleanup
  116. io_ctx.close();
  117. cluster.shutdown();
  118. return 0 ;
  119. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement