davisusanibar commented on code in PR #215:
URL: https://github.com/apache/arrow-cookbook/pull/215#discussion_r880894231
##########
java/source/create.rst:
##########
@@ -70,6 +70,66 @@ Array of Varchar
[one, two, three]
+Dictionary-Encoded Array of Varchar
+-----------------------------------
+
+In some scenarios `dictionary-encoding`_ a column is useful to save memory.
+
+.. testcode::
+
+ import org.apache.arrow.memory.BufferAllocator;
+ import org.apache.arrow.memory.RootAllocator;
+ import org.apache.arrow.vector.FieldVector;
+ import org.apache.arrow.vector.VarCharVector;
+ import org.apache.arrow.vector.dictionary.Dictionary;
+ import org.apache.arrow.vector.dictionary.DictionaryEncoder;
+ import org.apache.arrow.vector.types.pojo.ArrowType;
+ import org.apache.arrow.vector.types.pojo.DictionaryEncoding;
+
+ import java.nio.charset.StandardCharsets;
+
+ try (BufferAllocator root = new RootAllocator();
+ VarCharVector countries = new VarCharVector("country-dict", root);
+ VarCharVector appUserCountriesUnencoded = new
VarCharVector("app-use-country-dict", root)
+ ) {
+ countries.allocateNew(10);
+ countries.set(0, "Andorra".getBytes(StandardCharsets.UTF_8));
+ countries.set(1, "Cuba".getBytes(StandardCharsets.UTF_8));
+ countries.set(2, "Grecia".getBytes(StandardCharsets.UTF_8));
+ countries.set(3, "Guinea".getBytes(StandardCharsets.UTF_8));
+ countries.set(4, "Islandia".getBytes(StandardCharsets.UTF_8));
+ countries.set(5, "Malta".getBytes(StandardCharsets.UTF_8));
+ countries.set(6, "Tailandia".getBytes(StandardCharsets.UTF_8));
+ countries.set(7, "Uganda".getBytes(StandardCharsets.UTF_8));
+ countries.set(8, "Yemen".getBytes(StandardCharsets.UTF_8));
+ countries.set(9, "Zambia".getBytes(StandardCharsets.UTF_8));
+ countries.setValueCount(10);
+
+ Dictionary countriesDictionary = new Dictionary(countries,
+ new DictionaryEncoding(/*id=*/1L, /*ordered=*/false,
/*indexType=*/new ArrowType.Int(8, true)));
+ System.out.println("Dictionary: " + countriesDictionary);
+
+ appUserCountriesUnencoded.allocateNew(5);
+ appUserCountriesUnencoded.set(0,
"Andorra".getBytes(StandardCharsets.UTF_8));
+ appUserCountriesUnencoded.set(1,
"Guinea".getBytes(StandardCharsets.UTF_8));
+ appUserCountriesUnencoded.set(2,
"Islandia".getBytes(StandardCharsets.UTF_8));
+ appUserCountriesUnencoded.set(3,
"Malta".getBytes(StandardCharsets.UTF_8));
+ appUserCountriesUnencoded.set(4,
"Uganda".getBytes(StandardCharsets.UTF_8));
+ appUserCountriesUnencoded.setValueCount(5);
+ System.out.println("Unencoded data: " + appUserCountriesUnencoded);
+
+ try (FieldVector appUseCountryDictionaryEncoded = (FieldVector)
DictionaryEncoder
Review Comment:
Changed
##########
java/source/io.rst:
##########
@@ -443,3 +443,109 @@ Reading Parquet File
********************
Please check :doc:`Dataset <./dataset>`
+
+Handling Data with Dictionaries
+*******************************
+
+Reading and writing dictionary-encoded data requires separately tracking the
dictionaries.
+
+.. testcode::
+
+ import org.apache.arrow.memory.BufferAllocator;
+ import org.apache.arrow.memory.RootAllocator;
+ import org.apache.arrow.vector.FieldVector;
+ import org.apache.arrow.vector.ValueVector;
+ import org.apache.arrow.vector.VarCharVector;
+ import org.apache.arrow.vector.VectorSchemaRoot;
+ import org.apache.arrow.vector.dictionary.Dictionary;
+ import org.apache.arrow.vector.dictionary.DictionaryEncoder;
+ import org.apache.arrow.vector.dictionary.DictionaryProvider;
+ import org.apache.arrow.vector.ipc.ArrowFileReader;
+ import org.apache.arrow.vector.ipc.ArrowFileWriter;
+ import org.apache.arrow.vector.ipc.message.ArrowBlock;
+ import org.apache.arrow.vector.types.pojo.ArrowType;
+ import org.apache.arrow.vector.types.pojo.DictionaryEncoding;
+
+ import java.io.File;
+ import java.io.FileInputStream;
+ import java.io.FileNotFoundException;
+ import java.io.FileOutputStream;
+ import java.io.IOException;
+ import java.nio.charset.StandardCharsets;
+
+ try (BufferAllocator root = new RootAllocator();
+ VarCharVector countries = new VarCharVector("country-dict", root);
+ VarCharVector appUserCountriesUnencoded = new
VarCharVector("app-use-country-dict", root)
+ ) {
+ countries.allocateNew(10);
+ countries.set(0, "Andorra".getBytes(StandardCharsets.UTF_8));
+ countries.set(1, "Cuba".getBytes(StandardCharsets.UTF_8));
+ countries.set(2, "Grecia".getBytes(StandardCharsets.UTF_8));
+ countries.set(3, "Guinea".getBytes(StandardCharsets.UTF_8));
+ countries.set(4, "Islandia".getBytes(StandardCharsets.UTF_8));
+ countries.set(5, "Malta".getBytes(StandardCharsets.UTF_8));
+ countries.set(6, "Tailandia".getBytes(StandardCharsets.UTF_8));
+ countries.set(7, "Uganda".getBytes(StandardCharsets.UTF_8));
+ countries.set(8, "Yemen".getBytes(StandardCharsets.UTF_8));
+ countries.set(9, "Zambia".getBytes(StandardCharsets.UTF_8));
+ countries.setValueCount(10);
+
+ Dictionary countriesDictionary = new Dictionary(countries,
+ new DictionaryEncoding(/*id=*/123L, /*ordered=*/false,
/*indexType=*/new ArrowType.Int(8, true)));
+ System.out.println("Dictionary: " + countriesDictionary);
+
+ appUserCountriesUnencoded.allocateNew(5);
+ appUserCountriesUnencoded.set(0,
"Andorra".getBytes(StandardCharsets.UTF_8));
+ appUserCountriesUnencoded.set(1,
"Guinea".getBytes(StandardCharsets.UTF_8));
+ appUserCountriesUnencoded.set(2,
"Islandia".getBytes(StandardCharsets.UTF_8));
+ appUserCountriesUnencoded.set(3,
"Malta".getBytes(StandardCharsets.UTF_8));
+ appUserCountriesUnencoded.set(4,
"Uganda".getBytes(StandardCharsets.UTF_8));
+ appUserCountriesUnencoded.setValueCount(5);
+ System.out.println("Unencoded data: " + appUserCountriesUnencoded);
+
+ File file = new File("random_access_file_with_dictionary.arrow");
+ DictionaryProvider.MapDictionaryProvider provider = new
DictionaryProvider.MapDictionaryProvider();
+ provider.put(countriesDictionary);
+ try (FieldVector appUseCountryDictionaryEncoded = (FieldVector)
DictionaryEncoder
Review Comment:
Changed
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]