Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/algorithm/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Java中搜索大型csv的最佳/高效方法_Java_Algorithm_Csv_Search - Fatal编程技术网

在Java中搜索大型csv的最佳/高效方法

在Java中搜索大型csv的最佳/高效方法,java,algorithm,csv,search,Java,Algorithm,Csv,Search,我有一个很大的csv文件,有1.5K个条目。每个条目都代表全球一个城市的名称、纬度和经度。在Java中搜索csv的最佳快速方法是什么?我想用所有的条目填充ArrayList,但我认为这很慢(除非我错了)。不,这个文件的大小不会增长,它几乎是100KB。我希望能够输入城市名称并更新搜索结果;但这一点我可以自己解决。一个1.5K的条目文件,大约1MB,需要几十毫秒。一个1GB的文件可能需要数十秒,为这个文件保存一个索引可能是值得的,这样可以避免每次都要重新读取它 您可以通过name 您可以通过导航地

我有一个很大的csv文件,有1.5K个条目。每个条目都代表全球一个城市的名称、纬度和经度。在Java中搜索csv的最佳快速方法是什么?我想用所有的条目填充ArrayList,但我认为这很慢(除非我错了)。不,这个文件的大小不会增长,它几乎是100KB。我希望能够输入城市名称并更新搜索结果;但这一点我可以自己解决。

一个1.5K的条目文件,大约1MB,需要几十毫秒。一个1GB的文件可能需要数十秒,为这个文件保存一个索引可能是值得的,这样可以避免每次都要重新读取它

您可以通过
name

您可以通过导航地图添加纬度和经度索引。这将加快按位置查找的速度

加载文件一次需要一点时间,但是每次从磁盘读取文件要慢得多

顺便说一句,您可以有100 TB的数据和数万亿行,要在Java中使用这些数据,您必须具有创造性


简而言之,如果它的内存比你的少得多,那么它就是一个相对较小的文件。

一个1.5K的条目文件,大约1MB,需要几十毫秒的时间。一个1GB的文件可能需要数十秒,为这个文件保存一个索引可能是值得的,这样可以避免每次都要重新读取它

您可以通过
name

您可以通过导航地图添加纬度和经度索引。这将加快按位置查找的速度

加载文件一次需要一点时间,但是每次从磁盘读取文件要慢得多

顺便说一句,您可以有100 TB的数据和数万亿行,要在Java中使用这些数据,您必须具有创造性


简而言之,如果它比你的内存小很多,那么它就是一个相对较小的文件。

1.5K行的城市名称、纬度和经度不是一个很大的文件,它是一个相当小的文件,你如何读取它并不重要,只要你不做任何完全不合理的事情,比如使用无缓冲I/O一次读取一个字节

因此,如果我站在你的立场上,我会继续一行一行地读取文件,构造行对象,然后将它们添加到
ArrayList
。这可能足够快,您可以在每次搜索后丢弃列表,并在每次要搜索时重新加载它。或者,如果你不介意占用一些内存,你当然会想保留它


但在任何情况下,我只会担心性能,如果出于某种深不可测的原因,性能最终成为一个问题。您没有告诉我们您正在生产的产品的性能要求是什么。没有性能要求,没有测量,所有关于性能的讨论通常都是不合理的恐惧,往往会导致过早的优化。

1.5K行的城市名称、纬度和经度不是一个很大的文件,它是一个相当小的文件,你如何阅读它几乎无关紧要,只要您不做任何完全不合理的事情,比如使用无缓冲I/O一次读取一个字节

因此,如果我站在你的立场上,我会继续一行一行地读取文件,构造行对象,然后将它们添加到
ArrayList
。这可能足够快,您可以在每次搜索后丢弃列表,并在每次要搜索时重新加载它。或者,如果你不介意占用一些内存,你当然会想保留它


但在任何情况下,我只会担心性能,如果出于某种深不可测的原因,性能最终成为一个问题。您没有告诉我们您正在生产的产品的性能要求是什么。如果没有性能要求,也没有度量,所有关于性能的讨论通常都是不合理的担心,往往会导致过早的优化。

当您处理大型文本内容时,可能需要进行一些文本处理


请注意字符串连接。通常使用
StringBuffer
StringBuilder
来压缩字符串。

处理大型文本内容时,可能需要进行一些文本操作


请注意字符串连接。通常使用
StringBuffer
StringBuilder
来压缩字符串。

最快的CSV解析器是。有很多方法可以解决这个问题,下面的方法足够灵活,可以以相当快的速度给出结果。以下示例使用一个150 MB的CSV文件,包含130万行,并在约1秒内运行搜索:

首先,创建一个
RowProcessor
在这里,我们扩展了该库附带的现有库之一,即

public class CsvSearch extends RowListProcessor {
    //value to be searched for
    private final String stringToMatch;

    //name of column to match (if you don't have headers)
    private final String columnToMatch;

    //position of column to match
    private int indexToMatch = -1;

    public CsvSearch(String columnToMatch, String stringToMatch){
        this.columnToMatch = columnToMatch;
        this.stringToMatch = stringToMatch.toLowerCase(); //lower case to make the search case-insensitive
    }

    public CsvSearch(int columnToMatch, String stringToMatch){
        this(stringToMatch, null);
        this.indexToMatch = columnToMatch;
    }

    @Override
    public void rowProcessed(String[] row, ParsingContext context) {
        if(indexToMatch == -1) {
            //initializes the index to match
            indexToMatch = context.indexOf(columnToMatch);
        }

        String value = row[indexToMatch];
        if(value != null && value.toLowerCase().contains(stringToMatch)) {
            super.rowProcessed(row, context); // default behavior of the RowListProcessor: add the row into a List.
        }
        // else skip the row.
    }
}
配置解析器并运行 返回的结果如下所示:

[af, parisang, Parisang, 08, null, 33.180704, 67.470836]
[af, qaryeh-ye bid-e parishan, Qaryeh-ye Bid-e Parishan, 06, null, 33.242727, 63.389834]
[ar, parish, Parish, 01, null, -36.518335, -59.633313]
[at, parisdorf, Parisdorf, 03, null, 48.566667, 15.85]
[au, paris creek, Paris Creek, 05, null, -35.216667, 138.8]
[az, hayi paris, Hayi Paris, 21, null, 40.449626, 46.55542]
[az, hay paris, Hay Paris, 21, null, 40.449626, 46.55542]
[az, rousi paris, Rousi Paris, 21, null, 40.435789, 46.510691]
[az, rrusi paris, Rrusi Paris, 21, null, 40.435789, 46.510691]
[bb, parish land, Parish Land, 01, null, 13.0666667, -59.5166667]
... (and many more)
如果您选择要解析的列并忽略不需要的任何内容,则可以进一步提高速度。只需调用
settings.selectFields(“城市”)
在处理文件之前,指示解析器仅为
City
列生成
字符串

希望这有帮助。
披露:我是这个图书馆的作者。它是开源和免费的(Apache v2.0许可证)

最快的CSV解析器是。有很多方法可以解决这个问题,下面的方法足够灵活,可以以相当快的速度给出结果。以下示例使用一个150 MB的CSV文件,包含130万行,并在约1秒内运行搜索:

首先,创建一个
RowProcessor
在这里,我们扩展了该库附带的现有库之一,即

public class CsvSearch extends RowListProcessor {
    //value to be searched for
    private final String stringToMatch;

    //name of column to match (if you don't have headers)
    private final String columnToMatch;

    //position of column to match
    private int indexToMatch = -1;

    public CsvSearch(String columnToMatch, String stringToMatch){
        this.columnToMatch = columnToMatch;
        this.stringToMatch = stringToMatch.toLowerCase(); //lower case to make the search case-insensitive
    }

    public CsvSearch(int columnToMatch, String stringToMatch){
        this(stringToMatch, null);
        this.indexToMatch = columnToMatch;
    }

    @Override
    public void rowProcessed(String[] row, ParsingContext context) {
        if(indexToMatch == -1) {
            //initializes the index to match
            indexToMatch = context.indexOf(columnToMatch);
        }

        String value = row[indexToMatch];
        if(value != null && value.toLowerCase().contains(stringToMatch)) {
            super.rowProcessed(row, context); // default behavior of the RowListProcessor: add the row into a List.
        }
        // else skip the row.
    }
}
配置解析器并运行 返回的结果如下所示:

[af, parisang, Parisang, 08, null, 33.180704, 67.470836]
[af, qaryeh-ye bid-e parishan, Qaryeh-ye Bid-e Parishan, 06, null, 33.242727, 63.389834]
[ar, parish, Parish, 01, null, -36.518335, -59.633313]
[at, parisdorf, Parisdorf, 03, null, 48.566667, 15.85]
[au, paris creek, Paris Creek, 05, null, -35.216667, 138.8]
[az, hayi paris, Hayi Paris, 21, null, 40.449626, 46.55542]
[az, hay paris, Hay Paris, 21, null, 40.449626, 46.55542]
[az, rousi paris, Rousi Paris, 21, null, 40.435789, 46.510691]
[az, rrusi paris, Rrusi Paris, 21, null, 40.435789, 46.510691]
[bb, parish land, Parish Land, 01, null, 13.0666667, -59.5166667]
... (and many more)
如果您选择要解析的列并忽略不需要的任何内容,则可以进一步提高速度。只需调用
settings.selectFields(“城市”)在处理要指示的文件之前