- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我在想对于下面描述的场景是否值得使用 RXJava。
一次检索包含一组所需条目的列表,例如表中特定列的查询/选择的结果集。
然后需要在另一个数据源中执行新查询,以在 for 循环中获取第一个列表中每个元素的相关属性(每个元素一行或多行)。
最后,将检索到的属性集写入 csv 文件。
例如,可以从数据库中检索公司列表,在本例中为《财富》杂志中列出的第 100 个公司,并将这一百个对象存储在列表操作系统字符串中。
然后,对于每家公司,使用不同的数据源获取过去三十天内纳斯达克股票价格的信息,并将每个价格作为带有每个公司名称的键的值存储在 map 中。
下面显示了 map 中数据的示例。
{"Google", [450,440,455...[29]]},
{"Apple", [550,540,555...[29]]},
{"Oracle", [50,40,55...[29]]},
{"Amazon", [350,340,355...[29]]}
...
最后,迭代 map 以将每个条目写入 csv 文件中的一行中。
如何使用 RxJava 执行此任务的一些架构、示例或伪代码?
感谢您的帮助。
最佳答案
这个怎么样:
首先,一些辅助方法和 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();
浏览 map 以创建 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>>
,我们可以跳过代码中的几个步骤。
关于java - RxJava方法从数据库读取多个条目并将它们写入多个文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33954175/
情况:我想从数据条目列表导航回我的 PageViewController。 before 和 previous 函数起作用 func pageViewController(pageViewContro
尊敬的 StackOverflow 用户 我有一个 gradle 项目,我想将其工件转换为 osgi 包。在这个包中,我有: 我不想导出的包(可能不会出现在 list 的 Export-Package
我为我的 PendingIntent 设置了一个警报。现在我想在我的 Activity 中显示是否设置了此警报。 Intent service = new Intent(context, MyServ
我有 2 个表、作者和书籍 authors 包含唯一的 IDauthorId 书籍也包含此作为外键 我需要知道书籍数量最多的作者。如果 2 个或更多作者并列最多书籍,我需要显示这两位作者 我已经能够通
我有一个名为 prospective_shop 的表,其中一个列名称是“用户名”。用户名未设置为主键,但我想删除所有具有重复用户名的行。我怎样才能以最快的方式做到这一点? 我尝试执行以下操作: ALT
我现在可以添加条目了。在我的应用程序中,用户可以在他的日历上输入约会/事件。但在他这样做之前,它应该向他显示他已经添加的事件。它应该从日历中获取事件并将其显示给他。这该怎么做?我被困在这部分。提前致谢
#include #include #include #include #include #include char *msg; ssize_t write_proc(struct file
我想将大于 1024 个字符的字符串传递到我的模块(文件系统)。由于内核参数限制为 1024 个字符,someone recommended改为使用 sysfs。 我试图包括 this example
我正在尝试使用 SQLAlchemy 构建以下查询(用作包含查询的子查询,该查询定义名为 tbl_outer 的别名): SELECT max(tbl.ts) AS max_1 FROM tbl WH
假设我有两张 map : Map map1 = Map.of( "a", "1", "b", "2", "c", "3", "x
通过简化示例,假设您有以下数据集: A B C Name Group Amount Dave A 2 Mike B 3 Adam C 4
我正在尝试在我的服务器上创建一个三级域虚拟主机。我希望配置设置正确,但我得到一个 ERR_NAME_NOT_RESOLVED错误。 我已经读到我必须在某处“添加 DNS 条目”以便解析名称,但我该怎么
我需要一个可用于在逗号分隔列表中查找第 N 个条目的正则表达式。 例如,假设此列表如下所示: abc,def,4322,mail@mailinator.com,3321,alpha-beta,43 .
GWT 应用程序(在 Eclipse 中开发)的源代码管理忽略文件中的典型条目是什么? 最佳答案 我会推荐: 你leave the eclipse files (.project, .classpat
我必须创建显示表 (Tbl) 中所有字段的输出,并创建一个额外的列来按月计算每个客户的累计总和(例如,如果客户在 4 月份有两次销售,新列将具有这些销售额和两行中任何先前销售额的总和)。我能做的就这么
文档 ( http://kubernetes.io/docs/user-guide/configmap/ ) 上用于使用值的示例基于 ConfigMap,其中每个数据条目都是一对/值。例子: apiV
我有一个奇怪的错字,我一遍又一遍地犯,而不是实际工作我的打字技巧,我想编辑我的 AutoHotkey 脚本来弥补这一点。 有时,当我输入大写字母时,我会点击:按钮并输入“I:”,我希望 AHK 仅用字
使用 lgdt 初始化 GDT 并将其加载到 GDTR 后,稍后如何更新 GDT? 如果我使用 sgdt 命令获取基地址,然后更新或添加条目,然后使用 lgdt 再次重新加载,我是否正确?还有其他方法
我有两个应用程序共享同一个数据库,即 API 和 MVC5 应用程序。两者都在本地主机上运行良好,但在部署到我的 Azure 帐户时出现此错误 Configuration Error Descrip
我正在尝试修剪我拥有的一些文件。我将为您保存到目前为止我编写的野兽,并通过提供虚构代码使其保持简单。 让我们来看看这个数组: [System.String[]]$Collection = 'Invit
我是一名优秀的程序员,十分优秀!