Return-Path: X-Original-To: apmail-avro-user-archive@www.apache.org Delivered-To: apmail-avro-user-archive@www.apache.org Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by minotaur.apache.org (Postfix) with SMTP id 5697517287 for ; Tue, 13 Jan 2015 17:38:44 +0000 (UTC) Received: (qmail 2677 invoked by uid 500); 13 Jan 2015 17:38:45 -0000 Delivered-To: apmail-avro-user-archive@avro.apache.org Received: (qmail 2598 invoked by uid 500); 13 Jan 2015 17:38:45 -0000 Mailing-List: contact user-help@avro.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: user@avro.apache.org Delivered-To: mailing list user@avro.apache.org Received: (qmail 2588 invoked by uid 99); 13 Jan 2015 17:38:45 -0000 Received: from nike.apache.org (HELO nike.apache.org) (192.87.106.230) by apache.org (qpsmtpd/0.29) with ESMTP; Tue, 13 Jan 2015 17:38:45 +0000 X-ASF-Spam-Status: No, hits=-0.7 required=5.0 tests=RCVD_IN_DNSWL_LOW,SPF_PASS X-Spam-Check-By: apache.org Received-SPF: pass (nike.apache.org: domain of joey@cloudera.com designates 74.125.82.171 as permitted sender) Received: from [74.125.82.171] (HELO mail-we0-f171.google.com) (74.125.82.171) by apache.org (qpsmtpd/0.29) with ESMTP; Tue, 13 Jan 2015 17:38:20 +0000 Received: by mail-we0-f171.google.com with SMTP id u56so4301485wes.2 for ; Tue, 13 Jan 2015 09:38:19 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:mime-version:in-reply-to:references:date :message-id:subject:from:to:content-type; bh=MiiqxoRWIFAj1dw5VNThaDOb75psMcrfLzof0tzBbdo=; b=Aou6CbY5rkA3XqRfiHL+ODOyMdbKTFeDUue6vs/9Y6zc6D9z14WrAZE8vhSdmcGtU0 n2O6WJuPNsH0rTKw3j3X2QnFlp6m20aGlvXMyAWTQF7AOTgCIEotAFUEv6Pwvo1Ar42H aMI+xvoCPcu8810yNlNwo57EZGy3uf/IaF7N0yKLlocWyGIN8bG6jDnhC3p/+9ylQMGX aB9wgIF91obehsnJNF1FIGsU2hgPI6c1LDweRlJJlOUr9MKysGM/b0aAH4Z9C4G87Dd+ +1dsLd8nO5+jCCiBCtrk1wCRtBPqf5TTvEVHG7o6CQ/nGXo9TauzeZGium37DXHRrsMD lasw== X-Gm-Message-State: ALoCoQlQbFDKY2wb0eIVBRgq7cLUlk/MuPHjlpTooUROFeXi8+9fTyLSwOkWgK06r9b/SuNIP2XN MIME-Version: 1.0 X-Received: by 10.180.186.8 with SMTP id fg8mr909527wic.40.1421170699231; Tue, 13 Jan 2015 09:38:19 -0800 (PST) Received: by 10.194.77.204 with HTTP; Tue, 13 Jan 2015 09:38:19 -0800 (PST) In-Reply-To: References: Date: Tue, 13 Jan 2015 09:38:19 -0800 Message-ID: Subject: Re: How to change a particular field value after decoding the original byte array? From: Joey Echeverria To: user@avro.apache.org Content-Type: text/plain; charset=UTF-8 X-Virus-Checked: Checked by ClamAV on apache.org You can re-use the GenericRecord object you used before and then just re-serialize. If you've been given the byte array, then you can deserialize to a GenericRecord using something similar to this: reader = new GenericDatumReader(schema); Decoder decoder = DecoderFactory.get().binaryDecoder(bytes, null); GenericRecord record = reader.read(null, decoder); record.put("user_id", 123456L); -Joey On Mon, Jan 12, 2015 at 5:21 PM, Check Peck wrote: > Yes that's what I am looking for. Question is - How do I update the > GenericRecord, I want to update user_id field with some new random long > number.? > > Can you provide an example how can I update GenericRecord and then serialize > it to a new Byte Array? > > On Mon, Jan 12, 2015 at 4:34 PM, Joey Echeverria wrote: >> >> I'm not sure I understand the question. You can certainly deserialize >> the byte array back into a GenericRecord. You can then update that >> GenericRecord and serialize that to a new byte array. >> >> Is that what you're looking for? >> >> -Joey >> >> On Mon, Jan 12, 2015 at 3:42 PM, Check Peck >> wrote: >> > I have an Avro Schema which is like this - >> > >> > { >> > "type":"record", >> > "name":"new_user", >> > "namespace":"com.hello", >> > "fields":[ >> > { >> > "name":"user_id", >> > "type":[ >> > "long", >> > "null" >> > ] >> > }, >> > { >> > "name":"segment", >> > "type":[ >> > "string", >> > "null" >> > ] >> > } >> > ] >> > } >> > >> > I am using my above Avro Schema like this to serialize the data and >> > which >> > gives me a Byte Array and works fine - >> > >> > public static void main(String[] args) throws IOException { >> > Schema schema = new Parser() >> > .parse("{ \"type\":\"record\", \"name\":\"new_user\", >> > \"namespace\":\"com.hello\", \"fields\":[ { \"name\":\"user_id\", >> > \"type\":[ >> > \"long\", \"null\" ] }, { \"name\":\"segment\", \"type\":[ \"string\", >> > \"null\" ] } ] }"); >> > >> > byte[] originalAvrodata = getAvroBinaryData(schema); >> > >> > // how to get newAvroData byte array in which user_id >> > // is change to some other random long number? >> > } >> > >> > private static byte[] getAvroBinaryData(Schema schema) throws >> > IOException { >> > GenericRecord record = new GenericData.Record(schema); >> > record.put("user_id", 123456L); >> > record.put("segment", "hello"); >> > >> > GenericDatumWriter writer = new >> > GenericDatumWriter(schema); >> > ByteArrayOutputStream os = new ByteArrayOutputStream(); >> > >> > Encoder e = EncoderFactory.get().binaryEncoder(os, null); >> > >> > writer.write(record, e); >> > e.flush(); >> > byte[] byteData = os.toByteArray(); >> > return byteData; >> > } >> > >> > >> > >> > I need to decode the `originalAvrodata` byte array and then change the >> > `user_id` field value to some other `long` number and then construct a >> > `newAvroData` byte array using the same schema which should have >> > `user_id` >> > field value to some random `long` number. Is this possible to do by any >> > chance using Avro? >> >> >> >> -- >> Joey Echeverria > > -- Joey Echeverria