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 90E0A99F2 for ; Thu, 9 Feb 2012 00:30:06 +0000 (UTC) Received: (qmail 79605 invoked by uid 500); 9 Feb 2012 00:30:06 -0000 Delivered-To: apmail-avro-user-archive@avro.apache.org Received: (qmail 79533 invoked by uid 500); 9 Feb 2012 00:30:05 -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 79525 invoked by uid 99); 9 Feb 2012 00:30:05 -0000 Received: from nike.apache.org (HELO nike.apache.org) (192.87.106.230) by apache.org (qpsmtpd/0.29) with ESMTP; Thu, 09 Feb 2012 00:30:05 +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 easyvoip@gmail.com designates 209.85.160.43 as permitted sender) Received: from [209.85.160.43] (HELO mail-pw0-f43.google.com) (209.85.160.43) by apache.org (qpsmtpd/0.29) with ESMTP; Thu, 09 Feb 2012 00:29:56 +0000 Received: by pbbro2 with SMTP id ro2so2071191pbb.30 for ; Wed, 08 Feb 2012 16:29:34 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=subject:references:from:content-transfer-encoding:content-type :in-reply-to:message-id:date:cc:to:mime-version:x-mailer; bh=GHE/KZukg82FJm6JKucM2H2Eq714teXo8KIhMe6F9Ow=; b=NtaKzlePffJ8x+M2huUxwUJDct0DVqQXeyaBhpy2xu04JeFJoZJVgC86vEun100vvF //yU80rqCKVD6uBmIqnxvHurzTwmOWusvZQu4hQNALSpjWTEW8aumQWzoVvK99xDX6Gr Ui87Qb6Cxzd1qYfcyIbmX0ny67PG1hCKV1kIs= Received: by 10.68.240.135 with SMTP id wa7mr73530844pbc.7.1328747292596; Wed, 08 Feb 2012 16:28:12 -0800 (PST) Received: from [10.89.251.129] (mobile-166-205-137-119.mycingular.net. [166.205.137.119]) by mx.google.com with ESMTPS id p2sm2059185pbb.14.2012.02.08.16.28.11 (version=TLSv1/SSLv3 cipher=OTHER); Wed, 08 Feb 2012 16:28:11 -0800 (PST) Subject: Re: issue with default values in avro References: <4F3309AC.40407@apache.org> From: easyvoip@gmail.com Content-Transfer-Encoding: 7bit Content-Type: text/plain; charset=us-ascii In-Reply-To: <4F3309AC.40407@apache.org> Message-Id: Date: Wed, 8 Feb 2012 16:25:01 -0800 Cc: "user@avro.apache.org" To: "user@avro.apache.org" Mime-Version: 1.0 (1.0) X-Mailer: iPhone Mail (9A405) X-Virus-Checked: Checked by ClamAV on apache.org Worked like a charm! Thanks Doug! Sent from my iPhone On Feb 8, 2012, at 3:47 PM, Doug Cutting wrote: > Serge, > > Default field values are primarily used when reading records that don't > have those fields, typically an older version of the record. > > GenericData.Record's constructor does not use default values to populate > fields. All fields in an instance created by 'new > GenericData.Record()' are initially null, not set to the default > value of the schema. > > However the GenericRecordBuilder API does use default values to populate > instances. The value of 'new GenericRecordBuilder().set("SSN", > "XXX").build()' should contain what you want. > > Doug > > On 02/08/2012 03:35 PM, Serge Blazhievsky wrote: >> Hi all, >> >> >> >> I am trying to utilize default setting for values in avro schema, >> however, it does not seems to be set. >> >> >> My code is below. >> >> >> List fields2 = new ArrayList(); >> >> org.codehaus.jackson.JsonNode defaultValue2 >> =TextNode.valueOf("default2"); >> >> fields2.add(new Schema.Field("SSN", Schema.create(Type.STRING), >> "Second schema", defaultValue2)); >> >> fields2.add(new Schema.Field("LastName", >> Schema.create(Type.STRING), "Second schema", defaultValue2)); >> >> Schema schema2 = Schema.createRecord("blah", "blah", >> "merced.ssn", false); >> >> schema2.setFields(fields2); >> >> >> System.out.println(schema2.toString(true)); >> >> GenericRecord r=new GenericData.Record(schema2); >> >> >> r.put("SSN", "12345"); >> >> System.out.println(r.get("LastName")); >> >> >> System.out.println(r.toString()); >> >> >>>>>>>>>>>>>>>>>>>>>>>>>>>> >> >> { >> "type" : "record", >> "name" : "blah", >> "namespace" : "merced.ssn", >> "doc" : "blah", >> "fields" : [ { >> "name" : "SSN", >> "type" : "string", >> "doc" : "Second schema", >> "default" : "default2" >> }, { >> "name" : "LastName", >> "type" : "string", >> "doc" : "Second schema", >> "default" : "default2" >> } ] >> } >> null >> {"SSN": "12345", "LastName": null} >> >> >> I expect LastName to be set to default2 >> >> >> Thank you for your help. >> >> Serge >>