You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@avro.apache.org by Pradeep Kanchgar <Pr...@lntinfotech.com> on 2013/01/22 06:33:38 UTC

Getting started with java

Hi,

I'm trying the example from http://avro.apache.org/docs/1.7.3/gettingstartedjava.html  site.

Serializing and deserializing with code generation

Schema :  user.avsc

{"namespace": "example.avro",
"type": "record",
"name": "User",
"fields": [
     {"name": "name", "type": "string"},
     {"name": "favorite_number",  "type": ["int", "null"]},
     {"name": "favorite_color", "type": ["string", "null"]}
]
}


And compiled schema as



java -jar /home/user/Avro/avro-tools-1.7.3.jar compile schema user.avsc .

It created  "User.java" file in example.avro folder


**********************
SpecificMain.java
------
package example.avro;

import java.io.File;
import java.io.IOException;

import org.apache.avro.file.DataFileReader;
import org.apache.avro.file.DataFileWriter;
import org.apache.avro.io.DatumReader;
import org.apache.avro.io.DatumWriter;
import org.apache.avro.specific.SpecificDatumReader;
import org.apache.avro.specific.SpecificDatumWriter;

import example.avro.User;

public class SpecificMain {
        public static void main(String[] args) throws IOException {
                User user1 = new User();
                user1.setName("Alyssa");
                user1.setFavoriteNumber(256);
                // Leave favorite color null

                // Alternate constructor
                User user2 = new User("Ben", 7, "red");

                // Construct via builder
                User user3 = User.newBuilder()
                                     .setName("Charlie")
                                     .setFavoriteColor("blue")
                                     .setFavoriteNumber(null)
                                     .build();

                // Serialize user1 and user2 to disk
                File file = new File("users.avro");
                DatumWriter<User> userDatumWriter = new SpecificDatumWriter<User>(User.class);
                DataFileWriter<User> dataFileWriter = new DataFileWriter<User>(userDatumWriter);
                dataFileWriter.create(user1.getSchema(), file);
                dataFileWriter.append(user1);
                dataFileWriter.append(user2);
                dataFileWriter.append(user3);
                dataFileWriter.close();

                // Deserialize Users from disk
                DatumReader<User> userDatumReader = new SpecificDatumReader<User>(User.class);
                DataFileReader<User> dataFileReader = new DataFileReader<User>(file, userDatumReader);
                User user = null;
                while (dataFileReader.hasNext()) {
                        // Reuse user object by passing it to next(). This saves us from
                        // allocating and garbage collecting many objects for files with
                        // many items.
                        user = dataFileReader.next(user);
                        System.out.println(user);
                }

        }
}
**************
I placed this above SpecificMain.java in example.avro folder and compiled but it is giving me error like
--------------

SpecificMain.java:17: cannot find symbol
symbol  : class User
location: class example.avro.SpecificMain
                User user1 = new User();
                ^
--------------


Am I doing it right? Or Did I miss anything ?


Thanks & Regards,
Pradeep C Kanchgar


________________________________
The contents of this e-mail and any attachment(s) may contain confidential or privileged information for the intended recipient(s). Unintended recipients are prohibited from taking action on the basis of information in this e-mail and using or disseminating the information, and must notify the sender and delete it from their system. L&T Infotech will not accept responsibility or liability for the accuracy or completeness of, or the presence of any virus or disabling code in this e-mail"