Hbase之批量數據寫入


/**
 * Created by similarface on 16/8/16.
 */

import java.io.IOException;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.TableName;
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.Table;
import org.apache.hadoop.hbase.client.Put;
import org.apache.hadoop.hbase.client.Result;
import org.apache.hadoop.hbase.util.Bytes;
import org.apache.hadoop.hbase.client.BufferedMutator;
import org.apache.hadoop.hbase.Cell;
import org.apache.hadoop.hbase.CellUtil;
import org.apache.hadoop.hbase.client.Mutation;
import java.util.List;
import java.util.ArrayList;
public class PutBufferExample {
    public static void main(String[] args) throws IOException {
        //獲取陪着參數
        Configuration config = HBaseConfiguration.create();
        //建立連接
        Connection connection = ConnectionFactory.createConnection(config);
        try {
            //連接表 獲取表對象
            Table t = connection.getTable(TableName.valueOf("testtable"));
            BufferedMutator table = connection.getBufferedMutator(TableName.valueOf("testtable"));
            try {
                Put p = new Put(Bytes.toBytes("myrow-1"));
                //p.add(); 這個地方的add 是個過期的方法然而我並不知道Cell的用法是什么
                p.add(Bytes.toBytes("colfam1"), Bytes.toBytes("name1"), Bytes.toBytes("zhangsan1"));
                //table.put(p);
                List<Mutation> mutations = new ArrayList<Mutation>();
                mutations.add(p);
                table.mutate(mutations);
                //如果不flush 在后面get可能是看不見的
                table.flush();
                // Close your table and cluster connection.
                Get get=new Get(Bytes.toBytes("myrow-1"));
                Result result=t.get(get);
                for(Cell cell:result.rawCells()){
                    System.out.print("行健: "+new String(CellUtil.cloneRow(cell)));
                    System.out.print("\t列簇: "+new String(CellUtil.cloneFamily(cell)));
                    System.out.print("\t列: "+new String(CellUtil.cloneQualifier(cell)));
                    System.out.print("\t值: "+new String(CellUtil.cloneValue(cell)));
                    System.out.println("\t時間戳: "+cell.getTimestamp());
                }
                System.out.print(">>>>end");
            } finally {
                if (table != null) table.close();
            }
        } finally {
            connection.close();
        }
    }
}

  


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM