0

I am trying to write a custom java.util.Date serializer for titan graph. Here is my titan configuration file:

attributes.allow-all = true
attributes.custom.attribute1.attribute-class = java.util.Date
attributes.custom.attribute1.serializer-class = com.serializer.MyDateSerializer

And My serializer looks like :

public class MyDateSerializer implements AttributeSerializer<Date> {
    @Override
    public void verifyAttribute(Date value) {
        // TODO Auto-generated method stub
    }
    @Override
    public Date convert(Object value) {
        // TODO Auto-generated method stub
        return null;
    }
    @Override
    public Date read(ScanBuffer buffer) {
        // TODO Auto-generated method stub
    }
    @Override
    public void write(WriteBuffer buffer, Date date) {
        // TODO Auto-generated method stub
    }
}

But after opening the TitanGraph , I am getting the following exception:

java.lang.IllegalStateException: Need to set configuration value: root.attributes.custom.serializer-class
at com.google.common.base.Preconditions.checkState(Preconditions.java:177)
at com.thinkaurelius.titan.diskstorage.configuration.ConfigOption.get(ConfigOption.java:158)
at com.thinkaurelius.titan.diskstorage.configuration.BasicConfiguration.get(BasicConfiguration.java:56)
at com.thinkaurelius.titan.graphdb.configuration.GraphDatabaseConfiguration.<init>(GraphDatabaseConfiguration.java:1334)
at com.thinkaurelius.titan.core.TitanFactory.open(TitanFactory.java:91)
at com.thinkaurelius.titan.core.TitanFactory.open(TitanFactory.java:71)

Here is how I am trying to read the configuration from the property file :

        BaseConfiguration configuration = new BaseConfiguration();
        Properties properties = new Properties();
        properties.load(getClass().getResourceAsStream(
                "/titanConfiguration.properties"));
        Set<Entry<Object, Object>> entrySet = properties.entrySet();

        for (Entry<Object, Object> entry : entrySet) {
            configuration.setProperty(entry.getKey().toString(), entry
                    .getValue().toString());
        }

I have gone through the titan graph documentation for the version 0.5.2, but Iam not able to figure out the problem. I have gone through similar posts also, but still I am not able to resolve this. Has anybody faced this problem before?

If I try to persist the java.util.Date inside titan vertex as follows :

vertex.setProperty("myDate",new Date());

And then when I try to retrieve the Date from vertex as :

((Date)vertex.getProperty("myDate"));

I get the following exception :

java.lang.ClassCastException: java.lang.String cannot be cast to java.util.Date
at org.rampal.Transaction.getUsers(Transaction.java:179)
at org.rampal.Controller.getUsers(Controller.java:71)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:483)
user3244615
  • 330
  • 1
  • 15

1 Answers1

0

You're not going to be able to override the date handling as there is already a default handler included with titan.

Using your instructions and Titan 0.5.2 you get:

java.lang.IllegalArgumentException: DataType has already been registered: class java.util.Date

The error message you got is cryptic and doesn't happen for me unless I specify a custom attribute that doesn't start at the number 1. For instance:

attributes.allow-all = true
attributes.custom.attribute10.attribute-class = java.util.Date
attributes.custom.attribute10.serializer-class = com.serializer.MyDateSerializer

Will cause:

java.lang.IllegalStateException: Need to set configuration value: root.attributes.custom.serializer-class
Bryn
  • 487
  • 3
  • 11
  • Thanks for your reply. When I am saving date into the vertex (vertex.setProperty("date", new Date())), while fetching the date I am getting classcast exception stating that java.lang.String cannot be cast to java.util.Date. That's why I was motivated to write a serializer. Any idea what could have gone wrong while saving new Date() directly in the vertex? – user3244615 Mar 09 '15 at 11:39
  • Do you have a stack trace? – Bryn Mar 09 '15 at 13:44
  • org.rampal.Transaction.getUsers(Transaction.java:179) is not in the Titan code base. Please check the source code there to see why you are getting the exception. – Bryn Mar 10 '15 at 13:30
  • I think I found the problem. It's totally my mistake. I have to set the data type of the key to Object.class. Currently it's Date.class. I haven't tried it, but will post my result after checking. Also I want some suggestion on using Date versus using Long(timestamp) for saving the date inside the titan graph. Which one would be better and why? – user3244615 Mar 11 '15 at 05:43