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.

librados 简介 - 图1

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

第一步:获取 librados

你的客户端应用必须绑定 librados 才能连接 Ceph 存储集群。在写使用 librados 的应用程序前,要安装 librados 及其他依赖包。 librados API 本身是用 C++ 实现的,另外有 C 、 Python 、 Java 和 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

获取 librados 的 Python 支持

rados.py 模块为 Python 应用提供了 librados 支持。在 Debian/Ubuntu 下软件包名为 librados-dev ,在 RHEL/CentOS 下是 librados2-devel ,它们包含了 python-rados 包。你也可以直接安装 python-rados

要在 Debian/Ubuntu 发行版上安装 librados 的 Python 开发支持文件,用此命令:

  1. sudo apt-get install python-rados

要在 RHEL/CentOS 发行版上安装 librados 的 Python 开发支持文件,用此命令:

  1. sudo yum install python-rados

此模块在 Debian 风格的系统上安装到了 /usr/share/pyshared ,在 CentOS/RHEL 系统上安装到了 /usr/lib/python*/site-packages

获取 librados 的 Java 支持

要安装 librados 的 Java 支持,你需要执行下列步骤:

  • 安装 jna.jar 。在 Debian/Ubuntu 系统下应执行:
  1. sudo apt-get install libjna-java

在 CentOS/RHEL 下应执行:

  1. sudo yum install jna

JAR 文件位于 /usr/share/java 。

  • 克隆 rados-java 软件库:
  1. git clone --recursive https://github.com/ceph/rados-java.git
  • 构建 rados-java 软件库:
  1. cd rados-java
  2. ant

JAR 文件位于 rados-java/target 。

  • 把 RADOS 的 JAR 文件复制到统一位置(如 /usr/share/java ),并确保它和 JNA JAR 都位于 JVM 的类路径里。例如:
  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

要编译文档,用下列命令:

  1. ant docs

获取 librados 的 PHP 绑定

要安装 librados 的 PHP 扩展,可按如下步骤:

  • 安装 php-dev ,在 Debian/Ubuntu 下应该执行:
  1. sudo apt-get install php5-dev build-essential

在 CentOS/RHEL 下应该执行:

  1. sudo yum install php-devel
  • 克隆 phprados 源码库:
  1. git clone https://github.com/ceph/phprados.git
  • 构建 phprados:
  1. cd phprados
  2. phpize
  3. ./configure
  4. make
  5. sudo make install
  • 把下列配置加入 php.ini 以启用 phprados:
  1. extension=rados.so

第二步:配置集群句柄

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 ruleset 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.

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,. monhost). 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.

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

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;
  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. ret = EXIT_FAILURE;
  22. return 1;
  23. } else {
  24. std::cout << "Created a cluster handle." << std::endl;
  25. }
  26. }
  27.  
  28. /* Read a Ceph configuration file to configure the cluster handle. */
  29. {
  30. ret = cluster.conf_read_file("/etc/ceph/ceph.conf");
  31. if (ret < 0) {
  32. std::cerr << "Couldn't read the Ceph configuration file! error " << ret << std::endl;
  33. ret = EXIT_FAILURE;
  34. return 1;
  35. } else {
  36. std::cout << "Read the Ceph configuration file." << std::endl;
  37. }
  38. }
  39.  
  40. /* Read command line arguments */
  41. {
  42. ret = cluster.conf_parse_argv(argc, argv);
  43. if (ret < 0) {
  44. std::cerr << "Couldn't parse command line options! error " << ret << std::endl;
  45. ret = EXIT_FAILURE;
  46. return 1;
  47. } else {
  48. std::cout << "Parsed command line options." << std::endl;
  49. }
  50. }
  51.  
  52. /* Connect to the cluster */
  53. {
  54. ret = cluster.connect();
  55. if (ret < 0) {
  56. std::cerr << "Couldn't connect to cluster! error " << ret << std::endl;
  57. ret = EXIT_FAILURE;
  58. return 1;
  59. } else {
  60. std::cout << "Connected to the cluster." << std::endl;
  61. }
  62. }
  63.  
  64. return 0;
  65. }

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 实例

在启用了 RADOS 扩展的 PHP 上,新建集群句柄非常简单:

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

把上述内容保存为 rados.php 并运行:

  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.

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

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 实例

  1. <?php
  2. $io = rados_ioctx_create($r, "mypool");
  3. rados_write_full($io, "oidOne", "mycontents");
  4. rados_remove("oidOne");
  5. 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();

Python Example

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

PHP 实例

  1. rados_shutdown($r);