Introduction to librados

The Ceph Storage Cluster provides the basic storage service that allowsCeph to uniquely deliver object, block, and file storage in oneunified system. However, you are not limited to using the RESTful, block, orPOSIX interfaces. Based upon RADOS, the librados API enables you to create your own interface to theCeph Storage Cluster.

The librados API enables you to interact with the two types of daemons inthe Ceph Storage Cluster:

  • The Ceph Monitor, which maintains a master copy of the cluster map.

  • The Ceph OSD Daemon (OSD), which stores data as objects on a storage node.

Introduction to librados - 图1

This guide provides a high-level introduction to using librados.Refer to Architecture for additional details of the CephStorage Cluster. To use the API, you need a running Ceph Storage Cluster.See Installation (Quick) for details.

Step 1: Getting librados

Your client application must bind with librados to connect to the CephStorage Cluster. You must install librados and any required packages towrite applications that use librados. The librados API is written inC++, with additional bindings for C, Python, Java and PHP.

Getting librados for C/C++

To install librados development support files for C/C++ on Debian/Ubuntudistributions, execute the following:

  1. sudo apt-get install librados-dev

To install librados development support files for C/C++ on RHEL/CentOSdistributions, execute the following:

  1. sudo yum install librados2-devel

Once you install librados for developers, you can find the requiredheaders for C/C++ under /usr/include/rados.

  1. ls /usr/include/rados

Getting librados for Python

The rados module provides librados support to Pythonapplications. The librados-dev package for Debian/Ubuntuand the librados2-devel package for RHEL/CentOS will install thepython-rados package for you. You may install python-radosdirectly too.

To install librados development support files for Python on Debian/Ubuntudistributions, execute the following:

  1. sudo apt-get install python-rados

To install librados development support files for Python on RHEL/CentOSdistributions, execute the following:

  1. sudo yum install python-rados

You can find the module under /usr/share/pyshared on Debian systems,or under /usr/lib/python*/site-packages on CentOS/RHEL systems.

Getting librados for Java

To install librados for Java, you need to execute the following procedure:

  • Install jna.jar. For Debian/Ubuntu, execute:
  1. sudo apt-get install libjna-java

For CentOS/RHEL, execute:

  1. sudo yum install jna

The JAR files are located in /usr/share/java.

  • Clone the rados-java repository:
  1. git clone --recursive https://github.com/ceph/rados-java.git
  • Build the rados-java repository:
  1. cd rados-java
  2. ant

The JAR file is located under rados-java/target.

  • Copy the JAR for RADOS to a common location (e.g., /usr/share/java) andensure that it and the JNA JAR are in your JVM’s classpath. For example:
  1. sudo cp target/rados-0.1.3.jar /usr/share/java/rados-0.1.3.jar
  2. sudo ln -s /usr/share/java/jna-3.2.7.jar /usr/lib/jvm/default-java/jre/lib/ext/jna-3.2.7.jar
  3. sudo ln -s /usr/share/java/rados-0.1.3.jar /usr/lib/jvm/default-java/jre/lib/ext/rados-0.1.3.jar

To build the documentation, execute the following:

  1. ant docs

Getting librados for PHP

To install the librados extension for PHP, you need to execute the following procedure:

  • Install php-dev. For Debian/Ubuntu, execute:
  1. sudo apt-get install php5-dev build-essential

For CentOS/RHEL, execute:

  1. sudo yum install php-devel
  • Clone the phprados repository:
  1. git clone https://github.com/ceph/phprados.git
  • Build phprados:
  1. cd phprados
  2. phpize
  3. ./configure
  4. make
  5. sudo make install
  • Enable phprados in php.ini by adding:
  1. extension=rados.so

Step 2: Configuring a Cluster Handle

A Ceph Client, via librados, interacts directly with OSDs to storeand retrieve data. To interact with OSDs, the client app must invokelibrados and connect to a Ceph Monitor. Once connected, libradosretrieves the Cluster Map from the Ceph Monitor. When the client appwants to read or write data, it creates an I/O context and binds to apool. The pool has an associated CRUSH Rule that defines how itwill place data in the storage cluster. Via the I/O context, the clientprovides the object name to librados, which takes the object nameand the cluster map (i.e., the topology of the cluster) and computes theplacement group and OSD for locating the data. Then the client applicationcan read or write data. The client app doesn’t need to learn about the topologyof the cluster directly.

Introduction to librados - 图2

The Ceph Storage Cluster handle encapsulates the client configuration, including:

  • The user ID for rados_create() or user name for rados_create2()(preferred).

  • The cephx authentication key

  • The monitor ID and IP address

  • Logging levels

  • Debugging levels

Thus, the first steps in using the cluster from your app are to 1) createa cluster handle that your app will use to connect to the storage cluster,and then 2) use that handle to connect. To connect to the cluster, theapp must supply a monitor address, a username and an authentication key(cephx is enabled by default).

Tip

Talking to different Ceph Storage Clusters – or to the same clusterwith different users – requires different cluster handles.

RADOS provides a number of ways for you to set the required values. Forthe monitor and encryption key settings, an easy way to handle them is to ensurethat your Ceph configuration file contains a keyring path to a keyring fileand at least one monitor address (e.g., mon host). For example:

  1. [global]
  2. mon host = 192.168.1.1
  3. keyring = /etc/ceph/ceph.client.admin.keyring

Once you create the handle, you can read a Ceph configuration file to configurethe handle. You can also pass arguments to your app and parse them with thefunction for parsing command line arguments (e.g., rados_conf_parse_argv()),or parse Ceph environment variables (e.g., rados_conf_parse_env()). Somewrappers may not implement convenience methods, so you may need to implementthese capabilities. The following diagram provides a high-level flow for theinitial connection.

Introduction to librados - 图3

Once connected, your app can invoke functions that affect the whole clusterwith only the cluster handle. For example, once you have a clusterhandle, you can:

  • Get cluster statistics

  • Use Pool Operation (exists, create, list, delete)

  • Get and set the configuration

One of the powerful features of Ceph is the ability to bind to different pools.Each pool may have a different number of placement groups, object replicas andreplication strategies. For example, a pool could be set up as a “hot” pool thatuses SSDs for frequently used objects or a “cold” pool that uses erasure coding.

The main difference in the various librados bindings is between C andthe object-oriented bindings for C++, Java and Python. The object-orientedbindings use objects to represent cluster handles, IO Contexts, iterators,exceptions, etc.

C Example

For C, creating a simple cluster handle using the admin user, configuringit and connecting to the cluster might look something like this:

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <rados/librados.h>
  5.  
  6. int main (int argc, const char **argv)
  7. {
  8.  
  9. /* Declare the cluster handle and required arguments. */
  10. rados_t cluster;
  11. char cluster_name[] = "ceph";
  12. char user_name[] = "client.admin";
  13. uint64_t flags = 0;
  14.  
  15. /* Initialize the cluster handle with the "ceph" cluster name and the "client.admin" user */
  16. int err;
  17. err = rados_create2(&cluster, cluster_name, user_name, flags);
  18.  
  19. if (err < 0) {
  20. fprintf(stderr, "%s: Couldn't create the cluster handle! %s\n", argv[0], strerror(-err));
  21. exit(EXIT_FAILURE);
  22. } else {
  23. printf("\nCreated a cluster handle.\n");
  24. }
  25.  
  26.  
  27. /* Read a Ceph configuration file to configure the cluster handle. */
  28. err = rados_conf_read_file(cluster, "/etc/ceph/ceph.conf");
  29. if (err < 0) {
  30. fprintf(stderr, "%s: cannot read config file: %s\n", argv[0], strerror(-err));
  31. exit(EXIT_FAILURE);
  32. } else {
  33. printf("\nRead the config file.\n");
  34. }
  35.  
  36. /* Read command line arguments */
  37. err = rados_conf_parse_argv(cluster, argc, argv);
  38. if (err < 0) {
  39. fprintf(stderr, "%s: cannot parse command line arguments: %s\n", argv[0], strerror(-err));
  40. exit(EXIT_FAILURE);
  41. } else {
  42. printf("\nRead the command line arguments.\n");
  43. }
  44.  
  45. /* Connect to the cluster */
  46. err = rados_connect(cluster);
  47. if (err < 0) {
  48. fprintf(stderr, "%s: cannot connect to cluster: %s\n", argv[0], strerror(-err));
  49. exit(EXIT_FAILURE);
  50. } else {
  51. printf("\nConnected to the cluster.\n");
  52. }
  53.  
  54. }

Compile your client and link to librados using -lrados. For example:

  1. gcc ceph-client.c -lrados -o ceph-client

C++ Example

The Ceph project provides a C++ example in the ceph/examples/libradosdirectory. For C++, a simple cluster handle using the admin user requiresyou to initialize a librados::Rados cluster handle object:

  1. #include <iostream>
  2. #include <string>
  3. #include <rados/librados.hpp>
  4.  
  5. int main(int argc, const char **argv)
  6. {
  7.  
  8. int ret = 0;
  9.  
  10. /* Declare the cluster handle and required variables. */
  11. librados::Rados cluster;
  12. char cluster_name[] = "ceph";
  13. char user_name[] = "client.admin";
  14. uint64_t flags = 0;
  15.  
  16. /* Initialize the cluster handle with the "ceph" cluster name and "client.admin" user */
  17. {
  18. ret = cluster.init2(user_name, cluster_name, flags);
  19. if (ret < 0) {
  20. std::cerr << "Couldn't initialize the cluster handle! error " << ret << std::endl;
  21. return EXIT_FAILURE;
  22. } else {
  23. std::cout << "Created a cluster handle." << std::endl;
  24. }
  25. }
  26.  
  27. /* Read a Ceph configuration file to configure the cluster handle. */
  28. {
  29. ret = cluster.conf_read_file("/etc/ceph/ceph.conf");
  30. if (ret < 0) {
  31. std::cerr << "Couldn't read the Ceph configuration file! error " << ret << std::endl;
  32. return EXIT_FAILURE;
  33. } else {
  34. std::cout << "Read the Ceph configuration file." << std::endl;
  35. }
  36. }
  37.  
  38. /* Read command line arguments */
  39. {
  40. ret = cluster.conf_parse_argv(argc, argv);
  41. if (ret < 0) {
  42. std::cerr << "Couldn't parse command line options! error " << ret << std::endl;
  43. return EXIT_FAILURE;
  44. } else {
  45. std::cout << "Parsed command line options." << std::endl;
  46. }
  47. }
  48.  
  49. /* Connect to the cluster */
  50. {
  51. ret = cluster.connect();
  52. if (ret < 0) {
  53. std::cerr << "Couldn't connect to cluster! error " << ret << std::endl;
  54. return EXIT_FAILURE;
  55. } else {
  56. std::cout << "Connected to the cluster." << std::endl;
  57. }
  58. }
  59.  
  60. return 0;
  61. }

Compile the source; then, link librados using -lrados.For example:

  1. g++ -g -c ceph-client.cc -o ceph-client.o
  2. g++ -g ceph-client.o -lrados -o ceph-client

Python Example

Python uses the admin id and the ceph cluster name by default, andwill read the standard ceph.conf file if the conffile parameter isset to the empty string. The Python binding converts C++ errorsinto exceptions.

  1. import rados
  2.  
  3. try:
  4. cluster = rados.Rados(conffile='')
  5. except TypeError as e:
  6. print 'Argument validation error: ', e
  7. raise e
  8.  
  9. print "Created cluster handle."
  10.  
  11. try:
  12. cluster.connect()
  13. except Exception as e:
  14. print "connection error: ", e
  15. raise e
  16. finally:
  17. print "Connected to the cluster."

Execute the example to verify that it connects to your cluster.

  1. python ceph-client.py

Java Example

Java requires you to specify the user ID (admin) or user name(client.admin), and uses the ceph cluster name by default . The Javabinding converts C++-based errors into exceptions.

  1. import com.ceph.rados.Rados;
  2. import com.ceph.rados.RadosException;
  3.  
  4. import java.io.File;
  5.  
  6. public class CephClient {
  7. public static void main (String args[]){
  8.  
  9. try {
  10. Rados cluster = new Rados("admin");
  11. System.out.println("Created cluster handle.");
  12.  
  13. File f = new File("/etc/ceph/ceph.conf");
  14. cluster.confReadFile(f);
  15. System.out.println("Read the configuration file.");
  16.  
  17. cluster.connect();
  18. System.out.println("Connected to the cluster.");
  19.  
  20. } catch (RadosException e) {
  21. System.out.println(e.getMessage() + ": " + e.getReturnValue());
  22. }
  23. }
  24. }

Compile the source; then, run it. If you have copied the JAR to/usr/share/java and sym linked from your ext directory, you won’t needto specify the classpath. For example:

  1. javac CephClient.java
  2. java CephClient

PHP Example

With the RADOS extension enabled in PHP you can start creating a new cluster handle very easily:

  1. <?php
  2.  
  3. $r = rados_create();
  4. rados_conf_read_file($r, '/etc/ceph/ceph.conf');
  5. if (!rados_connect($r)) {
  6. echo "Failed to connect to Ceph cluster";
  7. } else {
  8. echo "Successfully connected to Ceph cluster";
  9. }

Save this as rados.php and run the code:

  1. php rados.php

Step 3: Creating an I/O Context

Once your app has a cluster handle and a connection to a Ceph Storage Cluster,you may create an I/O Context and begin reading and writing data. An I/O Contextbinds the connection to a specific pool. The user must have appropriateCAPS permissions to access the specified pool. For example, a user with readaccess but not write access will only be able to read data. I/O Contextfunctionality includes:

  • Write/read data and extended attributes

  • List and iterate over objects and extended attributes

  • Snapshot pools, list snapshots, etc.

Introduction to librados - 图4

RADOS enables you to interact both synchronously and asynchronously. Once yourapp has an I/O Context, read/write operations only require you to know theobject/xattr name. The CRUSH algorithm encapsulated in librados uses thecluster map to identify the appropriate OSD. OSD daemons handle the replication,as described in Smart Daemons Enable Hyperscale. The librados library alsomaps objects to placement groups, as described in Calculating PG IDs.

The following examples use the default data pool. However, you may alsouse the API to list pools, ensure they exist, or create and delete pools. Forthe write operations, the examples illustrate how to use synchronous mode. Forthe read operations, the examples illustrate how to use asynchronous mode.

Important

Use caution when deleting pools with this API. If you deletea pool, the pool and ALL DATA in the pool will be lost.

C Example

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <rados/librados.h>
  5.  
  6. int main (int argc, const char **argv)
  7. {
  8. /*
  9. * Continued from previous C example, where cluster handle and
  10. * connection are established. First declare an I/O Context.
  11. */
  12.  
  13. rados_ioctx_t io;
  14. char *poolname = "data";
  15.  
  16. err = rados_ioctx_create(cluster, poolname, &io);
  17. if (err < 0) {
  18. fprintf(stderr, "%s: cannot open rados pool %s: %s\n", argv[0], poolname, strerror(-err));
  19. rados_shutdown(cluster);
  20. exit(EXIT_FAILURE);
  21. } else {
  22. printf("\nCreated I/O context.\n");
  23. }
  24.  
  25. /* Write data to the cluster synchronously. */
  26. err = rados_write(io, "hw", "Hello World!", 12, 0);
  27. if (err < 0) {
  28. fprintf(stderr, "%s: Cannot write object \"hw\" to pool %s: %s\n", argv[0], poolname, strerror(-err));
  29. rados_ioctx_destroy(io);
  30. rados_shutdown(cluster);
  31. exit(1);
  32. } else {
  33. printf("\nWrote \"Hello World\" to object \"hw\".\n");
  34. }
  35.  
  36. char xattr[] = "en_US";
  37. err = rados_setxattr(io, "hw", "lang", xattr, 5);
  38. if (err < 0) {
  39. fprintf(stderr, "%s: Cannot write xattr to pool %s: %s\n", argv[0], poolname, strerror(-err));
  40. rados_ioctx_destroy(io);
  41. rados_shutdown(cluster);
  42. exit(1);
  43. } else {
  44. printf("\nWrote \"en_US\" to xattr \"lang\" for object \"hw\".\n");
  45. }
  46.  
  47. /*
  48. * Read data from the cluster asynchronously.
  49. * First, set up asynchronous I/O completion.
  50. */
  51. rados_completion_t comp;
  52. err = rados_aio_create_completion(NULL, NULL, NULL, &comp);
  53. if (err < 0) {
  54. fprintf(stderr, "%s: Could not create aio completion: %s\n", argv[0], strerror(-err));
  55. rados_ioctx_destroy(io);
  56. rados_shutdown(cluster);
  57. exit(1);
  58. } else {
  59. printf("\nCreated AIO completion.\n");
  60. }
  61.  
  62. /* Next, read data using rados_aio_read. */
  63. char read_res[100];
  64. err = rados_aio_read(io, "hw", comp, read_res, 12, 0);
  65. if (err < 0) {
  66. fprintf(stderr, "%s: Cannot read object. %s %s\n", argv[0], poolname, strerror(-err));
  67. rados_ioctx_destroy(io);
  68. rados_shutdown(cluster);
  69. exit(1);
  70. } else {
  71. printf("\nRead object \"hw\". The contents are:\n %s \n", read_res);
  72. }
  73.  
  74. /* Wait for the operation to complete */
  75. rados_aio_wait_for_complete(comp);
  76.  
  77. /* Release the asynchronous I/O complete handle to avoid memory leaks. */
  78. rados_aio_release(comp);
  79.  
  80.  
  81. char xattr_res[100];
  82. err = rados_getxattr(io, "hw", "lang", xattr_res, 5);
  83. if (err < 0) {
  84. fprintf(stderr, "%s: Cannot read xattr. %s %s\n", argv[0], poolname, strerror(-err));
  85. rados_ioctx_destroy(io);
  86. rados_shutdown(cluster);
  87. exit(1);
  88. } else {
  89. printf("\nRead xattr \"lang\" for object \"hw\". The contents are:\n %s \n", xattr_res);
  90. }
  91.  
  92. err = rados_rmxattr(io, "hw", "lang");
  93. if (err < 0) {
  94. fprintf(stderr, "%s: Cannot remove xattr. %s %s\n", argv[0], poolname, strerror(-err));
  95. rados_ioctx_destroy(io);
  96. rados_shutdown(cluster);
  97. exit(1);
  98. } else {
  99. printf("\nRemoved xattr \"lang\" for object \"hw\".\n");
  100. }
  101.  
  102. err = rados_remove(io, "hw");
  103. if (err < 0) {
  104. fprintf(stderr, "%s: Cannot remove object. %s %s\n", argv[0], poolname, strerror(-err));
  105. rados_ioctx_destroy(io);
  106. rados_shutdown(cluster);
  107. exit(1);
  108. } else {
  109. printf("\nRemoved object \"hw\".\n");
  110. }
  111.  
  112. }

C++ Example

  1. #include <iostream>
  2. #include <string>
  3. #include <rados/librados.hpp>
  4.  
  5. int main(int argc, const char **argv)
  6. {
  7.  
  8. /* Continued from previous C++ example, where cluster handle and
  9. * connection are established. First declare an I/O Context.
  10. */
  11.  
  12. librados::IoCtx io_ctx;
  13. const char *pool_name = "data";
  14.  
  15. {
  16. ret = cluster.ioctx_create(pool_name, io_ctx);
  17. if (ret < 0) {
  18. std::cerr << "Couldn't set up ioctx! error " << ret << std::endl;
  19. exit(EXIT_FAILURE);
  20. } else {
  21. std::cout << "Created an ioctx for the pool." << std::endl;
  22. }
  23. }
  24.  
  25.  
  26. /* Write an object synchronously. */
  27. {
  28. librados::bufferlist bl;
  29. bl.append("Hello World!");
  30. ret = io_ctx.write_full("hw", bl);
  31. if (ret < 0) {
  32. std::cerr << "Couldn't write object! error " << ret << std::endl;
  33. exit(EXIT_FAILURE);
  34. } else {
  35. std::cout << "Wrote new object 'hw' " << std::endl;
  36. }
  37. }
  38.  
  39.  
  40. /*
  41. * Add an xattr to the object.
  42. */
  43. {
  44. librados::bufferlist lang_bl;
  45. lang_bl.append("en_US");
  46. ret = io_ctx.setxattr("hw", "lang", lang_bl);
  47. if (ret < 0) {
  48. std::cerr << "failed to set xattr version entry! error "
  49. << ret << std::endl;
  50. exit(EXIT_FAILURE);
  51. } else {
  52. std::cout << "Set the xattr 'lang' on our object!" << std::endl;
  53. }
  54. }
  55.  
  56.  
  57. /*
  58. * Read the object back asynchronously.
  59. */
  60. {
  61. librados::bufferlist read_buf;
  62. int read_len = 4194304;
  63.  
  64. //Create I/O Completion.
  65. librados::AioCompletion *read_completion = librados::Rados::aio_create_completion();
  66.  
  67. //Send read request.
  68. ret = io_ctx.aio_read("hw", read_completion, &read_buf, read_len, 0);
  69. if (ret < 0) {
  70. std::cerr << "Couldn't start read object! error " << ret << std::endl;
  71. exit(EXIT_FAILURE);
  72. }
  73.  
  74. // Wait for the request to complete, and check that it succeeded.
  75. read_completion->wait_for_complete();
  76. ret = read_completion->get_return_value();
  77. if (ret < 0) {
  78. std::cerr << "Couldn't read object! error " << ret << std::endl;
  79. exit(EXIT_FAILURE);
  80. } else {
  81. std::cout << "Read object hw asynchronously with contents.\n"
  82. << read_buf.c_str() << std::endl;
  83. }
  84. }
  85.  
  86.  
  87. /*
  88. * Read the xattr.
  89. */
  90. {
  91. librados::bufferlist lang_res;
  92. ret = io_ctx.getxattr("hw", "lang", lang_res);
  93. if (ret < 0) {
  94. std::cerr << "failed to get xattr version entry! error "
  95. << ret << std::endl;
  96. exit(EXIT_FAILURE);
  97. } else {
  98. std::cout << "Got the xattr 'lang' from object hw!"
  99. << lang_res.c_str() << std::endl;
  100. }
  101. }
  102.  
  103.  
  104. /*
  105. * Remove the xattr.
  106. */
  107. {
  108. ret = io_ctx.rmxattr("hw", "lang");
  109. if (ret < 0) {
  110. std::cerr << "Failed to remove xattr! error "
  111. << ret << std::endl;
  112. exit(EXIT_FAILURE);
  113. } else {
  114. std::cout << "Removed the xattr 'lang' from our object!" << std::endl;
  115. }
  116. }
  117.  
  118. /*
  119. * Remove the object.
  120. */
  121. {
  122. ret = io_ctx.remove("hw");
  123. if (ret < 0) {
  124. std::cerr << "Couldn't remove object! error " << ret << std::endl;
  125. exit(EXIT_FAILURE);
  126. } else {
  127. std::cout << "Removed object 'hw'." << std::endl;
  128. }
  129. }
  130. }

Python Example

  1. print "\n\nI/O Context and Object Operations"
  2. print "================================="
  3.  
  4. print "\nCreating a context for the 'data' pool"
  5. if not cluster.pool_exists('data'):
  6. raise RuntimeError('No data pool exists')
  7. ioctx = cluster.open_ioctx('data')
  8.  
  9. print "\nWriting object 'hw' with contents 'Hello World!' to pool 'data'."
  10. ioctx.write("hw", "Hello World!")
  11. print "Writing XATTR 'lang' with value 'en_US' to object 'hw'"
  12. ioctx.set_xattr("hw", "lang", "en_US")
  13.  
  14.  
  15. print "\nWriting object 'bm' with contents 'Bonjour tout le monde!' to pool 'data'."
  16. ioctx.write("bm", "Bonjour tout le monde!")
  17. print "Writing XATTR 'lang' with value 'fr_FR' to object 'bm'"
  18. ioctx.set_xattr("bm", "lang", "fr_FR")
  19.  
  20. print "\nContents of object 'hw'\n------------------------"
  21. print ioctx.read("hw")
  22.  
  23. print "\n\nGetting XATTR 'lang' from object 'hw'"
  24. print ioctx.get_xattr("hw", "lang")
  25.  
  26. print "\nContents of object 'bm'\n------------------------"
  27. print ioctx.read("bm")
  28.  
  29. print "Getting XATTR 'lang' from object 'bm'"
  30. print ioctx.get_xattr("bm", "lang")
  31.  
  32.  
  33. print "\nRemoving object 'hw'"
  34. ioctx.remove_object("hw")
  35.  
  36. print "Removing object 'bm'"
  37. ioctx.remove_object("bm")

Java-Example

  1. import com.ceph.rados.Rados;
  2. import com.ceph.rados.RadosException;
  3.  
  4. import java.io.File;
  5. import com.ceph.rados.IoCTX;
  6.  
  7. public class CephClient {
  8. public static void main (String args[]){
  9.  
  10. try {
  11. Rados cluster = new Rados("admin");
  12. System.out.println("Created cluster handle.");
  13.  
  14. File f = new File("/etc/ceph/ceph.conf");
  15. cluster.confReadFile(f);
  16. System.out.println("Read the configuration file.");
  17.  
  18. cluster.connect();
  19. System.out.println("Connected to the cluster.");
  20.  
  21. IoCTX io = cluster.ioCtxCreate("data");
  22.  
  23. String oidone = "hw";
  24. String contentone = "Hello World!";
  25. io.write(oidone, contentone);
  26.  
  27. String oidtwo = "bm";
  28. String contenttwo = "Bonjour tout le monde!";
  29. io.write(oidtwo, contenttwo);
  30.  
  31. String[] objects = io.listObjects();
  32. for (String object: objects)
  33. System.out.println(object);
  34.  
  35. io.remove(oidone);
  36. io.remove(oidtwo);
  37.  
  38. cluster.ioCtxDestroy(io);
  39.  
  40. } catch (RadosException e) {
  41. System.out.println(e.getMessage() + ": " + e.getReturnValue());
  42. }
  43. }
  44. }

PHP Example

  1. <?php
  2.  
  3. $io = rados_ioctx_create($r, "mypool");
  4. rados_write_full($io, "oidOne", "mycontents");
  5. rados_remove("oidOne");
  6. rados_ioctx_destroy($io);

Step 4: Closing Sessions

Once your app finishes with the I/O Context and cluster handle, the app shouldclose the connection and shutdown the handle. For asynchronous I/O, the appshould also ensure that pending asynchronous operations have completed.

C Example

  1. rados_ioctx_destroy(io);
  2. rados_shutdown(cluster);

C++ Example

  1. io_ctx.close();
  2. cluster.shutdown();

Java Example

  1. cluster.ioCtxDestroy(io);
  2. cluster.shutDown();

Python Example

  1. print "\nClosing the connection."
  2. ioctx.close()
  3.  
  4. print "Shutting down the handle."
  5. cluster.shutdown()

PHP Example

  1. rados_shutdown($r);