RxJava接近来自数据库的读取倍数条目并将它们写入多个文件中

时间:2015-11-27 09:27:09

标签: java multithreading concurrency rx-java

我想是否值得将RXJava用于下面描述的场景。

一次检索包含一组所需条目的列表,例如特定列的表中查询/选择的结果集。

然后需要在另一个数据源中执行新查询,以获取for循环中第一个列表中每个元素的相关属性(每个元素一行或多行)。

最后,在csv文件中写入检索到的属性集。

例如,可以从数据库中检索公司列表,在这种情况下,是财富杂志中列出的第100个公司,并将列表中的100个对象存储在字符串中。

然后,每个公司使用不同的数据源在过去30天内获取纳斯达克股票价格的信息,并将每个价格存储在地图中,作为每个公司名称的密钥值。

下图显示了地图中的数据样本。

{"Google", [450,440,455...[29]]},
{"Apple", [550,540,555...[29]]},
{"Oracle", [50,40,55...[29]]},
{"Amazon", [350,340,355...[29]]}

...

最后,迭代地图以在csv文件中的一行中写入每个条目。

如何使用RxJava执行此任务的一些架构,示例或伪代码?

感谢您的帮助。

1 个答案:

答案 0 :(得分:2)

这个怎么样:

首先,一些辅助方法和一个Tuple类:

public static class CompanyNameAndPricesTuple {
    private final String companyName;
    private final List<Double> prices;

    public CompanyNameAndPricesTuple(String companyName, List<Double> prices) {
        this.companyName = companyName;
        this.prices = prices;
    }

    public String getCompanyName() {
        return companyName;
    }

    public List<Double> getPrices() {
        return prices;
    }
}

//Dummy method for fetching the list of companies from the DB
public static List<String> getCompaniesFromDB() {
    return Arrays.asList("Google", "Apple", "Oracle", "Amazon");
}

//Dummy method for fetching the list of prices for a company
public static List<Double> getStockPricesOfCompany(String companyName) {
    try {
        Thread.sleep((long) (Math.random() * 100)); //Simulate an IO access to some external resource
    } catch (InterruptedException e) {
    }

    //A list of 100 random doubles, using Rx of course
    return Observable.fromCallable(() -> Math.random() * 1000).repeat(100).toList().toBlocking().first();
}

现在创建Map:

Map<String, List<Double>> map =
        // Get the list of companies
Observable.from(getCompaniesFromDB())
        // Use flat map with subscribeOn to make price lookup
        // asynchronous since it's an IO operation
        .flatMap(
                company -> Observable
                        .fromCallable(
                                () -> new CompanyNameAndPricesTuple(company, getStockPricesOfCompany(company)))
                            .subscribeOn(Schedulers.io()))
        // Create a map out of the tuples we've created
        .toMap(tuple -> tuple.companyName, tuple -> tuple.getPrices())
        // Get the map
        .toBlocking().first();

浏览地图以创建CSV:

//Go over the map using RX and print the entries to csv
Observable.from(map.entrySet()).subscribe(entry -> {
    StringJoiner csvJoiner = new StringJoiner("\",\"", "\"", "\"");
    csvJoiner.add(entry.getKey());
    Observable.from(entry.getValue()).subscribe(price -> csvJoiner.add(Double.toString(price)));
    System.out.println(csvJoiner.toString());
});

示例输出:

"Google","959.2878508843913","436.641836332889","949.2311108474885","430.88591824803046","998.3635151904227","787.6192929324355","246.70094442318103","768.9725060603417","454.29280887009605","979.7358119484536","978.9935637832604","450.1620838295586","597.781415996234","635.8082520052969","267.56600364514094","340.6035839879187","421.375027366284","338.3737431595535","643.5942337386992","275.921096115596","166.35756640012755","905.1095809053156","214.25608647864814","190.0991876053689","348.4232512343668","558.5585300521548","357.0492600705275","990.6541858659072","61.95489155415268","823.7783298809883","90.1960325311989","495.44293591634647","920.218185518567","830.1251898386492","280.7317476822244","239.42216850958764","66.32120740315828","678.619930151823","334.26611360276723","3.5426702898532225","261.8608575518252","819.7474609802136","644.5058443633695","71.02595530405776","647.0229374801525","184.9729100706564","670.9919121858969","125.15638791105543","450.41769972055965","744.9914482542455","900.4645027092372","485.5339774633393","409.0077954125788","515.4293415031436","184.29510365074307","789.6656706892425","762.0327561549535","217.01549434227007","763.615314573589","364.196669887774","220.92635290736652","947.9381010328653","530.7287585811933","113.3814222988444","135.67912412570305","561.4835150734573","203.353215478841","897.8307503777418","719.2741196772579","111.30540698657376","107.04479472561734","250.0088297129157","134.04774555273247","200.55518823852015","787.3031516733688","423.067424811199","565.185412188241","809.6203452093137","495.65673829294667","651.3121526209319","342.8337549590542","804.7921587674813","729.4558663703352","919.1152851576243","177.2395792303173","998.8322660124871","872.766240624","25.51460195522459","303.4651003791586","837.4513262972841","776.4174040871623","976.044676203481","232.12142009210223","747.4697560622542","696.0224995089213","319.4536209676343","313.4547165687421","172.52609015709885","510.01478159786694","275.5668577430399"
"Apple","291.11293075486867","785.0336450686032","652.069716836489","754.4704913558785","108.67936438982983","840.7847445228634","150.6491307545459","998.6263206447975","670.8870247679373","302.8284322681881","993.8050426444913","205.6723694585999","758.1210872402044","365.14395040500057","236.67461601505602","835.284877025743","617.1110461567522","705.4426490428996","379.8119578857708","424.81813911683184","377.1038401619066","306.3671064837501","692.9496993894444","64.24219120660734","832.3294048479033","814.9184256777826","253.0802452171209","752.5052589517011","738.9518932994971","881.3978700476039","617.0248430194841","440.96422603527975","410.77445878547945","152.2785990601315","635.1542734592632","995.289705579233","197.50270914088918","786.2754306884477","376.04053005536554","830.5164539552532","961.8697901213636","590.1612005739438","714.2028546472743","419.2927188004315","371.24670910223836","387.63195865311116","707.8805900783582","421.40125241861926","78.85800600192117","899.8933242080855","595.672038312263","856.7047539525968","345.289284976112","808.3471844503869","62.90019169605432","463.75842409376264","802.1436984337877","7.10278690983468","672.8453290771528","623.102277247963","144.53366269054413","657.0033655078183","819.9064687819746","338.275706554625","775.9113817460491","611.3178188390739","491.09220032066247","19.245648138829672","687.2465001107902","15.714562284498857","718.3474262884066","680.7629279604844","511.57711809799554","759.9521826136543","523.4303384406502","649.3580392980201","321.5228754137637","16.497737419232216","183.17158793288846","846.5397655299303","87.23500853581534","279.92509063004434","404.43089311412797","392.95987400824714","330.5516504312694","884.6743174057291","870.4926067619718","642.8787745211212","592.6055958180643","475.8243883086172","451.9930907352392","390.60229793397104","341.7082831852384","675.3921108835287","672.4291416359736","699.8968853721092","204.02549329245323","786.7778986338739","556.5399117963805","726.6301187723658"
"Oracle","396.71881895692616","779.3755500334734","632.292494780106","683.211168130178","440.86033446206665","444.69149786329297","890.4771062562829","396.5244929261328","222.61124137253918","544.6321979210616","936.0567053211939","11.714671675446041","812.8232254525584","446.5498720737745","973.7046907750497","619.8719006862021","9.054677977662351","40.68069316577638","903.0309452234895","261.0989303085078","780.0603819874879","992.8214429187562","888.9989801716529","773.8372524854843","907.5173864139356","631.1087653000355","816.3620855343391","554.5563986773843","745.6715886350912","91.93188945055486","334.4476454114842","595.5128649441835","36.191798571644675","593.1731776914423","230.84555328360733","52.07380884993851","13.368755165875768","477.3853623386546","686.0334185144663","506.14002694065186","613.8989723178786","973.177858392173","555.6973316556204","38.49171916039062","465.8444109661538","459.9993426061023","203.48377904144087","200.41958003933692","472.96034180962386","389.66696726233687","564.1452267933737","983.7928258122934","233.43588581499986","917.4075415291275","55.51754481097382","928.7547078941666","236.1929729106782","725.8392032093936","677.4950610675298","579.5256363093064","763.2407670079655","487.2370803494914","137.1447363469025","601.3320959770995","693.1846829681579","902.5895100993929","121.32812692027828","5.269794586020948","948.8404633325544","280.3663709883006","901.6758647648297","92.94767141034998","161.0050452029651","173.58452243502597","301.48640303218775","929.0488298957273","230.78803957349447","680.6696458381412","680.0970051473068","377.4833599235019","864.5088530368304","633.7779019056729","997.5015912506425","995.5972824290502","828.0162058290772","47.6546695809793","210.46681256649046","370.9407791413722","621.3880422585504","882.771883552694","221.12405134489921","806.3877914558287","510.32212450549883","388.39966306507466","349.99167144066524","611.9384765871963","367.1282812735893","34.43129441385639","837.6084147326712","645.3248232926841"
"Amazon","572.085209843116","315.19596473776636","192.43554516534456","158.57868376105822","305.0268457378549","514.1562218291148","297.4115444613473","454.01112407755284","917.37378562943","855.3023479086708","63.52026221993023","330.5900382159442","716.4276078402033","335.21954211757253","75.95936181842944","810.4932095751776","416.59749865902774","929.2173926802831","116.9730068563165","175.6522673430879","716.329525566489","795.5759561944192","669.1389444617747","915.3920589512217","944.8611971331","563.8763747129032","503.87692193377245","788.8096210651095","489.52503590441745","786.5480201313527","242.1717533425537","839.3253321895784","762.1767170733514","157.17845126268793","337.9811673233969","863.8783989481051","792.4244765516959","154.79624191237852","279.52465408183315","535.5273302446069","145.68893414343353","360.25094193587836","929.6268108330559","105.75582831776975","755.7226644152611","808.9788441471997","833.514582623111","371.57298034996165","434.07211417078196","393.85638774045674","10.944574701129529","382.5358549575427","273.06416524600695","512.5080330260929","219.26348356881098","184.11207302632891","894.2530166195362","865.3491486253771","924.9808071594011","746.338275959416","510.8632113334423","553.8082764209388","253.65958708990243","110.52002575915498","70.61686626381214","402.58749876010734","1.4081118953461758","701.7576629244851","324.4347941541167","735.4837219889406","385.9810940231063","701.3713030311328","526.8523865416837","671.2074954568883","591.4763567136415","350.8379760926574","615.5929261511512","373.8002656342847","317.83889383839903","67.64196884620965","140.59250902804143","591.6649609680568","484.86106792186","293.479488961957","916.8724718788407","249.88888709421232","960.0944074430158","66.5473449676861","361.00642351546605","822.4517711044532","681.0646709698492","412.18397613089905","737.5308183611437","704.0764559087525","824.4085909087993","779.9214074693646","89.49975450463587","825.8927228520342","738.0274423067282","920.0444933517336"

请注意,如果您只需要CSV并且您并不真正需要Map<String,List<Double>>,我们可以跳过代码中的几个步骤。

相关问题