I have previously introduced the hive2solr project on GitHub AND THE SOLR multivalue function.
Online data is pushed to SOLR using hive after calculation. If multivalue is required, the default hive2solr is problematic. Even if the field in hive is multiple characters, it is only a whole string after SOLR is imported. For example, the data in the following table is as follows:
id test_s test_ss3 d f d h
Test_ss is of the multivalue type. After importing SOLR:
{"Test_ss": ["f d h" // identifies an element], "test_s": "D", "ID": "3 ", "_ version _": 1472413953618346000}
If SOLR is directly inserted into an array generated by hive, an error occurred while converting array to string.
select id,test_s,split(test_ss,‘ ‘) from t2;FAILED: NoMatchingMethodException No matching method for class org.apache.hadoop.hive.ql.udf.UDFToString with (array<string>). Possible choices: _FUNC_(void) _FUNC_(boolean) _FUNC_(tinyint) _FUNC_(smallint) _FUNC_(int) _FUNC_(bigint) _FUNC_(float) _FUNC_(double) _FUNC_(string) _FUNC_(timestamp) _FUNC_(decimal) _FUNC_(binary)
Hive writes data to SOLR mainly through the write method of solrwriter. In the end, it calls the setfield method of solrinputdocument. You can workaround und by changing the code to the following content.
The write method of solrwriter:
@ Override public void write (writable W) throws ioexception {mapwritable map = (mapwritable) W; solrinputdocument Doc = new solrinputdocument (); For (final map. entry <writable, writable> entry: map. entryset () {string key = entry. getkey (). tostring (); Doc. setfield (Key, entry. getvalue (). tostring (); // call the setfield method of solrinputdocument} table. save (DOC );}
Changed:
@ Override public void write (writable W) throws ioexception {mapwritable map = (mapwritable) W; solrinputdocument Doc = new solrinputdocument (); For (final map. entry <writable, writable> entry: map. entryset () {string key = entry. getkey (). tostring (); string value = entry. getvalue (). tostring (); string [] SL = value. split ("\ s +"); // separate hive input data by spaces and cut it into arrays (hive SQL only needs to be concact) List <string> valuesl = Java. util. arrays. aslist (SL); log.info ("Add entry value lists:" + valuesl); For (string Vl: valuesl) {Doc. addfield (Key, VL); // call the addfiled method to avoid overwriting} table. save (DOC );}
Import test results:
{ "test_ss": [ "f", "d", "h" ], "test_s": "d", "id": "3", "_version_": 1472422023801077800 }
This article from the "Food light blog" blog, please be sure to keep this source http://caiguangguang.blog.51cto.com/1652935/1433770