title: How to serialize timestamp-millis logicalType to avro file using java
body: <p>I am having a scenario where i want to create avro file having timestamp column which will look like <strong>2016-11-16 06:43:19.77</strong></p>
  
  <p>I have used <strong>avro-1.8.2.jar</strong> for writing avro file. This works perfect for primitive types of record but for logical type like timestamp-millis facing issue.</p>
  
  <p>Schema : </p>
  
  <pre><code>{
    "type": "record",
    "name": "MyRecord",
    "namespace": "org.demo",
    "fields": [
      {
        "name": "timestamp_with_logical_type",
        "type": {
          "type": "long",
          "logicalType": "timestamp-millis"
        }
      },
      {
        "name": "timestamp_no_logical_type",
        "type": "long"
      }
    ]
  }
  </code></pre>
  
  <p>Java Code :</p>
  
  <pre><code>       //Instantiating the GenericRecord class.
          GenericRecord record  = new Record(schema);
          long millis = Instant.now().toEpochMilli();
          //Insert data according to schema
          record.put("timestamp_with_logical_type", new Timestamp(millis));
          record.put("timestamp_no_logical_type", millis);
  
          DataFileWriter&lt;GenericRecord&gt; dataFileWriter = null;
          DatumWriter&lt;GenericRecord&gt; datumWriter = new GenericDatumWriter&lt;GenericRecord&gt;(schema);
          dataFileWriter = new DataFileWriter&lt;GenericRecord&gt;(datumWriter);
          dataFileWriter.setCodec(CodecFactory.snappyCodec());
          dataFileWriter.setFlushOnEveryBlock(true);
          dataFileWriter.setSyncInterval(32);
          dataFileWriter.create(SCHEMA,new File("E:\\MyFiles\\Avro_Data\\demo.avro"));
          dataFileWriter.append(record);
          dataFileWriter.close();
  </code></pre>
  
  <p>The above code giving following exception : </p>
  
  <pre><code>Exception in thread "main" org.apache.avro.file.DataFileWriter$AppendWriteException: java.lang.ClassCastException: java.sql.Timestamp cannot be cast to java.lang.CharSequence
      at org.apache.avro.file.DataFileWriter.append(DataFileWriter.java:308)
      at AvroFileReaderWriter.writeToAvro(AvroFileReaderWriter.java:264)
      at AvroFileReaderWriter.main(AvroFileReaderWriter.java:74)
  Caused by: java.lang.ClassCastException: java.sql.Timestamp cannot be cast to java.lang.CharSequence
      at org.apache.avro.generic.GenericDatumWriter.writeString(GenericDatumWriter.java:267)
      at org.apache.avro.generic.GenericDatumWriter.writeString(GenericDatumWriter.java:262)
      at org.apache.avro.generic.GenericDatumWriter.writeWithoutConversion(GenericDatumWriter.java:128)
      at org.apache.avro.generic.GenericDatumWriter.write(GenericDatumWriter.java:75)
      at org.apache.avro.generic.GenericDatumWriter.writeField(GenericDatumWriter.java:166)
      at org.apache.avro.generic.GenericDatumWriter.writeRecord(GenericDatumWriter.java:156)
      at org.apache.avro.generic.GenericDatumWriter.writeWithoutConversion(GenericDatumWriter.java:118)
      at org.apache.avro.generic.GenericDatumWriter.write(GenericDatumWriter.java:75)
      at org.apache.avro.generic.GenericDatumWriter.write(GenericDatumWriter.java:62)
      at org.apache.avro.file.DataFileWriter.append(DataFileWriter.java:302)
      ... 2 more
  </code></pre>