In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-14 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)05/31 Report--
这篇文章主要讲解了"Hadoop怎么自定制数据类型",文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习"Hadoop怎么自定制数据类型"吧!
一般有两个办法,一种较为简单的是针对值,另外一种更为完整的是对于键和值都适应的方法:
1、实现Writable接口:
/* DataInput and DataOutput 类是java.io的类 */
public interface Writable {
void readFields(DataInput in);
void write(DataOutput out);
}
下面是一个小例子:
public class Point3D implement Writable {
public float x, y, z;
public Point3D(float fx, float fy, float fz) {
this.x = fx;
this.y = fy;
this.z = fz;
}
public Point3D() {
this(0.0f, 0.0f, 0.0f);
}
public void readFields(DataInput in) throws IOException {
x = in.readFloat();
y = in.readFloat();
z = in.readFloat();
}
public void write(DataOutput out) throws IOException {
out.writeFloat(x);
out.writeFloat(y);
out.writeFloat(z);
}
public String toString() {
return Float.toString(x) + ", "
+ Float.toString(y) + ", "
+ Float.toString(z);
}
}
2、对于键来说,需要指定排序规则,对此,Java版Hadoop的办法是实现WritableComparable这个泛型接口,WritableComparable,顾名思义了,一半是Writable,一半是Comparable
public interface WritableComparable {
public void readFields(DataInput in);
public void write(DataOutput out);
public int compareTo(T other);
}
这里的compareTo方法是默认的key排序
先给出下面的简单例子,再做说明和扩展。
public class Point3D inplements WritableComparable {
public float x, y, z;
public Point3D(float fx, float fy, float fz) {
this.x = fx;
this.y = fy;
this.z = fz;
}
public Point3D() {
this(0.0f, 0.0f, 0.0f);
}
public void readFields(DataInput in) throws IOException {
x = in.readFloat();
y = in.readFloat();
z = in.readFloat();
}
public void write(DataOutput out) throws IOException {
out.writeFloat(x);
out.writeFloat(y);
out.writeFloat(z);
}
public String toString() {
return Float.toString(x) + ", "
+ Float.toString(y) + ", "
+ Float.toString(z);
}
public float distanceFromOrigin() {
return (float) Math.sqrt( x*x + y*y +z*z);
}
public int compareTo(Point3D other) {
return Float.compareTo(
distanceFromOrigin(),
other.distanceFromOrigin());
}
public boolean equals(Object o) {
if( !(o instanceof Point3D)) {
return false;
}
Point3D other = (Point3D) o;
return this.x == o.x
&& this.y == o.y
&& this.z == o.z;
}
/* 实现 hashCode() 方法很重要
* Hadoop的Partitioners会用到这个方法,后面再说
*/
public int hashCode() {
return Float.floatToIntBits(x)
^ Float.floatToIntBits(y)
^ Float.floatToIntBits(z);
}
}
如果要将对象写入数据库则还要继承DBWritable接口
public interface WritableComparable {
public void write(PreparedStatement statement) throwsSQLException;
public void readFields(ResultSet resultSet) throws SQLException;}
下面写个例子
public class LocationBean implements Writable, DBWritable {
private String mobilenetworkcode;
private String mobilecountrycode;
private Integer cellid;
private Integer locationareacode;
private Integer baiduareaid;
private Double lat;
private Double lng;
private Integer areaid;
@Override
public void write(PreparedStatement statement) throws SQLException {
int index = 1;
statement.setString(index++, this.getMobilenetworkcode());
statement.setString(index++, this.getMobilecountrycode());
statement.setInt(index++, this.getCellid());
statement.setInt(index++, this.getLocationareacode());
statement.setInt(index++, this.getBaiduareaid());
statement.setDouble(index++, this.getLat());
statement.setDouble(index++, this.getLng());
statement.setInt(index, this.getAreaid());
}
@Override
public void readFields(ResultSet resultSet) throws SQLException {
this.mobilenetworkcode = resultSet.getString(1);
this.mobilecountrycode = resultSet.getString(2);
this.cellid = resultSet.getInt(3);
this.locationareacode = resultSet.getInt(4);
this.baiduareaid = resultSet.getInt(5);
this.lat = resultSet.getDouble(6);
this.lng = resultSet.getDouble(7);
this.areaid = resultSet.getInt(8);
}
@Override
public void write(DataOutput out) throws IOException {
// TODO Auto-generated method stub
}
@Override
public void readFields(DataInput in) throws IOException {
}
public String getMobilenetworkcode() {
return mobilenetworkcode;
}
public void setMobilenetworkcode(String mobilenetworkcode) {
this.mobilenetworkcode = mobilenetworkcode;
}
public String getMobilecountrycode() {
return mobilecountrycode;
}
public void setMobilecountrycode(String mobilecountrycode) {
this.mobilecountrycode = mobilecountrycode;
}
public Integer getCellid() {
return cellid;
}
public void setCellid(Integer cellid) {
this.cellid = cellid;
}
public Integer getLocationareacode() {
return locationareacode;
}
public void setLocationareacode(Integer locationareacode) {
this.locationareacode = locationareacode;
}
public Integer getBaiduareaid() {
return baiduareaid;
}
public void setBaiduareaid(Integer baiduareaid) {
this.baiduareaid = baiduareaid;
}
public Double getLat() {
return lat;
}
public void setLat(Double lat) {
this.lat = lat;
}
public Double getLng() {
return lng;
}
public void setLng(Double lng) {
this.lng = lng;
}
public Integer getAreaid() {
return areaid;
}
public void setAreaid(Integer areaid) {
this.areaid = areaid;
}
}
感谢各位的阅读,以上就是"Hadoop怎么自定制数据类型"的内容了,经过本文的学习后,相信大家对Hadoop怎么自定制数据类型这一问题有了更深刻的体会,具体使用情况还需要大家实践验证。这里是,小编将为大家推送更多相关知识点的文章,欢迎关注!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.