您好,欢迎访问三七文档
当前位置:首页 > 商业/管理/HR > 管理学资料 > educoder平台HBase-开发:批量操作
第1关:批量获取数据packagestep1;importjava.io.IOException;importjava.util.ArrayList;importjava.util.List;importorg.apache.hadoop.conf.Configuration;importorg.apache.hadoop.hbase.Cell;importorg.apache.hadoop.hbase.CellScanner;importorg.apache.hadoop.hbase.CellUtil;importorg.apache.hadoop.hbase.HBaseConfiguration;importorg.apache.hadoop.hbase.TableName;importorg.apache.hadoop.hbase.client.Admin;importorg.apache.hadoop.hbase.client.Connection;importorg.apache.hadoop.hbase.client.ConnectionFactory;importorg.apache.hadoop.hbase.client.Get;importorg.apache.hadoop.hbase.client.HTable;importorg.apache.hadoop.hbase.client.Result;importorg.apache.hadoop.hbase.client.ResultScanner;importorg.apache.hadoop.hbase.client.Scan;importorg.apache.hadoop.hbase.client.Table;importorg.apache.hadoop.hbase.client.TableDescriptor;importorg.apache.hadoop.hbase.generated.rest.rest_jsp;importorg.apache.hadoop.hbase.util.Bytes;publicclassTask{publicvoidbatchGet()throwsException{/*********Begin*********/Configurationconfig=HBaseConfiguration.create();ConnectionConnection=ConnectionFactory.createConnection(config);ListStringrows=newArrayList();rows.add(2018);//rows.add(2019);rows.add(2020);TableNametableName=TableName.valueOf(Bytes.toBytes(step1_student));Tabletable=Connection.getTable(tableName);getData(table,rows);/*********End*********/}publicListStringgetData(Tabletable,ListStringrows)throwsException{ListGetgets=newArrayList();for(Stringstr:rows){Getget=newGet(Bytes.toBytes(str));gets.add(get);}ListStringvalues=newArrayList();Result[]results=table.get(gets);for(Resultresult:results){System.out.println(Row:+Bytes.toString(result.getRow()));for(Cellkv:result.rawCells()){Stringfamily=Bytes.toString(CellUtil.cloneFamily(kv));Stringqualifire=Bytes.toString(CellUtil.cloneQualifier(kv));Stringvalue=Bytes.toString(CellUtil.cloneValue(kv));values.add(value);System.out.println(family+:+qualifire+\t+value);}}returnvalues;}}命令行:点击测评之前,请先开启Hadoop(start-dfs.sh)和HBase(start-hbase.sh),并且需要等待HBase初始化完成(20秒左右),否则无法在HBase中创建表。———————————————————————————————————————第2关:批量删除数据packagestep2;importjava.io.IOException;importjava.util.ArrayList;importjava.util.HashMap;importjava.util.List;importjava.util.Map;importorg.apache.hadoop.conf.Configuration;importorg.apache.hadoop.hbase.HBaseConfiguration;importorg.apache.hadoop.hbase.HColumnDescriptor;importorg.apache.hadoop.hbase.HTableDescriptor;importorg.apache.hadoop.hbase.TableDescriptors;importorg.apache.hadoop.hbase.TableName;importorg.apache.hadoop.hbase.client.*;importorg.apache.hadoop.hbase.util.Bytes;publicclassTask{publicvoidbatchDelete()throwsException{/*********Begin*********/Configurationconf=HBaseConfiguration.create();Connectionconn=ConnectionFactory.createConnection(conf);TableNametableName=TableName.valueOf(step2_table);Tabletable=conn.getTable(tableName);ListStringrows1=newArrayList();for(inti=1;i6;i++){Stringrow=row+i;rows1.add(row);}delete(table,rows1);ListStringrows2=newArrayList();for(inti=7;i11;i++){Stringrow=row+i;rows2.add(row);}delete(table,rows2);/*********End*********/}publicvoiddelete(Tabletable,ListStringrows)throwsIOException{ListDeletedeletes=newArrayList();for(Stringstr:rows){byte[]row=Bytes.toBytes(str);Deletedelete=newDelete(row);deletes.add(delete);}table.delete(deletes);}}———————————————————————————————————————第3关:批量导入数据至HBasepackagestep3;importjava.io.IOException;importjava.util.ArrayList;importjava.util.HashMap;importjava.util.List;importjava.util.Map;importorg.apache.hadoop.conf.Configuration;importorg.apache.hadoop.hbase.HBaseConfiguration;importorg.apache.hadoop.hbase.HColumnDescriptor;importorg.apache.hadoop.hbase.HTableDescriptor;importorg.apache.hadoop.hbase.TableDescriptors;importorg.apache.hadoop.hbase.TableName;importorg.apache.hadoop.hbase.client.Admin;importorg.apache.hadoop.hbase.client.ColumnFamilyDescriptor;importorg.apache.hadoop.hbase.client.ColumnFamilyDescriptorBuilder;importorg.apache.hadoop.hbase.client.Connection;importorg.apache.hadoop.hbase.client.ConnectionFactory;importorg.apache.hadoop.hbase.client.Get;importorg.apache.hadoop.hbase.client.Put;importorg.apache.hadoop.hbase.client.Result;importorg.apache.hadoop.hbase.client.ResultScanner;importorg.apache.hadoop.hbase.client.Scan;importorg.apache.hadoop.hbase.client.Table;importorg.apache.hadoop.hbase.client.TableDescriptor;importorg.apache.hadoop.hbase.client.TableDescriptorBuilder;importorg.apache.hadoop.hbase.util.Bytes;publicclassTask{publicvoidbatchPut()throwsException{/*********Begin*********/Configurationconfig=newConfiguration();Connectionconn=ConnectionFactory.createConnection(config);Adminadmin=conn.getAdmin();//建表TableNametableName=TableName.valueOf(Bytes.toBytes(stu));TableDescriptorBuilderbuilder=TableDescriptorBuilder.newBuilder(tableName);ColumnFamilyDescriptorfamily=ColumnFamilyDescriptorBuilder.newBuilder(Bytes.toBytes(basic_info)).build();ColumnFamilyDescriptorfamily2=ColumnFamilyDescriptorBuilder.newBuilder(Bytes.toBytes(school_info)).build();builder.setColumnFam
本文标题:educoder平台HBase-开发:批量操作
链接地址:https://www.777doc.com/doc-7222669 .html