如何导入".csv"数据文件到Redis数据库中 [英] how to import a ".csv " data-file into the Redis database

查看:1032
本文介绍了如何导入".csv"数据文件到Redis数据库中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何将.csv数据文件导入Redis数据库? .csv文件中包含"id,时间,纬度,经度"列.您能为我建议导入CSV文件并能够执行空间查询的最佳方法吗?

How do I import a .csv data-file into the Redis database? The .csv file has "id, time, latitude, longitude" columns in it. Can you suggest to me the best possible way to import the CSV file and be able to perform spatial queries?

推荐答案

这是一个非常广泛的问题,因为我们不知道您想要的数据结构.您期望什么查询,等等.为了解决您的问题,您需要:

This is a very broad question, because we don't know what data structure you want to have. What queries do you expect, etc. In order to solve your question you need:

  1. 写下预期的查询.写下预期的分区.该文件是您的完整数据集吗?

  1. Write down expected queries. Write down expected partitions. Is this file your complete dataset?

写下您的数据结构.很大程度上取决于p1的答案.

Write down your data structure. It will heavily depend on answers from p1.

选择您最熟悉的任何(脚本)语言.加载文件,在CSV库中处理它,从p2映射到您的数据结构,推送到Redis.您可以使用客户端库或redis-cli.

Pick any (scripting) language you are most comfortable with. Load your file, process it in CSV library, map to your data structure from p2, push to Redis. You can do the latter with client library or with redis-cli.

例如,如果要将数据放置在排序集中,其中id是zset的键,时间戳是score,而lat,lon是有效负载,则可以执行以下操作:

The if for example, you want to lay your data in sorted sets where your id is zset's key, timestamp is score and lat,lon is the payload, you can do this:

$ cat data.csv
id1,1528961481,45.0,45.0
id1,1528961482,45.1,45.1
id2,1528961483,50.0,50.0
id2,1528961484,50.1,50.0

$ cat data.csv
id1,1528961481,45.0,45.0
id1,1528961482,45.1,45.1
id2,1528961483,50.0,50.0
id2,1528961484,50.1,50.0

cat data.csv | awk -F "," '{print $1" "$2" "$3" "$4}' | xargs -n4 sh -c 'redis-cli -p 6370 zadd $1 $2 "$3,$4"' sh

127.0.0.1:6370> zrange id2 0 -1
1)"50.0,50.0"
2)"50.1,50.0"

127.0.0.1:6370> zrange id2 0 -1
1) "50.0,50.0"
2) "50.1,50.0"

这篇关于如何导入".csv"数据文件到Redis数据库中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆