operation_hbase

package solr_search.tsf.hbase.domain;

import java.io.IOException;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.NavigableMap;
import java.util.Set;
import java.util.Map.Entry;
import java.util.concurrent.ConcurrentHashMap;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.HColumnDescriptor;
import org.apache.hadoop.hbase.HTableDescriptor;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.Admin;
import org.apache.hadoop.hbase.client.Connection;
import org.apache.hadoop.hbase.client.ConnectionFactory;
import org.apache.hadoop.hbase.client.Get;
import org.apache.hadoop.hbase.client.Put;
import org.apache.hadoop.hbase.client.Result;
import org.apache.hadoop.hbase.client.Table;
import org.apache.hadoop.hbase.io.compress.Compression.Algorithm;
import org.apache.hadoop.hbase.util.Bytes;
import org.apache.log4j.Logger;

public class Operation_Hbase {

    private static final Logger logger = Logger
            .getLogger(Operation_Hbase.class);
    public static Connection connection;
    public static Configuration conf;
    static {
        conf = HBaseConfiguration.create();
        try {
            connection = ConnectionFactory.createConnection(conf);
            System.err.println("連接成功");
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        try {
            String[] ids = new String[] { "10a9a3ef064ef0fd4d8ae2acdbf7a" };
            // List<HashMap<String, Object>> res = select1(ids);
            // for(HashMap<String, Object> re:res){
            // System.out.println(re);
            // }

            // List<BaseToken> resu = select2(ids);
            // for (BaseToken re : resu) {
            // System.out.println(re);
            // System.out.println(re.id);
            // }
            List<DocField> resul = select3(ids);
            for (DocField re : resul) {
                System.out.println(re);
                System.out.println(re.getId());
            }

        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }

    /**
     * 創建表
     * 
     * 
     * @param tablename
     *            表名
     * @param familys
     *            表成員數組
     */

    public void creatTable() throws Exception {
        String tablename = "Testdata";
        Admin admin = connection.getAdmin();
        if (admin.tableExists(TableName.valueOf("tablename"))) {
            logger.info("table already exists!");
        } else {
            HTableDescriptor desc = new HTableDescriptor(
                    TableName.valueOf(tablename));
            desc.addFamily(new HColumnDescriptor("article"));
            admin.createTable(desc);
            admin.close();
            logger.info("create table Success!");
        }
        logger.info("end create table ......");
    }

    /**
     * 向表中增加數據
     * 
     * @param tablename
     *            表名
     */
    public void add(String tablename, List<DocField> datas) {
        List<Put> putDatas = new ArrayList<Put>();
        Table table;
        try {
            table = connection.getTable(TableName.valueOf(tablename));
            HColumnDescriptor hcd = new HColumnDescriptor("article");
            hcd.setCompressionType(Algorithm.SNAPPY);
            for (DocField data : datas) {
                Put put = new Put(Bytes.toBytes(data.getId()));
                Field[] fields = data.getClass().getDeclaredFields();
                for (Field field : fields) {
                    field.setAccessible(true);
                    try {
                        put.addColumn(Bytes.toBytes("article"),
                                Bytes.toBytes(field.getName()),
                                Bytes.toBytes(field.get(data).toString()));
                    } catch (IllegalArgumentException | IllegalAccessException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                }

                putDatas.add(put);

            }
            table.put(putDatas);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    /**
     * 根據id查詢數據 Solr
     * 
     * @param id
     *            表主鍵(備註:個個表的主鍵會不會id衝突)
     */
    public static List<HashMap<String, Object>> select1(String[] ids)
            throws Exception {
        String tablename = "Testdata";
        Table table = connection.getTable(TableName.valueOf(tablename));
        List<Get> getList = new ArrayList<Get>();
        List<HashMap<String, Object>> domainList = new ArrayList<HashMap<String, Object>>();
        for (String id : ids) {
            Get get = new Get(id.getBytes());
            getList.add(get);
        }
        Result[] rts = table.get(getList);
        for (Result rs : rts) {
            NavigableMap<byte[], byte[]> familyMap = rs.getFamilyMap(Bytes
                    .toBytes("article"));
            // Map<String, Object> doc=new ConcurrentHashMap<String, Object>();
            HashMap<String, Object> map = new LinkedHashMap<String, Object>();
            Set<Entry<byte[], byte[]>> entryMap = familyMap.entrySet();
            for (Entry<byte[], byte[]> entry : entryMap) {
                System.out.println(new String(entry.getKey()) + ":"
                        + new String(entry.getValue()));
                map.put(new String(entry.getKey()),
                        new String(entry.getValue()));
            }
            domainList.add(map);
        }

        return domainList;

    }

    /**
     * 根據id查詢數據 Solr
     * 
     * @param id
     *            表主鍵(備註:個個表的主鍵會不會id衝突)
     */
    public static List<BaseToken> select2(String[] ids) throws Exception {
        String tablename = "Testdata";
        Table table = connection.getTable(TableName.valueOf(tablename));
        List<Get> getList = new ArrayList<Get>();
        List<BaseToken> domainList = new ArrayList<BaseToken>();
        for (String id : ids) {
            String rowkey = id;
            Get get = new Get(rowkey.getBytes());
            getList.add(get);
        }
        Result[] rts = table.get(getList);
        for (Result rs : rts) {
            NavigableMap<byte[], byte[]> familyMap = rs.getFamilyMap(Bytes
                    .toBytes("article"));
            BaseToken docc = new BaseToken();
            Field[] fieldss = docc.getClass().getDeclaredFields();
            for (Field field : fieldss) {
                String fieldName = field.getName();
                docc.put(fieldName,
                        new String(familyMap.get(fieldName.getBytes())));
            }
            domainList.add(docc);

        }

        return domainList;

    }

    /**
     * 根據id查詢數據 Solr
     * 
     * @param id
     *            表主鍵(備註:個個表的主鍵會不會id衝突)
     */
    public static List<DocField> select3(String[] ids) throws Exception {
        String tablename = "Testdata";
        Table table = connection.getTable(TableName.valueOf(tablename));
        List<Get> getList = new ArrayList<Get>();
        List<DocField> domainList = new ArrayList<DocField>();
        for (String id : ids) {
            String rowkey = id;
            Get get = new Get(rowkey.getBytes());
            getList.add(get);
        }
        Result[] rts = table.get(getList);
        for (Result rs : rts) {
            NavigableMap<byte[], byte[]> familyMap = rs.getFamilyMap(Bytes
                    .toBytes("article"));
            DocField solrField = new DocField();
            Field[] fields = solrField.getClass().getFields();

            for (Field field : fields) {
                String fieldName = field.getName();
                field.set(solrField,
                        new String(familyMap.get(fieldName.getBytes())));

            }

            domainList.add(solrField);
        }

        return domainList;

    }
}
package solr_search.tsf.hbase.domain;

import java.io.Serializable;
import java.util.concurrent.ConcurrentHashMap;

import org.apache.commons.lang3.builder.EqualsBuilder;
import org.apache.commons.lang3.builder.HashCodeBuilder;
import org.apache.commons.lang3.builder.ToStringBuilder;
import org.apache.commons.lang3.builder.ToStringStyle;

/**
 * doc處理Token基類
 * @author admin
 *
 */
public class BaseToken extends ConcurrentHashMap<String, Object>implements Serializable{

    /**
     * 
     */
    //private static final long serialVersionUID = 1L;

    public String id;
    public String url;
    public String title;
    public String type;
    public String author;
    public String source;
    public String pubTime;
    public String saveTime;
    public String site;
    public String content;
    public String sentiment;
    public String keyWords;
    public String topic;
    public String summary;

    public BaseToken() {
    }

    public BaseToken(String text) {
        this.put("Text", text);
    }

    public boolean equals(Object obj){
        return EqualsBuilder.reflectionEquals(this,obj);
    }

    public String getId(){
        return this.getValue("Id");
    }

    private String getValue(Object key) {
        Object value=super.get(key);
        String result=null;
        if(value instanceof String){
            result=String.valueOf(value);
        }
        return result;
    }

    public String getText(){
        String text=(String) this.get("Text");
        return text;
    }

    @Override
    public int hashCode() {
        return HashCodeBuilder.reflectionHashCode(this);
    }

    public void setId(int id) {
        this.put("Id", id);
    }

    public void setText(String text) {
        this.put("Text", text);
    }

    @Override
    public String toString() {
        this.id=this.getValue("id");
        this.url=this.getValue("url");
        this.title=this.getValue("title");
        this.type=this.getValue("type");
        this.author=this.getValue("author");
        this.source=this.getValue("source");
        this.pubTime=this.getValue("pubTime");
        this.sentiment=this.getValue("sentiment");
        this.keyWords=this.getValue("keyWords");
        this.summary=this.getValue("summary");
        return ToStringBuilder.reflectionToString(this,ToStringStyle.JSON_STYLE);
    }
}
發佈了53 篇原創文章 · 獲贊 8 · 訪問量 10萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章