Discussion:
Random access in an avro file
k***@public.gmane.org
2013-07-01 15:37:15 UTC
Permalink
Hello,

Is it possible to have random access to a record in an avro file? For
instance, if I have an avro file with a schema containing four
records: *employee
id, name, address *and* phone*. While reading the file, is there any way at
all to directly jump to a record with employee id 100 instead of having to
scan the whole file every single time and filtering out records?

Thanks for the help.
--
Swarnim
Doug Cutting
2013-07-01 17:05:02 UTC
Permalink
Avro data files do not generally support random access.

SortedKeyValueFile supports random access by key.

http://avro.apache.org/docs/current/api/java/org/apache/avro/hadoop/file/SortedKeyValueFile.Reader.html
"The SortedKeyValueFile is a directory with two files, named 'data'
and 'index'. The 'data' file is an ordinary Avro container file with
records. Each record has exactly two fields, 'key' and 'value'. The
keys are sorted lexicographically. The 'index' file is a small Avro
container file mapping keys in the 'data' file to their byte
positions. The index file is intended to fit in memory, so it should
remain small. There is one entry in the index file for each data block
in the Avro container file."

Doug
Post by k***@public.gmane.org
Hello,
Is it possible to have random access to a record in an avro file? For
employee id, name, address and phone. While reading the file, is there any
way at all to directly jump to a record with employee id 100 instead of
having to scan the whole file every single time and filtering out records?
Thanks for the help.
--
Swarnim
k***@public.gmane.org
2013-07-01 17:26:37 UTC
Permalink
Thanks for the reply Doug.

Out of curiosity, is maintaining sync markers while writing the file and
then passing these markers to the readers while reading not a good way to
achieve random access in avro? Atleast that's what my understanding from
reading the javadoc[1] was, which could be flawed.

[1]
http://avro.apache.org/docs/1.3.3/api/java/org/apache/avro/file/DataFileWriter.html#sync()
Post by Doug Cutting
Avro data files do not generally support random access.
SortedKeyValueFile supports random access by key.
http://avro.apache.org/docs/current/api/java/org/apache/avro/hadoop/file/SortedKeyValueFile.Reader.html
"The SortedKeyValueFile is a directory with two files, named 'data'
and 'index'. The 'data' file is an ordinary Avro container file with
records. Each record has exactly two fields, 'key' and 'value'. The
keys are sorted lexicographically. The 'index' file is a small Avro
container file mapping keys in the 'data' file to their byte
positions. The index file is intended to fit in memory, so it should
remain small. There is one entry in the index file for each data block
in the Avro container file."
Doug
Post by k***@public.gmane.org
Hello,
Is it possible to have random access to a record in an avro file? For
employee id, name, address and phone. While reading the file, is there
any
Post by k***@public.gmane.org
way at all to directly jump to a record with employee id 100 instead of
having to scan the whole file every single time and filtering out
records?
Post by k***@public.gmane.org
Thanks for the help.
--
Swarnim
--
Swarnim
Doug Cutting
2013-07-01 17:51:26 UTC
Permalink
Post by k***@public.gmane.org
Out of curiosity, is maintaining sync markers while writing the file and
then passing these markers to the readers while reading not a good way to
achieve random access in avro?
Yes, seeking to the position of a sync marker is possible. This is
what SortedKeyValueFile does. You need to store the list of positions
of sync markers, and if seek is to a column value rather than a row
number, then you need to store these values (keys) with the positions.
Those are what's in SortedKeyValueFile's "index" file.

Doug
k***@public.gmane.org
2013-07-01 18:50:10 UTC
Permalink
Thanks again Doug. SortedKeyValueFile looks really promising and seems to
fit our use case well.

One last thing I was concerned about was the performance of maintaining the
sorted order in the file. Especially because in our case the file might get
pretty large(hundred thousands to million). If there is a limit on the file
size to achieve maximum performance, we can possibly think about closing
the file and start writing to another file once we start to hit that limit.
Post by Doug Cutting
Post by k***@public.gmane.org
Out of curiosity, is maintaining sync markers while writing the file and
then passing these markers to the readers while reading not a good way to
achieve random access in avro?
Yes, seeking to the position of a sync marker is possible. This is
what SortedKeyValueFile does. You need to store the list of positions
of sync markers, and if seek is to a column value rather than a row
number, then you need to store these values (keys) with the positions.
Those are what's in SortedKeyValueFile's "index" file.
Doug
--
Swarnim
k***@public.gmane.org
2013-07-01 22:22:42 UTC
Permalink
I guess I will answer this question myself. It seems like the file expects
the records to be entered in a sorted order inside of it doing the sorting
internally[1]. I don't think it should hurt us but honestly was a little
surprising. It feels like this should be javadoc'ed somewhere that it is
the responsibility of the consumers to sort the records themselves by the
given key before appending to the file. Otherwise, a very useful addition
to the avro library! :)

[1]
http://grepcode.com/file/repo1.maven.org/maven2/org.apache.avro/avro-mapred/1.7.2/org/apache/avro/hadoop/file/SortedKeyValueFile.java#536
Post by k***@public.gmane.org
Thanks again Doug. SortedKeyValueFile looks really promising and seems to
fit our use case well.
One last thing I was concerned about was the performance
of maintaining the sorted order in the file. Especially because in our case
the file might get pretty large(hundred thousands to million). If there is
a limit on the file size to achieve maximum performance, we can possibly
think about closing the file and start writing to another file once we
start to hit that limit.
Post by Doug Cutting
Post by k***@public.gmane.org
Out of curiosity, is maintaining sync markers while writing the file and
then passing these markers to the readers while reading not a good way
to
Post by k***@public.gmane.org
achieve random access in avro?
Yes, seeking to the position of a sync marker is possible. This is
what SortedKeyValueFile does. You need to store the list of positions
of sync markers, and if seek is to a column value rather than a row
number, then you need to store these values (keys) with the positions.
Those are what's in SortedKeyValueFile's "index" file.
Doug
--
Swarnim
--
Swarnim
Doug Cutting
2013-07-01 22:52:00 UTC
Permalink
It seems like the file expects the records to be entered in a sorted order
inside of it doing the sorting internally[1].
Yes, that's right. The normal use is as the output of a MapReduce
job, where key/value pairs arrive in sorted order. If you think the
javadoc should make this more explicit, please file a bug report.

Thanks,

Doug
Scott Carey
2013-07-02 18:59:39 UTC
Permalink
There are a couple other index formats that could apply. You can seek to a
sync marker and scan from there. For example Avro files can be a target
for Elephant Twin
(http://www.slideshare.net/squarecog/flexible-insitu-indexing-for-hadoop-via
-elephant-twin ; http://gitrep.com/users/twitter/repos/elephant-twin).

However, that is a light-weight index for marking which blocks have records
that match the index, it does not locate the exact record.

From: "kulkarni.swarnim-***@public.gmane.org" <kulkarni.swarnim-***@public.gmane.org>
Reply-To: "user-A0UZ4V/***@public.gmane.org" <user-A0UZ4V/***@public.gmane.org>
Date: Monday, July 1, 2013 10:26 AM
To: user <user-A0UZ4V/***@public.gmane.org>
Subject: Re: Random access in an avro file

Thanks for the reply Doug.

Out of curiosity, is maintaining sync markers while writing the file and
then passing these markers to the readers while reading not a good way to
achieve random access in avro? Atleast that's what my understanding from
reading the javadoc[1] was, which could be flawed.

[1]
http://avro.apache.org/docs/1.3.3/api/java/org/apache/avro/file/DataFileWrit
er.html#sync()
Post by Doug Cutting
Avro data files do not generally support random access.
SortedKeyValueFile supports random access by key.
http://avro.apache.org/docs/current/api/java/org/apache/avro/hadoop/file/Sorte
dKeyValueFile.Reader.html
"The SortedKeyValueFile is a directory with two files, named 'data'
and 'index'. The 'data' file is an ordinary Avro container file with
records. Each record has exactly two fields, 'key' and 'value'. The
keys are sorted lexicographically. The 'index' file is a small Avro
container file mapping keys in the 'data' file to their byte
positions. The index file is intended to fit in memory, so it should
remain small. There is one entry in the index file for each data block
in the Avro container file."
Doug
Post by k***@public.gmane.org
Hello,
Is it possible to have random access to a record in an avro file? For
employee id, name, address and phone. While reading the file, is there any
way at all to directly jump to a record with employee id 100 instead of
having to scan the whole file every single time and filtering out records?
Thanks for the help.
--
Swarnim
--
Swarnim
Loading...