how to backup index files with Replicator

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

how to backup index files with Replicator

Dancer-2
Hi,
here is my code to backup index files with Lucene Replicator,but It doesn't work well, No files were backuped.
Could you check my code and give me your advice?


public class IndexFiles {


        private static Directory dir;
        private static Path bakPath;
        private static LocalReplicator replicator;


        public static LocalReplicator getInstance() {
                if (replicator == null) {
                        replicator = new LocalReplicator();
                }
                return replicator;
        }
        public static Directory getDirInstance() {
                if (dir == null) {
                        try {
                                dir = FSDirectory.open(Paths.get("/tmp/index"));
                        } catch (IOException e) {
                                e.printStackTrace();
                        }
                }
                return dir;
        }
        public static Path getPathInstance() {
                if (bakPath == null) {
                        bakPath = Paths.get("/tmp/indexBak");
                }
                return bakPath;
        }


       
        /** Index all text files under a directory. */
        public static void main(String[] args) {
                String id = "-oderfilssdhsjs";
                String title = "足球周刊";
                String body = "今天野狗,我们将关注欧冠赛场,曼联在客场先进一球的情况下,遭对手沃尔夫斯堡以总比分3:2淘汰,"
                                + "遗憾出局,将参加欧联杯的比赛,当红球星马夏尔贡献一球,狼堡进了一个乌龙球,狼堡十号球员德拉克斯勒" + "表现惊艳,多次导演攻势,希望22岁的他能在足球之路上走的更远。";
                try {
                        // Directory dir = FSDirectory.open(Paths.get(indexPath));
                        Analyzer analyzer = new IKAnalyzer(true);
                        IndexWriterConfig iwc = new IndexWriterConfig(analyzer);
                        iwc.setOpenMode(OpenMode.CREATE_OR_APPEND);
                        SnapshotDeletionPolicy snapshotter = new SnapshotDeletionPolicy(new KeepOnlyLastCommitDeletionPolicy());
                        iwc.setIndexDeletionPolicy(snapshotter);
                        IndexWriter writer = new IndexWriter(IndexFiles.getDirInstance(), iwc);// the
                        LocalReplicator replicator = IndexFiles.getInstance();


                        Document doc = new Document();
                        Field articleId = new StringField("id", id, Field.Store.YES);
                        doc.add(articleId);
                        Field articleTitle = new TextField("title", title, Field.Store.YES);
                        doc.add(articleTitle);
                        Field articleBody = new TextField("body", body, Field.Store.NO);
                        doc.add(articleBody);
                        Field tag1 = new TextField("tags", "野狗", Field.Store.NO);
                        doc.add(tag1);
                        // Field tag2 = new TextField("tags", "运动", Field.Store.NO);
                        // doc.add(tag2);
                        // Field tag3 = new TextField("tags", "国足", Field.Store.NO);
                        // doc.add(tag3);
                        // Field tag4 = new TextField("tags", "席大大", Field.Store.NO);
                        // doc.add(tag4);


                        writer.updateDocument(new Term("id", id), doc);
                        writer.commit();
                        ReplicatorThread p = new ReplicatorThread();
                        new Thread(p, "ReplicatorThread").start();
                        replicator.publish(new IndexRevision(writer));
                        Thread.sleep(50000);
                        writer.close();
                } catch (IOException e) {
                        System.out.println(" caught a " + e.getClass() + "\n with message: " + e.getMessage());
                } catch (InterruptedException e) {
                        e.printStackTrace();
                }
        }
}


class ReplicatorThread implements Runnable {


        public void run() {
                Callable<Boolean> callback = null;
                ReplicationHandler handler = null;
                try {
                        handler = new IndexReplicationHandler(IndexFiles.getDirInstance(), callback);
                        SourceDirectoryFactory factory = new PerSessionDirectoryFactory(IndexFiles.getPathInstance());
                        ReplicationClient client = new ReplicationClient(IndexFiles.getInstance(), handler, factory);
                        client.updateNow();
                        try {
                                Thread.sleep(10000);
                        } catch (InterruptedException e) {
                                e.printStackTrace();
                        }
                } catch (IOException e) {
                        e.printStackTrace();
                }
                System.out.println("death");
        }
}

Best Regards.
Jean Ju
Reply | Threaded
Open this post in threaded view
|

Re: how to backup index files with Replicator

wmartinusa
Hi Dancer:

Found this thread with good info that may be irrelevant to your scenario but, this in particular struck me

             writer.waitForMerges();
             writer.commit();
             replicator. replicate(new IndexRevision(writer));
writer.close();

even though writer.close() can trigger a commit. hmmm


thread:

http://grokbase.com/t/lucene/java-user/143dsnrxh8/replicator-how-to-use-it <http://grokbase.com/t/lucene/java-user/143dsnrxh8/replicator-how-to-use-it>

-will



> On Jan 23, 2016, at 4:39 AM, Dancer <[hidden email]> wrote:
>
> Hi,
> here is my code to backup index files with Lucene Replicator,but It doesn't work well, No files were backuped.
> Could you check my code and give me your advice?
>
>
> public class IndexFiles {
>
>
> private static Directory dir;
> private static Path bakPath;
> private static LocalReplicator replicator;
>
>
> public static LocalReplicator getInstance() {
> if (replicator == null) {
> replicator = new LocalReplicator();
> }
> return replicator;
> }
> public static Directory getDirInstance() {
> if (dir == null) {
> try {
> dir = FSDirectory.open(Paths.get("/tmp/index"));
> } catch (IOException e) {
> e.printStackTrace();
> }
> }
> return dir;
> }
> public static Path getPathInstance() {
> if (bakPath == null) {
> bakPath = Paths.get("/tmp/indexBak");
> }
> return bakPath;
> }
>
>
>
> /** Index all text files under a directory. */
> public static void main(String[] args) {
> String id = "-oderfilssdhsjs";
> String title = "足球周刊";
> String body = "今天野狗,我们将关注欧冠赛场,曼联在客场先进一球的情况下,遭对手沃尔夫斯堡以总比分3:2淘汰,"
> + "遗憾出局,将参加欧联杯的比赛,当红球星马夏尔贡献一球,狼堡进了一个乌龙球,狼堡十号球员德拉克斯勒" + "表现惊艳,多次导演攻势,希望22岁的他能在足球之路上走的更远。";
> try {
> // Directory dir = FSDirectory.open(Paths.get(indexPath));
> Analyzer analyzer = new IKAnalyzer(true);
> IndexWriterConfig iwc = new IndexWriterConfig(analyzer);
> iwc.setOpenMode(OpenMode.CREATE_OR_APPEND);
> SnapshotDeletionPolicy snapshotter = new SnapshotDeletionPolicy(new KeepOnlyLastCommitDeletionPolicy());
> iwc.setIndexDeletionPolicy(snapshotter);
> IndexWriter writer = new IndexWriter(IndexFiles.getDirInstance(), iwc);// the
> LocalReplicator replicator = IndexFiles.getInstance();
>
>
> Document doc = new Document();
> Field articleId = new StringField("id", id, Field.Store.YES);
> doc.add(articleId);
> Field articleTitle = new TextField("title", title, Field.Store.YES);
> doc.add(articleTitle);
> Field articleBody = new TextField("body", body, Field.Store.NO);
> doc.add(articleBody);
> Field tag1 = new TextField("tags", "野狗", Field.Store.NO);
> doc.add(tag1);
> // Field tag2 = new TextField("tags", "运动", Field.Store.NO);
> // doc.add(tag2);
> // Field tag3 = new TextField("tags", "国足", Field.Store.NO);
> // doc.add(tag3);
> // Field tag4 = new TextField("tags", "席大大", Field.Store.NO);
> // doc.add(tag4);
>
>
> writer.updateDocument(new Term("id", id), doc);
> writer.commit();
> ReplicatorThread p = new ReplicatorThread();
> new Thread(p, "ReplicatorThread").start();
> replicator.publish(new IndexRevision(writer));
> Thread.sleep(50000);
> writer.close();
> } catch (IOException e) {
> System.out.println(" caught a " + e.getClass() + "\n with message: " + e.getMessage());
> } catch (InterruptedException e) {
> e.printStackTrace();
> }
> }
> }
>
>
> class ReplicatorThread implements Runnable {
>
>
> public void run() {
> Callable<Boolean> callback = null;
> ReplicationHandler handler = null;
> try {
> handler = new IndexReplicationHandler(IndexFiles.getDirInstance(), callback);
> SourceDirectoryFactory factory = new PerSessionDirectoryFactory(IndexFiles.getPathInstance());
> ReplicationClient client = new ReplicationClient(IndexFiles.getInstance(), handler, factory);
> client.updateNow();
> try {
> Thread.sleep(10000);
> } catch (InterruptedException e) {
> e.printStackTrace();
> }
> } catch (IOException e) {
> e.printStackTrace();
> }
> System.out.println("death");
> }
> }
>
> Best Regards.
> Jean Ju