{"id":154,"date":"2010-11-22T23:38:15","date_gmt":"2010-11-22T22:38:15","guid":{"rendered":"http:\/\/sematext.solr.pl\/?p=154"},"modified":"2020-11-10T23:38:51","modified_gmt":"2020-11-10T22:38:51","slug":"data-import-handler-how-to-import-data-from-sql-databases-part-3","status":"publish","type":"post","link":"https:\/\/solr.pl\/en\/2010\/11\/22\/data-import-handler-how-to-import-data-from-sql-databases-part-3\/","title":{"rendered":"Data Import Handler \u2013 How to import data from SQL databases (part 3)"},"content":{"rendered":"<p>In previous episodes&nbsp; (<a title=\"Data Import Handler - How to import data from SQL databases (part 1)\" href=\"http:\/\/solr.pl\/en\/2010\/10\/11\/data-import-handler-%E2%80%93-how-to-import-data-from-sql-databases-part-1\/\">part 1<\/a> i <a title=\"Data Import Handler - How to import data from SQL databases (part 2)\" href=\"http:\/\/solr.pl\/en\/2010\/11\/01\/data-import-handler-%E2%80%93-how-to-import-data-from-sql-databases-part-2\/\">part 2<\/a>) we were able to import data from a database in a both wyas full and incremental. Today is the time for a short summary.<\/p>\n\n\n<!--more-->\n\n\n<h2>Setting dataSource<\/h2>\n<p>Recall the line with our setup:\n<\/p>\n<pre class=\"brush:xml\">&lt;dataSource\n    driver=\"org.postgresql.Driver\"\n    url=\"jdbc:postgresql:\/\/localhost:5432\/wikipedia\"\n    user=\"wikipedia\"\n    password=\"secret\" \/&gt;<\/pre>\n<p>These are not all attributes that can appear. For readability let&#8217;s mention them all:<\/p>\n<ul>\n<li><strong>name<\/strong> \u2013 the name of the source &#8211; you can define many different sources and refer to them by attribute &#8220;<em>DataSource<\/em>&#8221; tag &#8220;<em>entity<\/em>&#8220;<\/li>\n<li><strong>driver<\/strong> \u2013 JDBC driver class name<\/li>\n<li><strong>url<\/strong> \u2013 JDBC database url<\/li>\n<li><strong>user<\/strong> \u2013 database user name (if not defined, or empty, the connection to the database occurs without a pair user\/password)<\/li>\n<li><strong>password<\/strong> \u2013 user password<\/li>\n<li><strong>jndiName<\/strong> \u2013 instead of giving elements: driver\/url\/user\/password, you can specify the JNDI name under which the data source implementation (<em>javax.sql.DataSource<\/em>) is made available by the container (eg Jetty\/Tomcat)<\/li>\n<\/ul>\n<p>Advanced arguments:<\/p>\n<ul>\n<li><strong>batchSize<\/strong> (default: 500) &#8211; sets the maximum number (or rather a suggestion for the driver) records retrieved from the database in one query to the database. Changing this parameter can help in situations where queries return to much results. It may not help, since implementation of this mechanism depends on the JDBC driver.<\/li>\n<li><strong>convertType<\/strong> (default: false) &#8211; Applies an additional conversion from the field type returned by the database to the field type defined in the schema.xml. The default value seems to be safer, because it does not cause extra, magical conversion. However, in special cases (eg BLOB fields), that conversion is one of the ways of solving the problem.<\/li>\n<li><strong>maxRows<\/strong> (default: 0 &#8211; no limit) &#8211; sets the maximum number of results returned by a query to the database.<\/li>\n<li><strong>readOnly<\/strong> \u2013 set the connection to the database in read mode. In principle, this could mean that the driver will be able to perform additional optimizations. At the same time it means the default (!)<em> transactionIsolation<\/em> setting the <em>TRANSACTION_READ_UNCOMMITTED<\/em>, <em>holdability<\/em> the <em>CLOSE_CURSORS_AT_COMMIT<\/em>, <em>autoCommit<\/em> to <em>true<\/em>.<\/li>\n<li><strong>autoCommit<\/strong> \u2013 set autocommit transaction after each query.<\/li>\n<li><strong>transactionIsolation <\/strong>(<em>TRANSACTION_READ_UNCOMMITTED<\/em>, <em>TRANSACTION_READ_COMMITTED<\/em>, <em>TRANSACTION_REPEATABLE_READ<\/em>, <em>TRANSACTION_SERIALIZABLE<\/em>, <em>TRANSACTION_NONE<\/em>) &#8211; sets the transaction isolation (ie, the visibility of the data changed within a transaction)<\/li>\n<li><strong>holdability<\/strong> (<em>CLOSE_CURSORS_AT_COMMIT<\/em>, <em>HOLD_CURSORS_OVER_COMMIT<\/em>) &#8211; defines how the results will closed (ResultSet) when the transaction is closed<\/li>\n<li><strong>&#8230;<\/strong> &#8211; The important thing is that there may be any other attributes. All of them will be forwarded by DIH to the JDBC driver, which allows you to define special behavior defined by a specific JDBC driver.<\/li>\n<li><strong>type<\/strong> \u2013 type of source. The default value (JdbcDataSource) is sufficient, so the tag can forgotten (I remind you of him on the occasion of the definition of non-SQLowych source)<\/li>\n<\/ul>\n<h2>The \u201eentity\u201d element<\/h2>\n<p>Let us now turn to the description of the &#8220;entity&#8221; item.<\/p>\n<p>As a reminder:\n<\/p>\n<pre class=\"brush:xml\">&lt;entity name=\"page\" query=\"SELECT page_id as id, page_title as name from page\"&gt;\n    &lt;entity name=\"revision\" query=\"select rev_id from revision where rev_page=${page.id}\"&gt;\n        &lt;entity name=\"pagecontent\" query=\"select old_text as text from pagecontent where old_id=${revision.rev_id}\"&gt;\n        &lt;\/entity&gt;\n    &lt;\/entity&gt;\n&lt;\/entity&gt;<\/pre>\n<p>And all the attributes:<\/p>\n<p>Primary:<\/p>\n<ul>\n<li><strong>name<\/strong> \u2013 the name of the entity<\/li>\n<li><strong>query<\/strong> \u2013 SQL query used to retrieve data associated with that entity.<\/li>\n<li><strong>deltaQuery &#8211; <\/strong>query responsible for returning the IDs of those records that have changed since the last crawl (full or incremental) &#8211; the last crawl time is provided by DIH in the variable: ${dataimporter.last_index_time}. This query is used by Solr to find those records that have changed.<\/li>\n<li><strong>parentDeltaQuery &#8211; <\/strong>query requesting data for the parent entity record. With these queries Solr is able to retrieve all the data that make up the document, regardless of the entity from which they originate. This is necessary because the indexing engine is not able to modify the indexed data &#8211; so we need to index the entire document, regardless of the fact that some data has not changed.<\/li>\n<li><strong>deletedPkQuery &#8211; <\/strong>provides identifiers of deleted items.<\/li>\n<li><strong>deltaImportQuery &#8211; <\/strong>query requesting data for a given record identified by ID that is avaiable as a DIH variable: <em>${dataimport.delta.id}<\/em>.<\/li>\n<li><strong>dataSource<\/strong> \u2013 the name of the source, the definitions used in several sources (see dataSource.name)<\/li>\n<\/ul>\n<p>and advanced:<\/p>\n<ul>\n<li><strong>processor &#8211; <\/strong><em>SQLEntityProcessor<\/em> by default. Element whose function is to provide the data source further elements to a crawl. In the case of databases, usually the default implementation is sufficient<\/li>\n<li><strong>transformer <\/strong>&#8211; the data retrieved from the source can be further modified before transmission to a crawl. In particular, the transformer may return additional records, which makes it a very powerful tool<\/li>\n<li><strong>rootEntity<\/strong> &#8211; default true for entity element below the document element. This marks the element, which is treated as a root, that is, it will be used to create new items in the index<\/li>\n<li><strong>threads <\/strong>&#8211;<em> <\/em>the number of threads used in the service component entity<\/li>\n<li><strong>onError<\/strong> (<em>abort<\/em>, <em>skip<\/em>, <em>continue<\/em>) \u2013 a way to respond to issues: to stop working (abort, the default behavior), ignoring the document (skip), ignore the error (continue)<\/li>\n<li><strong>preImportDeleteQuery<\/strong> &#8211; used instead of &#8220;*:*&#8221; to delete data from the index. (Note: The query to the index, does not query database) &#8211; makes sense only in the main entity element<\/li>\n<li><strong>postImportDeleteQuery<\/strong> &#8211; used after a full import. (Like <em>preImportDeleteQuery<\/em> query to the index) &#8211; makes sense only in the main entity element<\/li>\n<li><strong>pk <\/strong>&#8211; primary key (database, not to be confused with the unique key of the document) &#8211; is relevant only in incremental indexing, if we let DIH <em>deltaImportQuery<\/em> guess based on the <em>query<\/em><\/li>\n<\/ul>\n<p>In the text above the word &#8220;guess&#8221; appeared. DIH is trying to streamline the work, by adopting reasonable defaults. For example, as mentioned above, during the incremental import is able to try to determine deltaImportQuery. Actually, it was the only behavior in earlier versions, it was realized, that the generated queries does not always work. Hence, I suggest caution and the limited principle of trust \ud83d\ude42<\/p>\n<p>Another thing is the ability to override the definition of the field in a situation where the column names returned by the query correspond to the names of fields in the schema.xml.&nbsp; (Hand up: who noted that the above example is not a copy of the second part but is using that mechanism?)<\/p>\n<p>Yet another example of that DIH is very flexible is to draw attention to the fact that having a structure:\n<\/p>\n<pre>${dataimporter.last_index_time}<\/pre>\n<p>we can write the full import of this definition that when the import has already been carried out, it will be preserved as an incremental import! I think this functionality, &#8220;came&#8221; a little by accident \ud83d\ude42<\/p>","protected":false},"excerpt":{"rendered":"<p>In previous episodes&nbsp; (part 1 i part 2) we were able to import data from a database in a both wyas full and incremental. Today is the time for a short summary.<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":"","_links_to":"","_links_to_target":""},"categories":[27],"tags":[256,254,204,255],"class_list":["post-154","post","type-post","status-publish","format-standard","hentry","category-solr-en","tag-data-import-handler-2","tag-dih-2","tag-import-2","tag-integration"],"_links":{"self":[{"href":"https:\/\/solr.pl\/en\/wp-json\/wp\/v2\/posts\/154","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/solr.pl\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/solr.pl\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/solr.pl\/en\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/solr.pl\/en\/wp-json\/wp\/v2\/comments?post=154"}],"version-history":[{"count":1,"href":"https:\/\/solr.pl\/en\/wp-json\/wp\/v2\/posts\/154\/revisions"}],"predecessor-version":[{"id":155,"href":"https:\/\/solr.pl\/en\/wp-json\/wp\/v2\/posts\/154\/revisions\/155"}],"wp:attachment":[{"href":"https:\/\/solr.pl\/en\/wp-json\/wp\/v2\/media?parent=154"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/solr.pl\/en\/wp-json\/wp\/v2\/categories?post=154"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/solr.pl\/en\/wp-json\/wp\/v2\/tags?post=154"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}