如何批量插入数百万个Json对象到Realm?

时间:2018-02-01 14:58:42

标签: android realm zipfile

我当前的Android应用程序是一个离线参考数据库。

我使用的数据库是Realm。

我加载领域数据库的过程如下

1)。下载包含原始数据的zip文件(文件内容由json数组组成)

2)。将文件保留到内部存储

3)。使用

读取zip文件(不解压缩)和“流”到inMemory()域中
realm.createAllFromJson(Data.class, inputStream);

4)。将inMemory()域复制到基于磁盘的Realm with

memoryRealm.writeCopyTo(diskFile);

这适用于具有15万个对象的json数组

但是我的生产版本中需要2500到4,000万个对象。

对于20万亿个对象,当json数组“流式化”到inMemory()域时,该过程失败。

我使用的代码如下: -

final Realm memoryRealm = Realm.getInstance(REALM_CONFIGURATION.IN_MEMORY.getRealmConfiguration());

final File diskFile = new File(getFilesDir().getPath() + "/test-data.zip");
final ZipFile zip = new ZipFile(diskFile);

for (Enumeration e = zip.entries(); e.hasMoreElements(); ) {
    final ZipEntry entry = (ZipEntry) e.nextElement();

    if (!entry.isDirectory()) {

        try (final InputStream inputStream = zip.getInputStream(entry)) {
            memoryRealm.executeTransaction(realm -> {
                try {
                    realm.createAllFromJson(Code.class, inputStream);
                } catch (IOException e1) {
                    Log.e(TAG, "execute: ", e1);
                }
            });
        }
    }
}

我的应用程序刚刚关闭,logcat除此之外没有显示更多细节(我的应用程序:com.research.reference.database)

02-01 14:28:47.480 883-938/? E/ActivityManager: ANR in com.estrongs.android.pop
PID: 15848
Reason: executing service com.estrongs.android.pop/com.android.apps.pros.LocalMService
Load: 0.0 / 0.0 / 0.0
    CPU usage from 296122ms to 0ms ago (2018-02-01 14:23:51.192 to 2018-02-01 14:28:47.315):
    108% 15148/com.research.reference.database: 106% user + 2.5% kernel / faults: 793345 minor
    68% 15848/com.estrongs.android.pop: 65% user + 3% kernel / faults: 1248171 minor
    24% 13727/com.metago.astro:remote: 22% user + 1.9% kernel / faults: 15561 minor 3 major

和这个

02-01 14:33:39.995 683-683/? E/lowmemorykiller: Error writing /proc/14864/oom_score_adj; errno=22
02-01 14:33:40.011 683-683/? E/lowmemorykiller: Error writing /proc/14864/oom_score_adj; errno=22
02-01 14:33:40.027 479-479/? E/hw-IPCThreadState: attemptIncStrongHandle(56): Not supported
02-01 14:33:40.136 683-683/? E/lowmemorykiller: Error writing /proc/16000/oom_score_adj; errno=22
02-01 14:33:40.142 683-683/? E/lowmemorykiller: Error writing /proc/16000/oom_score_adj; errno=22
02-01 14:33:40.148 683-683/? E/lowmemorykiller: Error writing /proc/15932/oom_score_adj; errno=22
02-01 14:33:40.153 683-683/? E/lowmemorykiller: Error writing /proc/16127/oom_score_adj; errno=22
02-01 14:33:40.153 683-683/? E/lowmemorykiller: Error writing /proc/14735/oom_score_adj; errno=22
02-01 14:33:40.153 683-683/? E/lowmemorykiller: Error writing /proc/15932/oom_score_adj; errno=22
02-01 14:33:40.185 683-683/? E/lowmemorykiller: Error writing /proc/16517/oom_score_adj; errno=22
02-01 14:33:40.185 683-683/? E/lowmemorykiller: Error writing /proc/13774/oom_score_adj; errno=22
02-01 14:33:40.185 683-683/? E/lowmemorykiller: Error writing /proc/16127/oom_score_adj; errno=22
02-01 14:33:40.214 683-683/? E/lowmemorykiller: Error writing /proc/16517/oom_score_adj; errno=22
02-01 14:33:40.214 683-683/? E/lowmemorykiller: Error writing /proc/13774/oom_score_adj; errno=22
02-01 14:33:40.214 683-683/? E/lowmemorykiller: Error writing /proc/16127/oom_score_adj; errno=22
02-01 14:33:40.259 683-683/? E/lowmemorykiller: Error opening /proc/16127/oom_score_adj; errno=2
02-01 14:33:40.260 683-683/? E/lowmemorykiller: Error writing /proc/13744/oom_score_adj; errno=22
02-01 14:33:40.260 683-683/? E/lowmemorykiller: Error writing /proc/16517/oom_score_adj; errno=22
02-01 14:33:40.260 683-683/? E/lowmemorykiller: Error writing /proc/16097/oom_score_adj; errno=22
02-01 14:33:40.260 683-683/? E/lowmemorykiller: Error writing /proc/15932/oom_score_adj; errno=22
02-01 14:33:40.265 683-683/? E/lowmemorykiller: Error writing /proc/16517/oom_score_adj; errno=22
02-01 14:33:40.265 683-683/? E/lowmemorykiller: Error opening /proc/16457/oom_score_adj; errno=2
02-01 14:33:40.265 683-683/? E/lowmemorykiller: Error writing /proc/16097/oom_score_adj; errno=22
02-01 14:33:40.266 683-683/? E/lowmemorykiller: Error writing /proc/15932/oom_score_adj; errno=22
02-01 14:33:40.281 683-683/? E/lowmemorykiller: Error writing /proc/16097/oom_score_adj; errno=22
02-01 14:33:40.290 683-683/? E/lowmemorykiller: Error writing /proc/16097/oom_score_adj; errno=22
02-01 14:33:40.304 683-683/? E/lowmemorykiller: Error opening /proc/16097/oom_score_adj; errno=2
02-01 14:33:40.422 883-1227/? E/InputDispatcher: channel '33ae07f com.android.settings/com.android.settings.Settings (server)' ~ Channel is unrecoverably broken and will be disposed!
02-01 14:33:41.148 883-1227/? E/InputDispatcher: channel '35729f1 com.research.reference.database/com.research.reference.database.MainActivity (server)' ~ Channel is unrecoverably broken and will be disposed!

如何分解我的批量插入,以便插入所有压缩的json对象?

更新

此过程在其自己的线程中运行

 final Thread thread = new Thread(new Runnable() {
            @Override
            public void run() {


                try {
                    writeToMemory();
                } catch (IOException e) {
                    Log.e(TAG, "realming: ", e);
                }

                copyMemoryToDisk();

            }
        });

        thread.start();

0 个答案:

没有答案