C # programming: DFS Distributed File System Code instance,

Source: Internet
Author: User

C # programming: DFS Distributed File System Code instance,

Recently, DFS Distributed File System was used in the project. Here we collect some information to learn about this system and how to use it in my project.

DFS Definition

Microsoft Dfs is a network server component that enables you to query and manage data on the network more easily.A Distributed File System combines files distributed on different computers into a single namespace.And createSingle, hierarchical multi-File ServerA more convenient way to share work with the server.

DFS advantages

DFS (Distributed File System)This makes it easier for users to access and manage files distributed physically across networks.. DFS providesSingle Access PointAndA logical tree structure, Through DFS,Users do not need to know their actual physical locations when accessing files, that is, files distributed on multiple servers are in front of users, just as they are in the same location on the network.. Through DFS, shared folders on different computers in the same network can be organized to form a separate, logical, and hierarchical shared file system.

DFS operation

DFS is a tree structure that containsA root directoryAndOne or more DFS links.

1. To create a DFS share, you must first create a DFS root.

2. Create one or more DFS links under each DFS root. Each link can point to a shared folder in the network.

Note:: The maximum number of Dfs links is 1000. If the destination folder of the Dfs link is not a Windows 2000 folder, the destination folder cannot have subfolders.

DFS practices

In the project system, dfs has the following processes in practice:

DFS Storage

1. Step 1: serialize the object into a string and compress it

======================================== Get the comparison result object ============ ================= Person p = new Person (); ========================================= serialize this object to a string ================ ================ string serializeStr = Maolin. common. serialize. serializeHelper. serialize (p ); // obtain the serialized string ========================== compress the string data ======== ====================== string str = GZipCompressHelper. GZipCompressString (serializeStr); // compress string data

2. Step 2: Convert the compressed string to a binary array and then convert it to a stream object.

Byte [] bData = System. Text. Encoding. UTF8.GetBytes (str); // convert to a binary bit stream, in UTF-8 Encoding style MemoryStream MS = new MemoryStream (bData );

MemoryStream is used to read and write data to memory instead of disk.MemoryStream encapsulates data stored in the form of an unsigned byte array. This array is initialized when a MemoryStream object is created, or this array can be created as an empty array. The encapsulated data can be directly accessed in the memory.Memory streams reduce the need for temporary buffers and temporary files in applications

3. Step 3: create a file name, call a method, store the stream object to dfs, and string the dfs object to the database.

Var fileName = tenantId + "_" + importLogID. toString (); // file name: Tenant ID + log id var dfsPath = Dfs. store (new DfsItem (BaseConst. publicDfsKeyspace, fileName + ". json ", MS, tenantId); // save the file to this dfs path CompareToolImportLogProvider. instance. create (status, dfsPath. toString (), createdBy, importLogID); // Save the generated data to the database.
DFS read

1. Step 1: retrieve the data and its dfspath from the database based on the id

======================================== Read database objects ============== ============== var compareToolImportLogProvider = CompareToolImportLogProvider. instance. getById (id ); // obtain the log record of the comparison tool based on the log id ====================== the dfs path ============================ var dfsPath = compar#limportlogprovider in the object. dfsPath; // read the dfs path

2. Step 2: directly convert the path to a binary array

Var item = GetDfsItem (dfsPath); // pass the dfs path to obtain the binary array

Auxiliary methods used

////// Private method: Obtain binary data /////////
 Private byte [] GetDfsItem (string dfsPath) {byte [] fileData = null; try {Dfs. get (new [] {dfsPath}, (operationResult, item) =>{ if (! OperationResult. succeeded) return; if (item. isStream) {fileData = new byte [item. length]; int index = 0; while (index <item. length) {int read = item. fileDataStream. read (fileData, index, (int) (item. length-index); index + = read;} else {fileData = item. fileDataBytes ;}});} catch (Exception ex) {}if (fileData = null) {throw new Maolin. dfsClient. dfsException ($ "the DFS file does not exist. dfsPath: [{dfsPath}] ");} return fileData ;}

3. Step 3: Convert the binary array into a string, decompress it, And deserialize it into an object.

Var str = System. text. encoding. UTF8.GetString (item); // convert the binary array to the string var strlcm = GZipCompressHelper. GZipDecompressString (str); // extract the string var person = Maolin. common. serialize. serializeHelper. deserialize
 
  
(Strlcm); // deserializes the object
 

This is a complete process of object storage to the file system and reading. If you need dfs, I believe you will be more skilled.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.