Wikidata-Toolkit
Wikidata-Toolkit copied to clipboard
Creating a property on a private wikibase instance
I am trying to create properties using the Java API:
public static void main(String[] args) throws Exception {
BasicApiConnection connection = new BasicApiConnection(siteIri);
connection.login(USERNAME, PASSWORD);
if (!connection.isLoggedIn()) {
throw new Exception("Login failed");
}
WikibaseDataEditor wbde = new WikibaseDataEditor(connection, siteIri);
wbde.setEditAsBot(true);
PropertyDocumentBuilder propertyDocumentBuilder = PropertyDocumentBuilder.forPropertyIdAndDatatype(PropertyIdValue.NULL, DatatypeIdValue.DT_PROPERTY);
propertyDocumentBuilder.withLabel("Label text " + uuid, "en");
propertyDocumentBuilder.withDescription("Description " + uuid , "en");
propertyDocumentBuilder.withAlias("My alias " + uuid, "en");
PropertyDocument propertyDocument = propertyDocumentBuilder.build();
wbde.createPropertyDocument(propertyDocument, "bot summary " + uuid);
}
It does create the property but I end up with a NullPointer which is not really clear in itself to me:
SLF4J: Class path contains multiple SLF4J bindings.
SLF4J: Found binding in [jar:file:/Users/jasperkoehorst/GitLab/wikidata/libs/RDFSimpleCon.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: Found binding in [jar:file:/Users/jasperkoehorst/.gradle/caches/modules-2/files-2.1/org.slf4j/slf4j-log4j12/1.7.10/b3eeae7d1765f988a1f45ea81517191315c69c9e/slf4j-log4j12-1.7.10.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: See http://www.slf4j.org/codes.html#multiple_bindings for an explanation.
SLF4J: Actual binding is of type [org.slf4j.impl.Log4jLoggerFactory]
log4j:WARN No appenders could be found for logger (org.wikidata.wdtk.wikibaseapi.ApiConnection).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
Exception in thread "main" com.fasterxml.jackson.databind.exc.InvalidDefinitionException: Cannot construct instance of `org.wikidata.wdtk.datamodel.implementation.PropertyDocumentImpl`, problem: `java.lang.NullPointerException`
at [Source: UNKNOWN; line: -1, column: -1]
at com.fasterxml.jackson.databind.exc.InvalidDefinitionException.from(InvalidDefinitionException.java:67)
at com.fasterxml.jackson.databind.DeserializationContext.instantiationException(DeserializationContext.java:1608)
at com.fasterxml.jackson.databind.deser.std.StdValueInstantiator.wrapAsJsonMappingException(StdValueInstantiator.java:484)
at com.fasterxml.jackson.databind.deser.std.StdValueInstantiator.rewrapCtorProblem(StdValueInstantiator.java:503)
at com.fasterxml.jackson.databind.deser.std.StdValueInstantiator.createFromObjectWith(StdValueInstantiator.java:285)
at com.fasterxml.jackson.databind.deser.ValueInstantiator.createFromObjectWith(ValueInstantiator.java:229)
at com.fasterxml.jackson.databind.deser.impl.PropertyBasedCreator.build(PropertyBasedCreator.java:195)
at com.fasterxml.jackson.databind.deser.BeanDeserializer._deserializeUsingPropertyBased(BeanDeserializer.java:488)
at com.fasterxml.jackson.databind.deser.BeanDeserializerBase.deserializeFromObjectUsingNonDefault(BeanDeserializerBase.java:1287)
at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserializeFromObject(BeanDeserializer.java:326)
at com.fasterxml.jackson.databind.deser.BeanDeserializer._deserializeOther(BeanDeserializer.java:194)
at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserialize(BeanDeserializer.java:161)
at com.fasterxml.jackson.databind.jsontype.impl.AsPropertyTypeDeserializer._deserializeTypedForId(AsPropertyTypeDeserializer.java:130)
at com.fasterxml.jackson.databind.jsontype.impl.AsPropertyTypeDeserializer.deserializeTypedFromObject(AsPropertyTypeDeserializer.java:97)
at com.fasterxml.jackson.databind.deser.AbstractDeserializer.deserializeWithType(AbstractDeserializer.java:254)
at com.fasterxml.jackson.databind.deser.impl.TypeWrappedDeserializer.deserialize(TypeWrappedDeserializer.java:68)
at com.fasterxml.jackson.databind.ObjectReader._bindAndClose(ObjectReader.java:1611)
at com.fasterxml.jackson.databind.ObjectReader.readValue(ObjectReader.java:1295)
at org.wikidata.wdtk.wikibaseapi.WbEditingAction.parseJsonResponse(WbEditingAction.java:783)
at org.wikidata.wdtk.wikibaseapi.WbEditingAction.getEntityDocumentFromResponse(WbEditingAction.java:761)
at org.wikidata.wdtk.wikibaseapi.WbEditingAction.wbEditEntity(WbEditingAction.java:296)
at org.wikidata.wdtk.wikibaseapi.WikibaseDataEditor.createPropertyDocument(WikibaseDataEditor.java:271)
at wikidata.App.main(App.java:55)
Caused by: java.lang.NullPointerException
at org.wikidata.wdtk.datamodel.implementation.DatatypeIdImpl.getDatatypeIriFromJsonDatatype(DatatypeIdImpl.java:124)
at org.wikidata.wdtk.datamodel.implementation.PropertyDocumentImpl.<init>(PropertyDocumentImpl.java:116)
at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.base/java.lang.reflect.Constructor.newInstance(Constructor.java:490)
at com.fasterxml.jackson.databind.introspect.AnnotatedConstructor.call(AnnotatedConstructor.java:124)
at com.fasterxml.jackson.databind.deser.std.StdValueInstantiator.createFromObjectWith(StdValueInstantiator.java:283)
... 18 more
when disabling the UUID and run the program twice I get the following message:
SLF4J: Class path contains multiple SLF4J bindings.
SLF4J: Found binding in [jar:file:/Users/jasperkoehorst/GitLab/wikidata/libs/RDFSimpleCon.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: Found binding in [jar:file:/Users/jasperkoehorst/.gradle/caches/modules-2/files-2.1/org.slf4j/slf4j-log4j12/1.7.10/b3eeae7d1765f988a1f45ea81517191315c69c9e/slf4j-log4j12-1.7.10.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: See http://www.slf4j.org/codes.html#multiple_bindings for an explanation.
SLF4J: Actual binding is of type [org.slf4j.impl.Log4jLoggerFactory]
log4j:WARN No appenders could be found for logger (org.wikidata.wdtk.wikibaseapi.ApiConnection).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
Exception in thread "main" org.wikidata.wdtk.wikibaseapi.apierrors.MediaWikiApiErrorException: [failed-save] The save has failed.
at org.wikidata.wdtk.wikibaseapi.apierrors.MediaWikiApiErrorHandler.throwMediaWikiApiErrorException(MediaWikiApiErrorHandler.java:65)
at org.wikidata.wdtk.wikibaseapi.ApiConnection.checkErrors(ApiConnection.java:466)
at org.wikidata.wdtk.wikibaseapi.ApiConnection.sendJsonRequest(ApiConnection.java:406)
at org.wikidata.wdtk.wikibaseapi.WbEditingAction.performAPIAction(WbEditingAction.java:716)
at org.wikidata.wdtk.wikibaseapi.WbEditingAction.wbEditEntity(WbEditingAction.java:294)
at org.wikidata.wdtk.wikibaseapi.WikibaseDataEditor.createPropertyDocument(WikibaseDataEditor.java:271)
at wikidata.App.main(App.java:55)
I assume this is the case due to an already existing property with the exact same label and description? However I am not certain since the message is not really clear in itself...
Hi! Thank you for the bug report. I'm not sure of what is happening exactly for your first problem (@wetneb do you have an idea?). About the second error, indeed, Wikibase enforces that the (label, description) couples are unique.
For the first bug it looks like the datatype of the property as returned by Wikibase is not understood. It would be worth adding some proper error handling there to understand which datatype value caused this, and fix it accordingly. We might also want to update the list of supported datatypes to include the recently added ones.
Hi, I am newcomer to wikidata, configured the api successfully in eclipse, run all examples from tutorial, but I have not been able to connect to our private wiki, and obtained the same results I just obtained with wikidata.org and test.wikidata.org.
I tried the published code, and obtained the following result:
log4j:WARN No appenders could be found for logger (org.wikidata.wdtk.wikibaseapi.ApiConnection).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
Exception in thread "main" java.lang.NullPointerException
at java.net.URLEncoder.encode(URLEncoder.java:204)
at org.wikidata.wdtk.wikibaseapi.ApiConnection.getQueryString(ApiConnection.java:708)
at org.wikidata.wdtk.wikibaseapi.ApiConnection.sendRequest(ApiConnection.java:431)
at org.wikidata.wdtk.wikibaseapi.ApiConnection.sendJsonRequest(ApiConnection.java:404)
at org.wikidata.wdtk.wikibaseapi.ApiConnection.confirmLogin(ApiConnection.java:577)
at org.wikidata.wdtk.wikibaseapi.BasicApiConnection.confirmLogin(BasicApiConnection.java:114)
at org.wikidata.wdtk.wikibaseapi.ApiConnection.login(ApiConnection.java:298)
at org.wikidata.wdtk.wikibaseapi.BasicApiConnection.login(BasicApiConnection.java:105)
at wikiloader.WikiPstReader.main(WikiPstReader.java:45)
when ussing the examplerhelper function provided with the api, I obtained the following result:
2019-08-23 14:01:12 WARN - Error: API request returned response code 404
2019-08-23 14:01:12 ERROR - Error when trying to fetch token: java.io.FileNotFoundException: https://wikidata.pst.ag/wiki/Main_Page
Exception in thread "main" java.lang.NullPointerException
at java.net.URLEncoder.encode(URLEncoder.java:204)
at org.wikidata.wdtk.wikibaseapi.ApiConnection.getQueryString(ApiConnection.java:708)
at org.wikidata.wdtk.wikibaseapi.ApiConnection.sendRequest(ApiConnection.java:431)
at org.wikidata.wdtk.wikibaseapi.ApiConnection.sendJsonRequest(ApiConnection.java:404)
at org.wikidata.wdtk.wikibaseapi.ApiConnection.confirmLogin(ApiConnection.java:577)
at org.wikidata.wdtk.wikibaseapi.BasicApiConnection.confirmLogin(BasicApiConnection.java:114)
at org.wikidata.wdtk.wikibaseapi.ApiConnection.login(ApiConnection.java:298)
at org.wikidata.wdtk.wikibaseapi.BasicApiConnection.login(BasicApiConnection.java:105)
at wikiloader.WikiPstReader.main(WikiPstReader.java:45)
any recommendation to solve the issue?, what is the best way to get access to a given wikidata page?
Luis
Instead of using https://wikidata.pst.ag/wiki/Main_Page
as the URL of your Wikibase instance, you should use the MediaWiki API endpoint, which should be https://wikidata.pst.ag/w/api.php
.
Dear wetneb,
thanks for the recommendation, I could get the connection, and write the property values, however after ending the task, it generates another error, and I wonder what could be the reason?
The last instruction executed was:
wbde.createPropertyDocument(propertyDocument, "Luis Test bot summary ");
and I think the problem is there.
Exception in thread "main" org.wikidata.wdtk.wikibaseapi.apierrors.MediaWikiApiErrorException: [failed-save] The save has failed. at org.wikidata.wdtk.wikibaseapi.apierrors.MediaWikiApiErrorHandler.throwMediaWikiApiErrorException(MediaWikiApiErrorHandler.java:65) at org.wikidata.wdtk.wikibaseapi.ApiConnection.checkErrors(ApiConnection.java:466) at org.wikidata.wdtk.wikibaseapi.ApiConnection.sendJsonRequest(ApiConnection.java:406) at org.wikidata.wdtk.wikibaseapi.WbEditingAction.performAPIAction(WbEditingAction.java:716) at org.wikidata.wdtk.wikibaseapi.WbEditingAction.wbEditEntity(WbEditingAction.java:294) at org.wikidata.wdtk.wikibaseapi.WikibaseDataEditor.createPropertyDocument(WikibaseDataEditor.java:271) at wikiloader.WikiPstReader.main(WikiPstReader.java:88)
Luis
I am trying to create properties using the Java API:
public static void main(String[] args) throws Exception { BasicApiConnection connection = new BasicApiConnection(siteIri); connection.login(USERNAME, PASSWORD); if (!connection.isLoggedIn()) { throw new Exception("Login failed"); } WikibaseDataEditor wbde = new WikibaseDataEditor(connection, siteIri); wbde.setEditAsBot(true); PropertyDocumentBuilder propertyDocumentBuilder = PropertyDocumentBuilder.forPropertyIdAndDatatype(PropertyIdValue.NULL, DatatypeIdValue.DT_PROPERTY); propertyDocumentBuilder.withLabel("Label text " + uuid, "en"); propertyDocumentBuilder.withDescription("Description " + uuid , "en"); propertyDocumentBuilder.withAlias("My alias " + uuid, "en"); PropertyDocument propertyDocument = propertyDocumentBuilder.build(); wbde.createPropertyDocument(propertyDocument, "bot summary " + uuid); }
It does create the property but I end up with a NullPointer which is not really clear in itself to me:
SLF4J: Class path contains multiple SLF4J bindings. SLF4J: Found binding in [jar:file:/Users/jasperkoehorst/GitLab/wikidata/libs/RDFSimpleCon.jar!/org/slf4j/impl/StaticLoggerBinder.class] SLF4J: Found binding in [jar:file:/Users/jasperkoehorst/.gradle/caches/modules-2/files-2.1/org.slf4j/slf4j-log4j12/1.7.10/b3eeae7d1765f988a1f45ea81517191315c69c9e/slf4j-log4j12-1.7.10.jar!/org/slf4j/impl/StaticLoggerBinder.class] SLF4J: See http://www.slf4j.org/codes.html#multiple_bindings for an explanation. SLF4J: Actual binding is of type [org.slf4j.impl.Log4jLoggerFactory] log4j:WARN No appenders could be found for logger (org.wikidata.wdtk.wikibaseapi.ApiConnection). log4j:WARN Please initialize the log4j system properly. log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info. Exception in thread "main" com.fasterxml.jackson.databind.exc.InvalidDefinitionException: Cannot construct instance of `org.wikidata.wdtk.datamodel.implementation.PropertyDocumentImpl`, problem: `java.lang.NullPointerException` at [Source: UNKNOWN; line: -1, column: -1] at com.fasterxml.jackson.databind.exc.InvalidDefinitionException.from(InvalidDefinitionException.java:67) at com.fasterxml.jackson.databind.DeserializationContext.instantiationException(DeserializationContext.java:1608) at com.fasterxml.jackson.databind.deser.std.StdValueInstantiator.wrapAsJsonMappingException(StdValueInstantiator.java:484) at com.fasterxml.jackson.databind.deser.std.StdValueInstantiator.rewrapCtorProblem(StdValueInstantiator.java:503) at com.fasterxml.jackson.databind.deser.std.StdValueInstantiator.createFromObjectWith(StdValueInstantiator.java:285) at com.fasterxml.jackson.databind.deser.ValueInstantiator.createFromObjectWith(ValueInstantiator.java:229) at com.fasterxml.jackson.databind.deser.impl.PropertyBasedCreator.build(PropertyBasedCreator.java:195) at com.fasterxml.jackson.databind.deser.BeanDeserializer._deserializeUsingPropertyBased(BeanDeserializer.java:488) at com.fasterxml.jackson.databind.deser.BeanDeserializerBase.deserializeFromObjectUsingNonDefault(BeanDeserializerBase.java:1287) at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserializeFromObject(BeanDeserializer.java:326) at com.fasterxml.jackson.databind.deser.BeanDeserializer._deserializeOther(BeanDeserializer.java:194) at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserialize(BeanDeserializer.java:161) at com.fasterxml.jackson.databind.jsontype.impl.AsPropertyTypeDeserializer._deserializeTypedForId(AsPropertyTypeDeserializer.java:130) at com.fasterxml.jackson.databind.jsontype.impl.AsPropertyTypeDeserializer.deserializeTypedFromObject(AsPropertyTypeDeserializer.java:97) at com.fasterxml.jackson.databind.deser.AbstractDeserializer.deserializeWithType(AbstractDeserializer.java:254) at com.fasterxml.jackson.databind.deser.impl.TypeWrappedDeserializer.deserialize(TypeWrappedDeserializer.java:68) at com.fasterxml.jackson.databind.ObjectReader._bindAndClose(ObjectReader.java:1611) at com.fasterxml.jackson.databind.ObjectReader.readValue(ObjectReader.java:1295) at org.wikidata.wdtk.wikibaseapi.WbEditingAction.parseJsonResponse(WbEditingAction.java:783) at org.wikidata.wdtk.wikibaseapi.WbEditingAction.getEntityDocumentFromResponse(WbEditingAction.java:761) at org.wikidata.wdtk.wikibaseapi.WbEditingAction.wbEditEntity(WbEditingAction.java:296) at org.wikidata.wdtk.wikibaseapi.WikibaseDataEditor.createPropertyDocument(WikibaseDataEditor.java:271) at wikidata.App.main(App.java:55) Caused by: java.lang.NullPointerException at org.wikidata.wdtk.datamodel.implementation.DatatypeIdImpl.getDatatypeIriFromJsonDatatype(DatatypeIdImpl.java:124) at org.wikidata.wdtk.datamodel.implementation.PropertyDocumentImpl.<init>(PropertyDocumentImpl.java:116) at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62) at java.base/jdk.internal.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) at java.base/java.lang.reflect.Constructor.newInstance(Constructor.java:490) at com.fasterxml.jackson.databind.introspect.AnnotatedConstructor.call(AnnotatedConstructor.java:124) at com.fasterxml.jackson.databind.deser.std.StdValueInstantiator.createFromObjectWith(StdValueInstantiator.java:283) ... 18 more
when disabling the UUID and run the program twice I get the following message:
SLF4J: Class path contains multiple SLF4J bindings. SLF4J: Found binding in [jar:file:/Users/jasperkoehorst/GitLab/wikidata/libs/RDFSimpleCon.jar!/org/slf4j/impl/StaticLoggerBinder.class] SLF4J: Found binding in [jar:file:/Users/jasperkoehorst/.gradle/caches/modules-2/files-2.1/org.slf4j/slf4j-log4j12/1.7.10/b3eeae7d1765f988a1f45ea81517191315c69c9e/slf4j-log4j12-1.7.10.jar!/org/slf4j/impl/StaticLoggerBinder.class] SLF4J: See http://www.slf4j.org/codes.html#multiple_bindings for an explanation. SLF4J: Actual binding is of type [org.slf4j.impl.Log4jLoggerFactory] log4j:WARN No appenders could be found for logger (org.wikidata.wdtk.wikibaseapi.ApiConnection). log4j:WARN Please initialize the log4j system properly. log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info. Exception in thread "main" org.wikidata.wdtk.wikibaseapi.apierrors.MediaWikiApiErrorException: [failed-save] The save has failed. at org.wikidata.wdtk.wikibaseapi.apierrors.MediaWikiApiErrorHandler.throwMediaWikiApiErrorException(MediaWikiApiErrorHandler.java:65) at org.wikidata.wdtk.wikibaseapi.ApiConnection.checkErrors(ApiConnection.java:466) at org.wikidata.wdtk.wikibaseapi.ApiConnection.sendJsonRequest(ApiConnection.java:406) at org.wikidata.wdtk.wikibaseapi.WbEditingAction.performAPIAction(WbEditingAction.java:716) at org.wikidata.wdtk.wikibaseapi.WbEditingAction.wbEditEntity(WbEditingAction.java:294) at org.wikidata.wdtk.wikibaseapi.WikibaseDataEditor.createPropertyDocument(WikibaseDataEditor.java:271) at wikidata.App.main(App.java:55)
I assume this is the case due to an already existing property with the exact same label and description? However I am not certain since the message is not really clear in itself...
I has same problem. After debug by program, the class PropertyDocument
of Wikidata-Toolkit:0.10.0 require datatype
parameter, but my wikibase:REL1_31 not return datatype
field when create new property.
Dear wetneb,
thanks for the recommendation, I could get the connection, and write the property values, however after ending the task, it generates another error, and I wonder what could be the reason?
The last instruction executed was:
wbde.createPropertyDocument(propertyDocument, "Luis Test bot summary ");
and I think the problem is there.
Exception in thread "main" org.wikidata.wdtk.wikibaseapi.apierrors.MediaWikiApiErrorException: [failed-save] The save has failed. at org.wikidata.wdtk.wikibaseapi.apierrors.MediaWikiApiErrorHandler.throwMediaWikiApiErrorException(MediaWikiApiErrorHandler.java:65) at org.wikidata.wdtk.wikibaseapi.ApiConnection.checkErrors(ApiConnection.java:466) at org.wikidata.wdtk.wikibaseapi.ApiConnection.sendJsonRequest(ApiConnection.java:406) at org.wikidata.wdtk.wikibaseapi.WbEditingAction.performAPIAction(WbEditingAction.java:716) at org.wikidata.wdtk.wikibaseapi.WbEditingAction.wbEditEntity(WbEditingAction.java:294) at org.wikidata.wdtk.wikibaseapi.WikibaseDataEditor.createPropertyDocument(WikibaseDataEditor.java:271) at wikiloader.WikiPstReader.main(WikiPstReader.java:88)
Luis
this exception because you have same property on wikibase already.
@wangchuan2008888 yes the bug you are describing is known: https://github.com/Wikidata/Wikidata-Toolkit/issues/376