Cephfs java api

环境

运行java的程序的主机必须安装libcephfs!!!

wget https://mirrors.aliyun.com/centos/7/storage/x86_64/ceph-luminous/libcephfs2-12.2.5-0.el7.x86_64.rpm
wget https://mirrors.aliyun.com/centos/7/storage/x86_64/ceph-luminous/libcephfs_jni1-12.2.5-0.el7.x86_64.rpm
yum install -y libcephfs2-12.2.5-0.el7.x86_64.rpm libcephfs_jni1-12.2.5-0.el7.x86_64.rpm

之后,在/usr/lib64中会生成libcephfs_jni.so.1,libcephfs_jni.so.1.0.0,java程序还是找不到,所以还要建立软连接到/usr/lib

ln -s /usr/lib64/libcephfs_jni.so.1 /usr/lib/libcephfs_jni.so.1
ln -s /usr/lib64/libcephfs_jni.so.1.0.0 /usr/lib/libcephfs_jni.so

注:最后一行必须是so结尾,而不是so.1.0.0

Java API

方便测试,建立一个普通的mvn工程,pom.xml文件如下

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.shaoyan</groupId>
    <artifactId>cephfs-test</artifactId>
    <version>1.0-SNAPSHOT</version>

    <dependencies>
        <dependency>
            <groupId>com.ceph</groupId>
            <artifactId>libcephfs</artifactId>
            <version>0.80.5</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/com.ceph/rados -->
        <dependency>
            <groupId>com.ceph</groupId>
            <artifactId>rados</artifactId>
            <version>0.3.0</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>

            <plugin>
                <artifactId>maven-assembly-plugin</artifactId>
                <configuration>
                    <archive>
                        <manifest>
                            <mainClass>CephFSTest</mainClass>
                        </manifest>
                    </archive>
                    <descriptorRefs>
                        <descriptorRef>jar-with-dependencies</descriptorRef>
                    </descriptorRefs>
                </configuration>

                <executions>
                    <execution>
                        <id>make-assembly</id>
                        <phase>package</phase>
                        <goals>
                            <goal>assembly</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>

        </plugins>
    </build>

</project>

说明:

  1. 使用maven-assembly-plugin,否则打包出来的jar没有包含其他maven依赖。
  2. 在在项目的目录下执行 mvn clean package,会在项目下生成 target 文件夹,并有两个jar包,一个包含依赖,一个不包含
archive-tmp                     classes
cephfs-test-1.0-SNAPSHOT-jar-with-dependencies.jar  maven-archiver
cephfs-test-1.0-SNAPSHOT.jar                maven-status

为方便测试每一个api,这里尽可能将其一一分开测试,测试程序如下:

package com.shaoyan.ceph;

import com.ceph.fs.CephFileExtent;
import com.ceph.fs.CephMount;
import com.ceph.fs.CephStat;

import java.io.IOException;
import java.util.Arrays;

public class CephOperate {

    private CephMount mount;
    private String username;
    private String monIp;
    private String userKey;

    public CephOperate(String username, String monIp, String userKey, String mountPath) {
        this.username = username;
        this.monIp = monIp;
        this.userKey = userKey;
        this.mount = new CephMount(username);
        this.mount.conf_set("mon_host", monIp);
        mount.conf_set("key",userKey);
        mount.mount(mountPath);
    }

    //查看目录列表
    public void listDir(String path) throws IOException {
        String[] dirs = mount.listdir(path);
        System.out.println("contents of the dir: " + Arrays.asList(dirs));
    }

    //新建目录
    public void mkDir(String path) throws IOException {
        mount.mkdirs(path,0755);//0表示十进制
    }

    //删除目录
    public void delDir(String path) throws IOException {
        mount.rmdir(path);
    }

    //重命名目录or文件
    public void renameDir(String oldName, String newName) throws IOException {
        mount.rename(oldName, newName);
    }

    //删除文件
    public void delFile(String path) throws IOException {
        mount.unlink(path);
    }

    //读文件
    public void readFile(String path) {
        System.out.println("start read file...");
        int fd = -1;
        try{
            fd = mount.open(path, CephMount.O_RDWR, 0755);
            System.out.println("file fd is : " + fd);

            byte[] buf = new byte[1024];
            long size = 10;
            long offset = 0;
            long count = 0;
            while((count = mount.read(fd, buf, size, offset)) > 0){
                for(int i = 0; i < count; i++){
                    System.out.print((char)buf[i]);
                }
                offset += count;
            }

        } catch (IOException e){
            e.printStackTrace();
        } finally {
            if(fd > 0){
                mount.close(fd);
            }
        }
    }

    //复制文件
    public void copyFile(String sourceFile, String targetFile){
        System.out.println("start write file...");
        int readFD = -1, createAA = -1, writeFD = -1;
        try{
            readFD = mount.open(sourceFile, CephMount.O_RDWR, 0755);
            writeFD = mount.open(targetFile, CephMount.O_RDWR | CephMount.O_CREAT, 0644);
//                createAA = mountLucy.open("aa.txt", CephMount.O_RDWR | CephMount.O_CREAT | CephMount.O_EXCL, 0644);//若文件已有, 会异常
            System.out.println("file read fd is : " + readFD);

            byte[] buf = new byte[1024];
            long size = 10;
            long offset = 0;
            long count = 0;
            while((count = mount.read(readFD, buf, size, -1)) > 0){
                mount.write(writeFD, buf, count, -1);//-1指针跟着走,若取值count,指针不动
                System.out.println("offset: " + offset);
                offset += count;
                System.out.println("writeFD position : " + mount.lseek(writeFD, 0, CephMount.SEEK_CUR));
            }

        } catch (IOException e){
            e.printStackTrace();
        } finally {
            if(readFD > 0){
                mount.close(readFD);
            }
            if(writeFD > 0){
                mount.close(writeFD);
            }
        }
    }

    //写文件
    public void writeFileWithLseek(String path, long offset, int type){
        if(type <= 0){
            type =CephMount.SEEK_CUR;
        }
        System.out.println("start write file...");
        int writeFD = -1;
        try{
            writeFD = mount.open(path, CephMount.O_RDWR | CephMount.O_APPEND, 0644);
            long pos = mount.lseek(writeFD, offset, type);
            System.out.println("pos : " + pos);
            String msg = " asdfasdfasdf123123123 \n";
            byte[] buf = msg.getBytes();
            mount.write(writeFD, buf, buf.length, pos);

        } catch (IOException e){
            e.printStackTrace();
        } finally {
            if(writeFD > 0){
                mount.close(writeFD);
            }
        }
    }

    // 判断是目录还是文件
    public void listFileOrDir(){
        int writeFD = -1;
        try{
            String[] lucyDir = mount.listdir("/");
            for(int i = 0; i < lucyDir.length; i++){
                CephStat cephStat = new CephStat();
                mount.lstat(lucyDir[i], cephStat);
                System.out.println(lucyDir[i] + " is dir : " + cephStat.isDir()
                        + " is file: " + cephStat.isFile()
                        + " size: " + cephStat.size
                        + " blksize: " + cephStat.blksize);//cephStat.size就是文件大小
            }

            writeFD = mount.open("lucy1.txt", CephMount.O_RDWR | CephMount.O_APPEND, 0644);
            CephFileExtent cephFileExtent = mount.get_file_extent(writeFD, 0);
            System.out.println("lucy1.txt size: " + cephFileExtent.getLength());//4M
            System.out.println("lucy1.txt stripe unit: " + mount.get_file_stripe_unit(writeFD));//4M
            long pos = mount.lseek(writeFD, 0, CephMount.SEEK_END);
            System.out.println("lucy1.txt true size: " + pos);//30Byte

        } catch (IOException e){
            e.printStackTrace();
        } finally {
            if(writeFD > 0){
                mount.close(writeFD);
            }
        }
    }

    //set current dir (work dir)
    public void setWorkDir(String path) throws IOException{
        mount.chdir(path);
    }


    //外部获取mount
    public CephMount getMount(){
        return this.mount;
    }

    //umount
    public void umount(){
        mount.unmount();
    }



}

package com.shaoyan.ceph;

import com.ceph.fs.CephMount;

import java.io.IOException;

public class CephFSTest {
    public static void main(String[] args) throws IOException{
        System.out.println("start...." + CephMount.class);

        String username = "admin";
        String monIp = "192.168.16.4:6789;192.168.16.5:6789;192.168.16.6:6789";
        String userKey = "AQC/tCxb/IKAERAAaHxoJyxP01pE7JT+f0vT5Q==";
        String mountPath = "/";

        CephOperate cephOperate = new CephOperate(username, monIp, userKey, mountPath);

        CephMount cephMount = cephOperate.getMount();
        cephMount.symlink("/lucy/bb.txt", "/jerry/softlink.txt");
        cephMount.link("/lucy/bb.txt", "/jerry/hardlink.txt");

        String content = cephMount.readlink("/jerry/softlink.txt");//"/lucy/bb.txt"
        System.out.println(content);

        System.out.println("hard link... ");
        cephOperate.readFile("/jerry/hardlink.txt");

        System.out.println("soft link... ");
        cephOperate.readFile("/jerry/softlink.txt");
        cephOperate.writeFileWithLseek("/jerry/softlink.txt", 0, 0);

        cephOperate.umount();


    }
}

问题

  1. 虽然cephfs支持随机写,但java api并不完全支持(可以随机进行定位,但是无法插入内容,即旧数据会被新数据覆盖)
  2. ceph未提供用户认证和权限管理的api,即无法通过java python go等进行用户管理和用户权限管理的操作

References

  1. http://docs.ceph.com/docs/giant/api/libcephfs-java/javadoc/
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 219,869评论 6 508
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 93,716评论 3 396
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 166,223评论 0 357
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 59,047评论 1 295
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 68,089评论 6 395
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 51,839评论 1 308
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,516评论 3 420
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 39,410评论 0 276
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 45,920评论 1 319
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 38,052评论 3 340
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 40,179评论 1 352
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,868评论 5 346
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,522评论 3 331
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 32,070评论 0 22
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 33,186评论 1 272
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 48,487评论 3 375
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 45,162评论 2 356

推荐阅读更多精彩内容