您好,欢迎访问三七文档
当前位置:首页 > 商业/管理/HR > 管理学资料 > educoder平台使用Java操作HBase
1创建表packagestep1;importjava.io.IOException;importorg.apache.hadoop.conf.Configuration;importorg.apache.hadoop.hbase.HBaseConfiguration;importorg.apache.hadoop.hbase.HColumnDescriptor;importorg.apache.hadoop.hbase.HTableDescriptor;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;/***HBase1.0versionofExampleClientthatuses{@codeConnection},*{@codeAdmin}and{@codeTable}.*/publicclassTask{publicvoidcreateTable()throwsException{/*********Begin*********/Configurationconfig=HBaseConfiguration.create();Connectionconnection=ConnectionFactory.createConnection(config);try{//CreatetableAdminadmin=connection.getAdmin();try{TableNametableName=TableName.valueOf(dept);//新API构建表//TableDescriptor对象通过TableDescriptorBuilder构建;TableDescriptorBuildertableDescriptor=TableDescriptorBuilder.newBuilder(tableName);ColumnFamilyDescriptorfamily=ColumnFamilyDescriptorBuilder.newBuilder(Bytes.toBytes(data)).build();//构建列族对象tableDescriptor.setColumnFamily(family);//设置列族admin.createTable(tableDescriptor.build());//创建表TableNameemp=TableName.valueOf(emp);//新API构建表//TableDescriptor对象通过TableDescriptorBuilder构建;TableDescriptorBuilderempDescriptor=TableDescriptorBuilder.newBuilder(emp);ColumnFamilyDescriptorempfamily=ColumnFamilyDescriptorBuilder.newBuilder(Bytes.toBytes(emp)).build();//构建列族对象empDescriptor.setColumnFamily(empfamily);//设置列族admin.createTable(empDescriptor.build());//创建表}finally{admin.close();}}finally{connection.close();}/*********End*********/}}命令行:start-dfs.sh(Hadoop启动)回车start-hbase.sh(hbase启动)测评———————————————————————————————————————2添加数据packagestep2;importjava.io.IOException;importorg.apache.hadoop.conf.Configuration;importorg.apache.hadoop.hbase.HBaseConfiguration;importorg.apache.hadoop.hbase.HColumnDescriptor;importorg.apache.hadoop.hbase.HTableDescriptor;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{publicvoidinsertInfo()throwsException{/*********Begin*********/Configurationconfig=HBaseConfiguration.create();Connectionconnection=ConnectionFactory.createConnection(config);Adminadmin=connection.getAdmin();TableNametableName=TableName.valueOf(tb_step2);TableDescriptorBuildertableDescriptor=TableDescriptorBuilder.newBuilder(tableName);ColumnFamilyDescriptorfamily=ColumnFamilyDescriptorBuilder.newBuilder(Bytes.toBytes(data)).build();//构建列族对象tableDescriptor.setColumnFamily(family);//设置列族admin.createTable(tableDescriptor.build());//创建表//添加数据byte[]row1=Bytes.toBytes(row1);Putput1=newPut(row1);byte[]columnFamily1=Bytes.toBytes(data);//列byte[]qualifier1=Bytes.toBytes(String.valueOf(1));//列族修饰词byte[]value1=Bytes.toBytes(张三丰);//值put1.addColumn(columnFamily1,qualifier1,value1);byte[]row2=Bytes.toBytes(row2);Putput2=newPut(row2);byte[]columnFamily2=Bytes.toBytes(data);//列byte[]qualifier2=Bytes.toBytes(String.valueOf(2));//列族修饰词byte[]value2=Bytes.toBytes(张无忌);//值put2.addColumn(columnFamily2,qualifier2,value2);Tabletable=connection.getTable(tableName);table.put(put1);table.put(put2);/*********End*********/}}测评———————————————————————————————————————3获取数据packagestep3;importjava.io.IOException;importorg.apache.hadoop.conf.Configuration;importorg.apache.hadoop.hbase.HBaseConfiguration;importorg.apache.hadoop.hbase.HColumnDescriptor;importorg.apache.hadoop.hbase.HTableDescriptor;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.hba
本文标题:educoder平台使用Java操作HBase
链接地址:https://www.777doc.com/doc-5242336 .html