educoder平台HBase开发:批量操作_第1页
educoder平台HBase开发:批量操作_第2页
educoder平台HBase开发:批量操作_第3页
educoder平台HBase开发:批量操作_第4页
educoder平台HBase开发:批量操作_第5页
已阅读5页,还剩4页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、第 1 关:批量获取数据package step1;import java.io.IOException;import java.util.ArrayList;import java.util.List;import org.apache.hadoop.conf.Configuration;import org.apache.hadoop.hbase.Cell;import org.apache.hadoop.hbase.CellScanner;import org.apache.hadoop.hbase.CellUtil;import org.apache.hadoop.hbase.HBas

2、eConfiguration;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.HTable;import o

3、rg.apache.hadoop.hbase.client.Result;import org.apache.hadoop.hbase.client.ResultScanner;import org.apache.hadoop.hbase.client.Scan;import org.apache.hadoop.hbase.client.Table;import org.apache.hadoop.hbase.client.TableDescriptor;import org.apache.hadoop.hbase.generated.rest.rest_jsp;import org.apac

4、he.hadoop.hbase.util.Bytes; public class Task public void batchGet() throws Exception *Begin*Configuration config = HBaseConfiguration.create();Connection Connection = ConnectionFactory.createConnection(config);List<String> rows = new ArrayList<>();rows.add("2018");/rows.add(&q

5、uot;2019");rows.add("2020");TableName tableName =TableName.valueOf(Bytes.toBytes("step1_student");Table table = Connection.getTable(tableName);getData(table,rows);/* 匸r" */public List<String> getData(Table table, List<String> rows) throws Exception List<G

6、et> gets = new ArrayList<>();for (String str : rows) Get get = new Get(Bytes.toBytes(str);gets.add(get);List<String> values = new ArrayList<>();Result results = table.get(gets);for (Result result : results) System.out.println("Row:" + Bytes.toString(result.getRow();for

7、 (Cell kv : result.rawCells() String family = Bytes.toString(CellUtil.cloneFamily(kv);String qualifire = Bytes.toString(CellUtil.cloneQualifier(kv);String value = Bytes.toString(CellUtil.cloneValue(kv);values.add(value);System.out.println(family + ":" + qualifire + "t" + value);r

8、eturn values;命令行:HBase点击测评之前,请先开启 Hadoop (start-dfs.sh)和HBase(start- hbase.sh),并且需要等待HBase初始化完成(20秒左右),否则无法在 中创建表。第 2 关:批量删除数据package step2;import java.io.IOException;import java.util.ArrayList;import java.util.HashMap;import java.util.List;import java.util.Map;import org.apache.hadoop.conf.Configur

9、ation;import org.apache.hadoop.hbase.HBaseConfiguration;import org.apache.hadoop.hbase.HColumnDescriptor;import org.apache.hadoop.hbase.HTableDescriptor;import org.apache.hadoop.hbase.TableDescriptors;import org.apache.hadoop.hbase.TableName;import org.apache.hadoop.hbase.client.*;import org.apache.

10、hadoop.hbase.util.Bytes; public class Task public void batchDelete()throws Exception9/ 9*Begin*Configuration conf = HBaseConfiguration.create();Connection conn = ConnectionFactory.createConnection(conf);TableName tableName = TableName.valueOf("step2_table");Table table = conn.getTable(tabl

11、eName);List<String> rows1 = new ArrayList(); for(int i = 1; i<6;i+)String row = "row" + i; rows1.add(row);delete(table,rows1);List<String> rows2 = new ArrayList<>(); for(int i = 7;i<11;i+)String row = "row" + i; rows2.add(row);delete(table,rows2);* End*pub

12、lic void delete(Table table,List<String> rows)throws IOException List<Delete> deletes = new ArrayList<>();for(String str : rows)byte row = Bytes.toBytes(str);Delete delete = new Delete(row);deletes.add(delete);table.delete(deletes);第 3 关:批量导入数据至 HBasepackage step3;import java.io.IO

13、Exception;import java.util.ArrayList;import java.util.HashMap;import java.util.List;import java.util.Map;import org.apache.hadoop.conf.Configuration;import org.apache.hadoop.hbase.HBaseConfiguration;import org.apache.hadoop.hbase.HColumnDescriptor;import org.apache.hadoop.hbase.HTableDescriptor;impo

14、rt org.apache.hadoop.hbase.TableDescriptors;import org.apache.hadoop.hbase.TableName;import org.apache.hadoop.hbase.client.Admin;import org.apache.hadoop.hbase.client.ColumnFamilyDescriptor;import org.apache.hadoop.hbase.client.ColumnFamilyDescriptorBuilder;import org.apache.hadoop.hbase.client.Conn

15、ection;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.ResultScanner;import org.apache.hadoop.hbase.client.Scan;import org.ap

16、ache.hadoop.hbase.client.Table;import org.apache.hadoop.hbase.client.TableDescriptor;import org.apache.hadoop.hbase.client.TableDescriptorBuilder;import org.apache.hadoop.hbase.util.Bytes;public class Task public void batchPut()throws Exception*Begin*Configuration config = new Configuration();Connec

17、tion conn = ConnectionFactory.createConnection(config);Admin admin = conn.getAdmin();/ 建表TableName tableName = TableName.valueOf(Bytes.toBytes("stu");TableDescriptorBuilder builder =TableDescriptorBuilder.newBuilder(tableName);ColumnFamilyDescriptorfamilyColumnFamilyDescriptorBuilder.newBu

18、ilder(Bytes.toBytes("basic_info").build();ColumnFamilyDescriptorfamily2ColumnFamilyDescriptorBuilder.newBuilder(Bytes.toBytes("school_info").build( );=builder.setColumnFamily(family);builder.setColumnFamily(family2); admin.createTable(builder.build();List<Put> puts = new Ar

19、rayList<>();String rows = "20181122","20181123"Strin gbasic_i nfos二"阿克蒙",”male","1987-05- 23","telquot;,"HUNan-ChangSha","F格拉","male","1986-05- 23","telquot;,"HUNan-Cha

20、ngSha"String basic_colums = "name","gender","birthday","connect","address"Stringschool_infos="ChengXing","class12","Software","ChengXing","class 2 grade 2","Software"String school_colums = "college","class",&

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论