zoukankan      html  css  js  c++  java
  • 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(" 列簇: "+new String(CellUtil.cloneFamily(cell)));
                        System.out.print(" 列: "+new String(CellUtil.cloneQualifier(cell)));
                        System.out.print(" 值: "+new String(CellUtil.cloneValue(cell)));
                        System.out.println(" 时间戳: "+cell.getTimestamp());
                    }
                    System.out.print(">>>>end");
                finally {
                    if (table != null) table.close();
                }
            finally {
                connection.close();
            }
        }
    }
  • 相关阅读:
    Linux下mysql使用systemctl restart mysqld命令失败
    Linux环境下mysql报错:bash: mysql: command not found 的解决方法
    Linux查看mysql是否启动的命令
    启动MySQL5.7时报错:initialize specified but the data directory has files in it. Aborting.
    ARM64架构下面安装mysql5.7.22
    Python3.6打开EAIDK-610开发板(计算机通用)摄像头拍照并保存
    Python的几种主动结束程序方式
    aarch64架构下安装tensorflow详细过程
    python代码在linux终端中执行报错:Unable to init server: Could not connect: Connection refused
    red hat 报错:apt-get:找不到命令
  • 原文地址:https://www.cnblogs.com/seaspring/p/6568226.html
Copyright © 2011-2022 走看看