标签:des style blog http color java 使用 os
大数据架构-使用HBase和Solr将存储与索引放在不同的机器上
/*
*版权:王安琪
*描述:监视HBase,一有数据postPut就向Solr发送,本类要作为触发器添加到HBase
*修改时间:2014-05-27
*修改内容:新增
*/
package solrHbase.test;
import java.io.UnsupportedEncodingException;
import ***;
public class SorlIndexCoprocessorObserver extends BaseRegionObserver {
private static final Logger LOG = LoggerFactory
.getLogger(SorlIndexCoprocessorObserver.class);
private static final String solrUrl = "http://192.1.11.108:80/solr/core1";
private static final SolrServer solrServer = new ConcurrentUpdateSolrServer(
solrUrl, 10000, 20);
/**
* 建立solr索引
*
* @throws UnsupportedEncodingException
*/
@Override
public void postPut(final ObserverContext<RegionCoprocessorEnvironment> e,
final Put put, final WALEdit edit, final boolean writeToWAL)
throws UnsupportedEncodingException {
inputSolr(put);
}
public void inputSolr(Put put) {
try {
solrServer.add(TestSolrMain.getInputDoc(put));
} catch (Exception ex) {
LOG.error(ex.getMessage());
}
}
}
|
public static SolrInputDocument getInputDoc(Put put) {
SolrInputDocument doc = new SolrInputDocument();
doc.addField("test_ID", Bytes.toString(put.getRow()));
for (KeyValue c : put.getFamilyMap().get(Bytes.toBytes(columnFamily))) {
String key = Bytes.toString(c.getKey());
String value = Bytes.toString(c.getValue());
if (value.isEmpty()) {
continue;
}
String fieldName = key.substring(key.indexOf(columnFamily) + 3,
key.indexOf("")).trim();
doc.addField(fieldName, value);
}
return doc;
} |
/*
*版权:王安琪
*描述:测试HBaseInsert,HBase插入性能
*修改时间:2014-05-27
*修改内容:新增
*/
package solrHbase.test;
import hbaseInput.HbaseInsert;
import ***;
public class TestHBaseMain {
private static Configuration config;
private static String tableName = "angelHbase";
private static HTable table = null;
private static final String columnFamily = "wanganqi";
/**
* @param args
*/
public static void main(String[] args) {
config = HBaseConfiguration.create();
config.set("hbase.zookeeper.quorum", "192.103.101.104");
HbaseInsert.createTable(config, tableName, columnFamily);
try {
table = new HTable(config, Bytes.toBytes(tableName));
for (int k = 0; k < 1; k++) {
Thread t = new Thread() {
public void run() {
for (int i = 0; i < 100000; i++) {
HbaseInsert.inputData(table,
PutCreater.createPuts(1000, columnFamily));
Calendar c = Calendar.getInstance();
String dateTime = c.get(Calendar.YEAR) + "-"
+ c.get(Calendar.MONTH) + "-"
+ c.get(Calendar.DATE) + "T"
+ c.get(Calendar.HOUR) + ":"
+ c.get(Calendar.MINUTE) + ":"
+ c.get(Calendar.SECOND) + ":"
+ c.get(Calendar.MILLISECOND) + "Z 写入: "
+ i * 1000;
System.out.println(dateTime);
}
}
};
t.start();
}
} catch (IOException e1) {
e1.printStackTrace();
}
}
}
|
/*
*版权:王安琪
*描述:与HBase相关操作,建表与插入数据
*修改时间:2014-05-27
*修改内容:新增
*/
package hbaseInput;
import ***;
import org.apache.hadoop.hbase.client.Put;
public class HbaseInsert {
public static void createTable(Configuration config, String tableName,
String columnFamily) {
HBaseAdmin hBaseAdmin;
try {
hBaseAdmin = new HBaseAdmin(config);
if (hBaseAdmin.tableExists(tableName)) {
return;
}
HTableDescriptor tableDescriptor = new HTableDescriptor(tableName);
tableDescriptor.addFamily(new HColumnDescriptor(columnFamily));
hBaseAdmin.createTable(tableDescriptor);
hBaseAdmin.close();
} catch (MasterNotRunningException e) {
e.printStackTrace();
} catch (ZooKeeperConnectionException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
public static void inputData(HTable table, ArrayList<Put> puts) {
try {
table.put(puts);
table.flushCommits();
puts.clear();
} catch (IOException e) {
e.printStackTrace();
}
}
}
|
public static Put createPut(String columnFamily) {
String ss = getSentence();
byte[] family = Bytes.toBytes(columnFamily);
byte[] rowKey = Bytes.toBytes("" + Math.abs(r.nextLong()));
Put put = new Put(rowKey);
put.add(family, Bytes.toBytes("DeviceID"),
Bytes.toBytes("" + Math.abs(r.nextInt())));
******
put.add(family, Bytes.toBytes("Company_mmsegsm"), Bytes.toBytes("ss"));
return put;
} |
private static void sendConcurrentUpdateSolrServer(final String url,
final int count) throws SolrServerException, IOException {
SolrServer solrServer = new ConcurrentUpdateSolrServer(url, 10000, 20);
for (int i = 0; i < count; i++) {
solrServer.add(getInputDoc(PutCreater.createPut(columnFamily)));
}
} |
大数据架构-使用HBase和Solr将存储与索引放在不同的机器上,布布扣,bubuko.com
大数据架构-使用HBase和Solr将存储与索引放在不同的机器上
标签:des style blog http color java 使用 os
原文地址:http://www.cnblogs.com/wgp13x/p/3927979.html