hbase table data to rewrite the content

Continue insert table stop

hbase shell>disable 'tbname'

Snapshot
hbase shell> snapshot 'tbname', 'tbsnap'

Cloning snapshot of the new name
hbase shell> clone_snapshot 'tbsnap', 'newtb'

Deleting a snapshot
hbase shell> delete_snapshot 'tableSnapshot'

Write data conversion program

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.apache.hadoop.hbase.Cell;
import org.apache.hadoop.hbase.CellUtil;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.client.HTableInterface;
import org.apache.hadoop.hbase.client.Put;
import org.apache.hadoop.hbase.client.Result;
import org.apache.hadoop.hbase.client.ResultScanner;
import org.apache.hadoop.hbase.client.Scan;
import org.apache.hadoop.hbase.filter.FilterList;
import org.apache.hadoop.hbase.util.Bytes;
import org.springframework.data.hadoop.hbase.HbaseTemplate;

import com.alibaba.fastjson.JSONObject;

import lombok.extern.slf4j.Slf4j;

@Slf4j
public class HbaseUtil {
    
    public static void saveTemperature2hbase(String tbname,String rowKey, String v
            , HbaseTemplate hbaseTemplate) {
        hbaseTemplate.execute(tbname, (table) -> {
            Put put = new Put(Bytes.toBytes(rowKey));
            put.addColumn(Bytes.toBytes("XXX"), Bytes.toBytes("XXX"), Bytes.toBytes(v));
            table.put(put);
            return true;
        });
    }
    
       public static Map<String, List<Map<String, Object>>> getColumnDataFromHbase(String table
               , String family, String column, HbaseTemplate hbaseTemplate) {

            return hbaseTemplate.execute(table, hTableInterface -> {
                Scan scan = new Scan();
                scan.setCacheBlocks(true);
                scan.setCaching(30000);
                scan.setBatch(300);

                scan.addColumn(family.getBytes(), column.getBytes());

                FilterList filterList = new FilterList();


                scan.setFilter(filterList);
                Map<String, List<Map<String, Object>>> tempData = new HashMap<>();

                ResultScanner scanner = null;

                try {
                    scanner = hTableInterface.getScanner(scan);

                    for (Result result : scanner) {
                        byte[] row = result.getRow();
                        String rowKey = Bytes.toString(row);

                        List<Cell> cells = result.listCells();
                        for (Cell cell : cells) {
                            Map<String, Object> data = new HashMap<>();
                            String id = Bytes.toString(CellUtil.cloneQualifier(cell));
                            String value = Bytes.toString(CellUtil.cloneValue(cell));
                            
                            log.info("rowKey:{}, column:{}, value:{}", rowKey, id, value);
                            
                            JSONObject json = new JSONObject();
                            
                            // 填写 json
                            
                            String jstr = json.toJSONString();
                            log.info(jstr);
                            
                            saveTemperature2hbase("XXX", rowKey, jstr, hbaseTemplate);
                        }
                    }
                } finally {
                    if (null != scanner)
                        scanner.close();
                }
                return tempData;
            });
        }
       
       public static void main(String[] args) {
            HbaseTemplate hbaseTemplate = new HbaseTemplate();
            org.apache.hadoop.conf.Configuration conf = HBaseConfiguration.create();
            conf.set("hbase.zookeeper.quorum", "127.0.0.1");
            conf.set("hbase.zookeeper.port", "2181");
            hbaseTemplate.setConfiguration(conf);
            hbaseTemplate.setAutoFlush(true);
            
            getColumnDataFromHbase("XXX", "XXX", "XXX", hbaseTemplate);
       }
}                

Delete the original table
hbase shell> drop 'tableName'

Guess you like

Origin www.cnblogs.com/m2492565210/p/11343879.html